1 /*
2  *  Copyright (C) 2011-2016  OpenDungeons Team
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef LOGMANAGER_H
19 #define LOGMANAGER_H
20 
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 
25 #include <SFML/System.hpp>
26 
27 #include <OgreSingleton.h>
28 
29 #include "utils/Helper.h"
30 #include "utils/LogMessageLevel.h"
31 #include "utils/LogSink.h"
32 
33 #define OD_LOG_ERR(_message)                      LogManager::getSingleton().logMessage(LogMessageLevel::CRITICAL, __FILE__, __LINE__, (std::string("") + _message))
34 #define OD_LOG_WRN(_message)                      LogManager::getSingleton().logMessage(LogMessageLevel::WARNING, __FILE__, __LINE__, (std::string("") + _message))
35 #define OD_LOG_INF(_message)                      LogManager::getSingleton().logMessage(LogMessageLevel::NORMAL, __FILE__, __LINE__, (std::string("") + _message))
36 #define OD_LOG_DBG(_message)                      LogManager::getSingleton().logMessage(LogMessageLevel::TRIVIAL, __FILE__, __LINE__, (std::string("") + _message))
37 
38 #define OD_ASSERT_TRUE(_condition)                if (!(_condition)) LogManager::getSingleton().logMessage(LogMessageLevel::CRITICAL, __FILE__, __LINE__, std::string(#_condition))
39 #define OD_ASSERT_TRUE_MSG(_condition, _message)  if (!(_condition)) LogManager::getSingleton().logMessage(LogMessageLevel::CRITICAL, __FILE__, __LINE__, (std::string("") + _message))
40 
41 //! \brief Helper/wrapper class to provide thread-safe logging when ogre is compiled without threads.
42 class LogManager : public Ogre::Singleton<LogManager>
43 {
44 public:
45     LogManager();
46     ~LogManager();
47 
48     //! \brief Add a sink for log messages.
49     void addSink(std::unique_ptr<LogSink> sink);
50 
51     //! \brief Set the global minimum logging level.
52     void setLevel(LogMessageLevel level);
53 
54     //! \brief Set the minimum logging level per module.
55     void setModuleLevel(const char* module, LogMessageLevel level);
56 
57     //! \brief Log a message to the sinks.
58     void logMessage(LogMessageLevel level, const char* filepath, int line, const std::string& message);
59 
60     static const std::string GAMELOG_NAME;
61 private:
62     LogManager(const LogManager&) = delete;
63     LogManager& operator=(const LogManager&) = delete;
64 
65     LogMessageLevel mLevel;
66     std::map<std::string, LogMessageLevel> mModuleLevel;
67     sf::Mutex mLock;
68     std::vector<std::unique_ptr<LogSink>> mSinks;
69     std::stringstream mTimestampStream;
70 };
71 
72 #endif // LOGMANAGER_H
73