Elements  6.2
A C++ base framework for the Euclid Software.
Logging.h
Go to the documentation of this file.
1 
26 #ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_LOGGING_H_
27 #define ELEMENTSKERNEL_ELEMENTSKERNEL_LOGGING_H_
28 
29 #include <map>
30 #include <string>
31 #include <utility> // for forward
32 
33 #include <log4cpp/Category.hh>
34 
35 #include "ElementsKernel/Export.h" // ELEMENTS_API
36 #include "ElementsKernel/Path.h" // for Item
37 
38 namespace Elements {
39 
94 
95 private:
96  // We declare the LogMessageStream here because it is used from the public
97  // functions. It is defined in the private section at the end.
98  class LogMessageStream;
99 
100 public:
107  static Logging getLogger(const std::string& name = "");
108 
119  static void setLevel(std::string level);
120 
137  static void setLogFile(const Path::Item& fileName);
138 
143  void debug(const std::string& logMessage) {
144  m_log4cppLogger.debug(logMessage);
145  }
146 
152  template <typename... Args>
153  void debug(const char* stringFormat, Args&&... args) {
154  m_log4cppLogger.debug(stringFormat, std::forward<Args>(args)...);
155  }
156 
163  return LogMessageStream{m_log4cppLogger, &log4cpp::Category::debug};
164  }
165 
170  void info(const std::string& logMessage) {
171  m_log4cppLogger.info(logMessage);
172  }
173 
179  template <typename... Args>
180  void info(const char* stringFormat, Args&&... args) {
181  m_log4cppLogger.info(stringFormat, std::forward<Args>(args)...);
182  }
183 
190  return LogMessageStream{m_log4cppLogger, &log4cpp::Category::info};
191  }
192 
197  void warn(const std::string& logMessage) {
198  m_log4cppLogger.warn(logMessage);
199  }
200 
206  template <typename... Args>
207  void warn(const char* stringFormat, Args&&... args) {
208  m_log4cppLogger.warn(stringFormat, std::forward<Args>(args)...);
209  }
210 
217  return LogMessageStream{m_log4cppLogger, &log4cpp::Category::warn};
218  }
219 
224  void error(const std::string& logMessage) {
225  m_log4cppLogger.error(logMessage);
226  }
227 
233  template <typename... Args>
234  void error(const char* stringFormat, Args&&... args) {
235  m_log4cppLogger.error(stringFormat, std::forward<Args>(args)...);
236  }
237 
244  return LogMessageStream{m_log4cppLogger, &log4cpp::Category::error};
245  }
246 
251  void fatal(const std::string& logMessage) {
252  m_log4cppLogger.fatal(logMessage);
253  }
254 
260  template <typename... Args>
261  void fatal(const char* stringFormat, Args&&... args) {
262  m_log4cppLogger.fatal(stringFormat, std::forward<Args>(args)...);
263  }
264 
271  return LogMessageStream{m_log4cppLogger, &log4cpp::Category::fatal};
272  }
273 
279  void log(log4cpp::Priority::Value level, const std::string& logMessage) {
280  m_log4cppLogger.log(level, logMessage);
281  }
282 
289  template <typename... Args>
290  void log(log4cpp::Priority::Value level, const char* stringFormat, Args&&... args) {
291  m_log4cppLogger.log(level, stringFormat, std::forward<Args>(args)...);
292  }
293 
294 private:
295  explicit Logging(log4cpp::Category& log4cppLogger);
296 
297  log4cpp::Category& m_log4cppLogger;
298 
310  // The P_log_func is a pointer to member function. If you have no idea what
311  // this is don't get scared! Just have a look in the following link:
312  // http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible
313  using P_log_func = void (log4cpp::Category::*)(const std::string&);
314 
315  public:
316  LogMessageStream(log4cpp::Category& logger, P_log_func log_func);
318  LogMessageStream(const LogMessageStream& other);
319  ~LogMessageStream();
320  template <typename T>
322  m_message << m;
323  return *this;
324  }
325 
326  private:
327  log4cpp::Category& m_logger;
329  std::stringstream m_message{};
330  };
331 };
332 
333 } // namespace Elements
334 
335 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_LOGGING_H_
336 
Elements::Kernel::Path::Item
boost::filesystem::path Item
Definition: Path.h:56
Elements::Logging::error
void error(const char *stringFormat, Args &&... args)
Definition: Logging.h:234
Elements::Logging::warn
LogMessageStream warn()
Definition: Logging.h:216
Export.h
defines the macros to be used for explicit export of the symbols
std::string
STL class.
Path.h
provide functions to retrieve resources pointed by environment variables
Elements::Logging
Logging API of the Elements framework.
Definition: Logging.h:93
std::stringstream
STL class.
Elements::Logging::log
void log(log4cpp::Priority::Value level, const std::string &logMessage)
Definition: Logging.h:279
Elements::Logging::debug
void debug(const char *stringFormat, Args &&... args)
Definition: Logging.h:153
Elements::Logging::info
void info(const char *stringFormat, Args &&... args)
Definition: Logging.h:180
Elements::Logging::warn
void warn(const char *stringFormat, Args &&... args)
Definition: Logging.h:207
Elements::Logging::info
LogMessageStream info()
Definition: Logging.h:189
Elements::Kernel::Units::m
constexpr double m
Definition: SystemOfUnits.h:79
Elements::Logging::m_log4cppLogger
log4cpp::Category & m_log4cppLogger
Definition: Logging.h:297
ELEMENTS_API
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
Elements::Logging::LogMessageStream::m_logger
log4cpp::Category & m_logger
Definition: Logging.h:327
Elements::Logging::error
LogMessageStream error()
Definition: Logging.h:243
Elements::Logging::fatal
void fatal(const char *stringFormat, Args &&... args)
Definition: Logging.h:261
Elements::Logging::warn
void warn(const std::string &logMessage)
Definition: Logging.h:197
Elements::Logging::debug
void debug(const std::string &logMessage)
Definition: Logging.h:143
Elements::Logging::debug
LogMessageStream debug()
Definition: Logging.h:162
Elements::Logging::fatal
void fatal(const std::string &logMessage)
Definition: Logging.h:251
Elements::Logging::info
void info(const std::string &logMessage)
Definition: Logging.h:170
Elements::Logging::LogMessageStream
A helper class for logging messages using the "<<" operator.
Definition: Logging.h:309
Elements::Logging::LogMessageStream::m_log_func
P_log_func m_log_func
Definition: Logging.h:328
Elements::Logging::error
void error(const std::string &logMessage)
Definition: Logging.h:224
Elements::Logging::LogMessageStream::operator<<
LogMessageStream & operator<<(const T &m)
Definition: Logging.h:321
Elements::Logging::log
void log(log4cpp::Priority::Value level, const char *stringFormat, Args &&... args)
Definition: Logging.h:290
Elements::Logging::fatal
LogMessageStream fatal()
Definition: Logging.h:270
Elements::Logging::LogMessageStream::P_log_func
void(log4cpp::Category::*)(const std::string &) P_log_func
Definition: Logging.h:313
Elements
Definition: callBackExample.h:35