Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <string>
30 #include <map>
31 #include <utility> // for forward
32 
33 #include <boost/filesystem.hpp>
34 #include <log4cpp/Category.hh>
35 
36 #include "ElementsKernel/Export.h" // ELEMENTS_API
37 
38 namespace Elements {
39 
94 
95 private:
96 
97  // We declare the LogMessageStream here because it is used from the public
98  // functions. It is defined in the private section at the end.
99  class LogMessageStream;
100 
101 public:
102 
109  static Logging getLogger(const std::string& name = "");
110 
121  static void setLevel(std::string level);
122 
139  static void setLogFile(const boost::filesystem::path& fileName);
140 
145  void debug(const std::string& logMessage) {
146  m_log4cppLogger.debug(logMessage);
147  }
148 
154  template<typename ...Args>
155  void debug(const char *stringFormat, Args &&...args) {
156  m_log4cppLogger.debug(stringFormat, std::forward<Args>(args)...);
157  }
158 
165  return LogMessageStream {m_log4cppLogger, &log4cpp::Category::debug};
166  }
167 
172  void info(const std::string& logMessage) {
173  m_log4cppLogger.info(logMessage);
174  }
175 
181  template<typename ...Args>
182  void info(const char *stringFormat, Args &&...args) {
183  m_log4cppLogger.info(stringFormat, std::forward<Args>(args)...);
184  }
185 
192  return LogMessageStream {m_log4cppLogger, &log4cpp::Category::info};
193  }
194 
199  void warn(const std::string& logMessage) {
200  m_log4cppLogger.warn(logMessage);
201  }
202 
208  template<typename ...Args>
209  void warn(const char *stringFormat, Args &&...args) {
210  m_log4cppLogger.warn(stringFormat, std::forward<Args>(args)...);
211  }
212 
219  return LogMessageStream {m_log4cppLogger, &log4cpp::Category::warn};
220  }
221 
226  void error(const std::string& logMessage) {
227  m_log4cppLogger.error(logMessage);
228  }
229 
235  template<typename ...Args>
236  void error(const char *stringFormat, Args &&...args) {
237  m_log4cppLogger.error(stringFormat, std::forward<Args>(args)...);
238  }
239 
246  return LogMessageStream {m_log4cppLogger, &log4cpp::Category::error};
247  }
248 
253  void fatal(const std::string& logMessage) {
254  m_log4cppLogger.fatal(logMessage);
255  }
256 
262  template<typename ...Args>
263  void fatal(const char *stringFormat, Args &&...args) {
264  m_log4cppLogger.fatal(stringFormat, std::forward<Args>(args)...);
265  }
266 
273  return LogMessageStream {m_log4cppLogger, &log4cpp::Category::fatal};
274  }
275 
276 
282  void log(log4cpp::Priority::Value level, const std::string& logMessage) {
283  m_log4cppLogger.log(level, logMessage);
284  }
285 
292  template<typename ...Args>
293  void log(log4cpp::Priority::Value level, const char *stringFormat, Args &&...args) {
294  m_log4cppLogger.log(level, stringFormat, std::forward<Args>(args)...);
295  }
296 
297 private:
298 
299  explicit Logging(log4cpp::Category& log4cppLogger);
300 
301  log4cpp::Category& m_log4cppLogger;
302 
314  // The P_log_func is a pointer to member function. If you have no idea what
315  // this is don't get scared! Just have a look in the following link:
316  // http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible
317  using P_log_func = void (log4cpp::Category::*)(const std::string&);
318  public:
319  LogMessageStream(log4cpp::Category& logger, P_log_func log_func);
321  ~LogMessageStream();
322  template <typename T>
324  m_message << m;
325  return *this;
326  }
327  private:
328  log4cpp::Category& m_logger;
330  std::stringstream m_message {};
331  };
332 
333 };
334 
335 } // namespace Elements
336 
337 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_LOGGING_H_
338 
LogMessageStream info()
Definition: Logging.h:191
void(log4cpp::Category::*)(const std::string &) P_log_func
Definition: Logging.h:317
log4cpp::Category & m_logger
Definition: Logging.h:328
log4cpp::Category & m_log4cppLogger
Definition: Logging.h:301
LogMessageStream debug()
Definition: Logging.h:164
void fatal(const char *stringFormat, Args &&...args)
Definition: Logging.h:263
void info(const std::string &logMessage)
Definition: Logging.h:172
LogMessageStream warn()
Definition: Logging.h:218
void debug(const std::string &logMessage)
Definition: Logging.h:145
void error(const char *stringFormat, Args &&...args)
Definition: Logging.h:236
STL class.
defines the macros to be used for explicit export of the symbols
constexpr double m
Definition: SystemOfUnits.h:79
LogMessageStream & operator<<(const T &m)
Definition: Logging.h:323
A helper class for logging messages using the &quot;&lt;&lt;&quot; operator.
Definition: Logging.h:313
void debug(const char *stringFormat, Args &&...args)
Definition: Logging.h:155
void warn(const std::string &logMessage)
Definition: Logging.h:199
void log(log4cpp::Priority::Value level, const char *stringFormat, Args &&...args)
Definition: Logging.h:293
void info(const char *stringFormat, Args &&...args)
Definition: Logging.h:182
Logging API of the Elements framework.
Definition: Logging.h:93
boost::filesystem::path path
Definition: DataSyncUtils.h:33
LogMessageStream error()
Definition: Logging.h:245
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
void warn(const char *stringFormat, Args &&...args)
Definition: Logging.h:209
void fatal(const std::string &logMessage)
Definition: Logging.h:253
void log(log4cpp::Priority::Value level, const std::string &logMessage)
Definition: Logging.h:282
void error(const std::string &logMessage)
Definition: Logging.h:226
LogMessageStream fatal()
Definition: Logging.h:272