i6engine  1.0
Logger.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_LOGGER_H__
26 #define __I6ENGINE_UTILS_LOGGER_H__
27 
28 #include <string>
29 
30 #include "i6engine/i6engineBuildSettings.h"
31 
32 #ifdef ISIXE_WITH_LOGGING
35 
36  #include "boost/log/common.hpp"
37  #include "boost/log/core.hpp"
38  #include "boost/log/sinks.hpp"
39 #endif
40 
41 #ifdef ISIXE_WITH_LOGGING
42  #define ISIXE_LOG_DEBUG(module, message) {\
43  std::stringstream logMessageString; \
44  logMessageString << message; \
45  i6e::utils::Logger::GetSingleton().LogMessage(i6e::utils::LogLevel::LOGDEBUG, __FILE__, __LINE__, module, logMessageString.str());\
46  }
47  #define ISIXE_LOG_INFO(module, message) {\
48  std::stringstream logMessageString; \
49  logMessageString << message; \
50  i6e::utils::Logger::GetSingleton().LogMessage(i6e::utils::LogLevel::LOGINFO, __FILE__, __LINE__, module, logMessageString.str());\
51  }
52  #define ISIXE_LOG_WARN(module, message) {\
53  std::stringstream logMessageString; \
54  logMessageString << message; \
55  i6e::utils::Logger::GetSingleton().LogMessage(i6e::utils::LogLevel::LOGWARN, __FILE__, __LINE__, module, logMessageString.str());\
56  }
57  #define ISIXE_LOG_ERROR(module, message) {\
58  std::stringstream logMessageString; \
59  logMessageString << message; \
60  i6e::utils::Logger::GetSingleton().LogMessage(i6e::utils::LogLevel::LOGERROR, __FILE__, __LINE__, module, logMessageString.str());\
61  }
62  #define ISIXE_LOG_FATAL(module, message) {\
63  std::stringstream logMessageString; \
64  logMessageString << message; \
65  i6e::utils::Logger::GetSingleton().LogMessage(i6e::utils::LogLevel::LOGFATAL, __FILE__, __LINE__, module, logMessageString.str());\
66  }
67 
68  #define ISIXE_LOG_SETLEVEL(level) {\
69  i6e::utils::Logger::GetSingleton().setLogLevel(level);\
70  }
71 #else
72  #define ISIXE_LOG_DEBUG(module, message) {\
73  }
74  #define ISIXE_LOG_INFO(module, message) {\
75  }
76  #define ISIXE_LOG_WARN(module, message) {\
77  }
78  #define ISIXE_LOG_ERROR(module, message) {\
79  }
80  #define ISIXE_LOG_FATAL(module, message) {\
81  }
82 
83  #define ISIXE_LOG_SETLEVEL(level) {\
84  }
85 #endif // ISIXE_WITH_LOGGING
86 
87 namespace i6e {
88 namespace utils {
89 
90  static const std::string logEnumNames[] = { "Debug", "Info", "Warning", "ERROR", "FATAL", "---" };
91 
95  enum class LogLevel : short {
96  LOGDEBUG = 0,
97  LOGINFO = 1,
98  LOGWARN = 2,
99  LOGERROR = 3,
100  LOGFATAL = 4,
101  LOGNONE = 5
102  };
103 
104 #ifdef ISIXE_WITH_LOGGING
105  class Logger;
106 
107  typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> TextSink;
108  typedef boost::shared_ptr<TextSink> SinkPtr;
109  typedef boost::shared_ptr<Logger> LogPtr;
110 
123  class ISIXE_UTILS_API Logger : public i6e::utils::Singleton<Logger> {
124  friend class i6e::utils::Singleton<Logger>;
125 
126  public:
130  inline void setLogLevel(const LogLevel logLevel) { _logLevel = logLevel; }
131 
135  void LogMessage(LogLevel level, const std::string & file, const int line, const std::string & module, const std::string & message);
136 
137  protected:
138  Logger();
139 
140  private:
141  boost::log::sources::channel_logger_mt<std::string> _logger;
142  LogLevel _logLevel;
143  };
144 #endif /* ISIXE_WITH_LOGGING */
145 
146 } /* namespace utils */
147 } /* namespace i6e */
148 
149 #endif /* __I6ENGINE_UTILS_LOGGER_H__ */
150 
LogLevel
Defines values to specify severity/importance of log message.
Definition: Logger.h:95
Derive from this templated class to make a class a singleton. Refer to the Singleton Design Pattern i...
Definition: Singleton.h:44
static const std::string logEnumNames[]
Definition: Logger.h:90
#define ISIXE_UTILS_API