rad  3.0.0
logger.hpp
Go to the documentation of this file.
1 
10 #ifndef RAD_LOGGER_HPP
11 #define RAD_LOGGER_HPP
12 
13 #include <log4cplus/logger.h>
14 #include <log4cplus/loggingmacros.h>
15 #include <log4cplus/version.h>
16 #include <log4cplus/configurator.h>
17 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2,0,0)
18 #include <log4cplus/initializer.h>
19 #endif
20 
21 #ifdef NDEBUG
22 #define RAD_TRACE(logger)
23 #else
24 #define RAD_TRACE(logger) \
25  LOG4CPLUS_TRACE(logger, __FILE__ << " " << __FUNCTION__ << " " << __LINE__)
26 #endif
27 
28 #if 0
29 #define LOG4CPLUS_STATE(logger, log_event) \
30  if (logger.isEnabledFor(log4cplus::STATE_LOG_LEVEL)) { \
31  log4cplus::tostringstream _log4cplus_buf; \
32  _log4cplus_buf << log_event; \
33  logger.forcedLog(log4cplus::STATE_LOG_LEVEL, _log4cplus_buf.str(), __FILE__, __LINE__); \
34  }
35 
36 #define LOG4CPLUS_EVENT(logger, log_event) \
37  if (logger.isEnabledFor(log4cplus::EVENT_LOG_LEVEL)) { \
38  log4cplus::tostringstream _log4cplus_buf; \
39  _log4cplus_buf << log_event; \
40  logger.forcedLog(log4cplus::EVENT_LOG_LEVEL, _log4cplus_buf.str(), __FILE__, __LINE__); \
41  }
42 
43 #define LOG4CPLUS_ACTION(logger, log_event) \
44  if (logger.isEnabledFor(log4cplus::ACTION_LOG_LEVEL)) { \
45  log4cplus::tostringstream _log4cplus_buf; \
46  _log4cplus_buf << log_event; \
47  logger.forcedLog(log4cplus::ACTION_LOG_LEVEL, _log4cplus_buf.str(), __FILE__, __LINE__); \
48  }
49 
50 #define LOG4CPLUS_GUARD(logger, log_event) \
51  if (logger.isEnabledFor(log4cplus::GUARD_LOG_LEVEL)) { \
52  log4cplus::tostringstream _log4cplus_buf; \
53  _log4cplus_buf << log_event; \
54  logger.forcedLog(log4cplus::GUARD_LOG_LEVEL, _log4cplus_buf.str(), __FILE__, __LINE__); \
55  }
56 
57 namespace log4cplus {
58 
59 // OFF, FATAL, ERROR, WARN,
60 // INFO_LOG_LEVEL = 20000
61 const LogLevel STATE_LOG_LEVEL = 10105;
62 const LogLevel EVENT_LOG_LEVEL = 10104;
63 const LogLevel ACTION_LOG_LEVEL = 10102;
64 const LogLevel GUARD_LOG_LEVEL = 10101;
65 // DEBUG_LOG_LEVEL = 10000
66 // TRACE_LOG_LEVEL = 0 (all)
67 
68 static log4cplus::tstring const STATE_LOG_LEVEL_STR(LOG4CPLUS_TEXT("STATE"));
69 static log4cplus::tstring const EVENT_LOG_LEVEL_STR(LOG4CPLUS_TEXT("EVENT"));
70 static log4cplus::tstring const ACTION_LOG_LEVEL_STR(LOG4CPLUS_TEXT("ACTION"));
71 static log4cplus::tstring const GUARD_LOG_LEVEL_STR(LOG4CPLUS_TEXT("GUARD"));
72 static log4cplus::tstring const EMPTY_LOG_LEVEL_STR(LOG4CPLUS_TEXT(""));
73 
74 } // namespace log4cplus
75 #endif
76 
77 namespace rad {
78 
79 const std::string LOGGER_NAME = "rad";
80 const std::string SM_LOGGER_NAME = "rad.sm";
81 
82 log4cplus::Logger& GetLogger();
83 log4cplus::Logger& GetSmLogger();
84 
85 [[deprecated("Replaced by rad::LogInitializer object")]]
86 void LogInitialize();
87 
88 [[deprecated("Replaced by rad::LogInitializer::Configure() method")]]
89 void LogConfigure(const std::string& filename = "");
90 
91 #if 0
92 log4cplus::tstring const& RadLogLevelToString(log4cplus::LogLevel ll);
93 log4cplus::LogLevel RadStringToLogLevel(const log4cplus::tstring& str);
94 #endif
95 
97 public:
98  inline LogInitializer() {
99 #if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2,0,0)
100  log4cplus::initialize();
101 #endif
102  log4cplus::BasicConfigurator().configure();
103  // log4cplus::Logger::getInstance(rad::LOGGER_NAME).setLogLevel(log4cplus::GUARD_LOG_LEVEL);
104  GetLogger().setLogLevel(log4cplus::INFO_LOG_LEVEL);
105  GetSmLogger().setLogLevel(log4cplus::INFO_LOG_LEVEL);
106 #if 0
107  /*
108  * Add RAD specific Log Levels.
109  */
110  log4cplus::getLogLevelManager().pushToStringMethod(rad::RadLogLevelToString);
111  log4cplus::getLogLevelManager().pushFromStringMethod(rad::RadStringToLogLevel);
112 #endif
113  }
114 
115  inline ~LogInitializer() {
116 #if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2,0,0)
117  log4cplus::Logger::shutdown();
118 #endif
119  }
120 
121  inline void Configure(const std::string& filename = "") {
122  if (filename.size() > 0) {
123  log4cplus::PropertyConfigurator::doConfigure(filename);
124  LOG4CPLUS_INFO(GetLogger(), "Loaded log properties: " << filename);
125  }
126  }
127 
128  LogInitializer(const LogInitializer&) = delete;
130 
131 private:
132 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2,0,0)
133  log4cplus::Initializer mInitializer;
134 #endif
135 };
136 
137 } // namespace rad
138 
139 // ============ FOLLOWING CODE IS OBSOLETE! TO BE REMOVED ===========
140 // When removing the obsolte code, also the dependency between
141 // core->utils library can be removed in the wscript!
142 //
143 //#if 0
144 #include <rad/helper.hpp>
145 
146 namespace rad {
147 
148 /*
149  * Supported Log Levels.
150  */
151 enum LogLevel {
166 };
167 
169 const int LOG_MAX_LEN = 2048;
170 const int LOG_TIMESTAMP_MAXLEN = 100;
171 
172 /*
173  * Logger class.
174  */
175 class Logger {
176  public:
177  Logger(){};
178  inline virtual ~Logger();
179  inline std::ostringstream& Get(LogLevel level = LOG_LEVEL_INFO);
180 
181  inline static LogLevel& ReportingLevel();
182  inline static bool& EnableConsole();
183  inline static bool& EnableLogMon();
184  inline static bool IsEnabled();
185  inline static std::string& ModuleName();
186  inline static std::string& ProcName();
187  inline static std::string LevelToString(LogLevel level);
188  inline static LogLevel LevelFromString(const std::string& level);
189 
190  protected:
191  std::ostringstream m_output;
192 
193  private:
194  Logger(const Logger&);
195  Logger& operator=(const Logger&);
196 };
197 
199  m_output << std::endl;
200  if (Logger::EnableConsole() == true) {
201  fprintf(stdout, "%s %s %s %s", rad::GetTimestamp().c_str(), ModuleName().c_str(),
202  ModuleName().c_str(), m_output.str().c_str());
203  fflush(stdout);
204  }
205 
206  if (Logger::EnableLogMon() == true) {
207  // @TODO add code to verbose the log somewhere
208  }
209 }
210 
211 std::ostringstream& Logger::Get(LogLevel level) {
212  m_output << LevelToString(level) << " ";
213  return m_output;
214 }
215 
217  static LogLevel reporting_level = LOG_LEVEL_INFO;
218  return reporting_level;
219 }
220 
222  static bool is_console_enabled = false;
223  return is_console_enabled;
224 }
225 
227  static bool is_logmon_enabled = false;
228  return is_logmon_enabled;
229 }
230 
232  return Logger::EnableConsole() == true || Logger::EnableLogMon() == true;
233 }
234 
235 std::string& Logger::ModuleName() {
236  static std::string mod_name = ""; // @TODO initialized to module name
237  return mod_name;
238 }
239 
241  static const char* const buffer[] = {"ERROR", "WARNING", "STATE", "EVENT", "GUARD",
242  "ACTION", "INFO", "DEBUG", "DEBUG1", "DEBUG2",
243  "DEBUG3", "DEBUG4", "TRACE"};
244  return (level < LOG_LEVEL_ERROR || level > LOG_LEVEL_TRACE) ? "" : buffer[level];
245 }
246 
249  return rad::LOG_LEVEL_TRACE;
250  } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG4)) {
251  return rad::LOG_LEVEL_DEBUG4;
252  } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG3)) {
253  return rad::LOG_LEVEL_DEBUG3;
254  } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG2)) {
255  return rad::LOG_LEVEL_DEBUG2;
256  } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG1)) {
257  return rad::LOG_LEVEL_DEBUG1;
258  } else if (level == LevelToString(rad::LOG_LEVEL_DEBUG)) {
259  return rad::LOG_LEVEL_DEBUG;
260  } else if (level == LevelToString(rad::LOG_LEVEL_INFO)) {
261  return rad::LOG_LEVEL_INFO;
262  } else if (level == LevelToString(rad::LOG_LEVEL_ACTION)) {
263  return rad::LOG_LEVEL_ACTION;
264  } else if (level == LevelToString(rad::LOG_LEVEL_GUARD)) {
265  return rad::LOG_LEVEL_GUARD;
266  } else if (level == LevelToString(rad::LOG_LEVEL_EVENT)) {
267  return rad::LOG_LEVEL_EVENT;
268  } else if (level == LevelToString(rad::LOG_LEVEL_STATE)) {
269  return rad::LOG_LEVEL_STATE;
270  } else if (level == LevelToString(rad::LOG_LEVEL_WARNING)) {
271  return rad::LOG_LEVEL_WARNING;
272  } else if (level == LevelToString(rad::LOG_LEVEL_ERROR)) {
273  return rad::LOG_LEVEL_ERROR;
274  }
275  return rad::LOG_LEVEL_UNKNOWN;
276 }
277 
278 inline void LogTraceFunction(const LogLevel level, const char* filename, const char* funcname,
279  const int linenum) {
281  Logger().Get(level) << filename << ":" << linenum << " " << funcname;
282  }
283 }
284 
285 inline void LogFunction(const LogLevel level, const char* funcname, const std::string& a) {
287  Logger().Get(level) << a << " (" << __FUNCTION__ << ")";
288  }
289 }
290 
291 inline void LogStateFunction(const std::string& a, const std::string& b) {
293  Logger().Get(LOG_LEVEL_STATE) << "from " << a << " to " << b;
294  }
295 }
296 
297 inline void LogEventFunction(const std::string& a) {
299  Logger().Get(LOG_LEVEL_EVENT) << a;
300  }
301 }
302 
303 } // namespace rad
304 
305 /*
306  * Logging definitions
307  */
308 #ifndef RAD_LOG_MAX_LEVEL
309 #define RAD_LOG_MAX_LEVEL rad::LOG_LEVEL_TRACE
310 #endif
311 
312 #define RAD_LOG_SETLEVEL(levelName) \
313  rad::Logger::ReportingLevel() = rad::Logger::LevelFromString(levelName)
314 
315 #define RAD_LOG_SETMODNAME(name) rad::Logger::ModuleName() = name
316 #define RAD_LOG_SETPROCNAME(name) rad::Logger::ProcName() = name
317 #define RAD_LOG_TO_LOGMON(is_enabled) rad::Logger::EnableLogMon() = is_enabled
318 #define RAD_LOG_TO_CONSOLE(is_enabled) rad::Logger::EnableConsole() = is_enabled
319 
320 #define RAD_LOG(level) \
321  if (level > RAD_LOG_MAX_LEVEL) \
322  ; \
323  else if (level > rad::Logger::ReportingLevel()) \
324  ; \
325  else if (rad::Logger::IsEnabled() == false) \
326  ; \
327  else \
328  rad::Logger().Get(level) << __FILE__ << ":" << __LINE__ << " "
329 
330 #define RAD_LOG_ERROR() \
331  if (rad::LOG_LEVEL_ERROR > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
332  ; \
333  else \
334  rad::Logger().Get(rad::LOG_LEVEL_ERROR) << __FILE__ << ":" << __LINE__ << " "
335 
336 #define RAD_LOG_WARNING() \
337  if (rad::LOG_LEVEL_WARNING > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
338  ; \
339  else \
340  rad::Logger().Get(rad::LOG_LEVEL_WARNING) << __FILE__ << ":" << __LINE__ << " "
341 
342 #define RAD_LOG_INFO() \
343  if (rad::LOG_LEVEL_INFO > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
344  ; \
345  else \
346  rad::Logger().Get(rad::LOG_LEVEL_INFO)
347 
348 #define RAD_LOG_DEBUG() \
349  if (rad::LOG_LEVEL_DEBUG > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
350  ; \
351  else \
352  rad::Logger().Get(rad::LOG_LEVEL_DEBUG) << __FILE__ << ":" << __LINE__ << " "
353 
354 #define RAD_LOG_DEBUG1() \
355  if (rad::LOG_LEVEL_DEBUG1 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
356  ; \
357  else \
358  rad::Logger().Get(rad::LOG_LEVEL_DEBUG1) << __FILE__ << ":" << __LINE__ << " "
359 
360 #define RAD_LOG_DEBUG2() \
361  if (rad::LOG_LEVEL_DEBUG2 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
362  ; \
363  else \
364  rad::Logger().Get(rad::LOG_LEVEL_DEBUG2) << __FILE__ << ":" << __LINE__ << " "
365 
366 #define RAD_LOG_DEBUG3() \
367  if (rad::LOG_LEVEL_DEBUG3 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
368  ; \
369  else \
370  rad::Logger().Get(rad::LOG_LEVEL_DEBUG3) << __FILE__ << ":" << __LINE__ << " "
371 
372 #define RAD_LOG_DEBUG4() \
373  if (rad::LOG_LEVEL_DEBUG4 > rad::Logger::ReportingLevel() || !rad::Logger::IsEnabled()) \
374  ; \
375  else \
376  rad::Logger().Get(rad::LOG_LEVEL_DEBUG4) << __FILE__ << ":" << __LINE__ << " "
377 
378 // (void)("LCOV_EXCL_BR_LINE") has been added to skip branch coverage the macros.
379 #define RAD_LOG_STATE(a, b) \
380  rad::LogStateFunction(a, b); \
381  (void)("LCOV_EXCL_BR_LINE")
382 #define RAD_LOG_EVENT(a) \
383  rad::LogEventFunction(a); \
384  (void)("LCOV_EXCL_BR_LINE")
385 #define RAD_LOG_GUARD(a) \
386  rad::LogFunction(rad::LOG_LEVEL_GUARD, __FUNCTION__, a); \
387  (void)("LCOV_EXCL_BR_LINE")
388 #define RAD_LOG_ACTION(a) \
389  rad::LogFunction(rad::LOG_LEVEL_ACTION, __FUNCTION__, a); \
390  (void)("LCOV_EXCL_BR_LINE")
391 #define RAD_LOG_TRACE() \
392  rad::LogTraceFunction(rad::LOG_LEVEL_TRACE, __FILE__, __FUNCTION__, __LINE__); \
393  (void)("LCOV_EXCL_BR_LINE")
394 //#endif
395 
396 #endif // RAD_LOGGER_HPP
hellorad.level
level
Definition: hellorad.py:30
rad::LOG_LEVEL_DEBUG1
@ LOG_LEVEL_DEBUG1
Definition: logger.hpp:161
rad::LOG_LEVEL_GUARD
@ LOG_LEVEL_GUARD
Definition: logger.hpp:157
rad::Logger
Definition: logger.hpp:175
rad::LogLevel
LogLevel
Definition: logger.hpp:151
rad::LOG_LEVEL_DEBUG2
@ LOG_LEVEL_DEBUG2
Definition: logger.hpp:162
rad::LogConfigure
void LogConfigure(const std::string &filename="")
Definition: logger.cpp:69
rad::GetSmLogger
log4cplus::Logger & GetSmLogger()
Definition: logger.cpp:49
rad::LogInitializer::~LogInitializer
~LogInitializer()
Definition: logger.hpp:115
rad::LogInitializer::Configure
void Configure(const std::string &filename="")
Definition: logger.hpp:121
rad::Logger::LevelFromString
static LogLevel LevelFromString(const std::string &level)
Definition: logger.hpp:247
rad::LOG_LEVEL_DEBUG
@ LOG_LEVEL_DEBUG
Definition: logger.hpp:160
rad::LogTraceFunction
void LogTraceFunction(const LogLevel level, const char *filename, const char *funcname, const int linenum)
Definition: logger.hpp:278
rad::LogInitializer::LogInitializer
LogInitializer()
Definition: logger.hpp:98
rad::LogFunction
void LogFunction(const LogLevel level, const char *funcname, const std::string &a)
Definition: logger.hpp:285
rad::Logger::ReportingLevel
static LogLevel & ReportingLevel()
Definition: logger.hpp:216
rad::LOG_LEVEL_TRACE
@ LOG_LEVEL_TRACE
Definition: logger.hpp:165
rad::LogInitializer::LogInitializer
LogInitializer(const LogInitializer &)=delete
rad::LOG_LEVEL_DEBUG3
@ LOG_LEVEL_DEBUG3
Definition: logger.hpp:163
rad::LOG_LEVEL_UNKNOWN
@ LOG_LEVEL_UNKNOWN
Definition: logger.hpp:152
rad::LOG_TIMESTAMP_MAXLEN
const int LOG_TIMESTAMP_MAXLEN
Definition: logger.hpp:170
rad::LOGGER_NAME
const std::string LOGGER_NAME
Definition: logger.hpp:79
rad::Logger::Get
std::ostringstream & Get(LogLevel level=LOG_LEVEL_INFO)
Definition: logger.hpp:211
rad::LOG_LEVEL_ACTION
@ LOG_LEVEL_ACTION
Definition: logger.hpp:158
rad::LOG_LEVEL_WARNING
@ LOG_LEVEL_WARNING
Definition: logger.hpp:154
rad::LOG_LEVEL_DEBUG4
@ LOG_LEVEL_DEBUG4
Definition: logger.hpp:164
rad::LogInitialize
void LogInitialize()
Definition: logger.cpp:54
rad::Logger::Logger
Logger()
Definition: logger.hpp:177
rad::LogInitializer
Definition: logger.hpp:96
rad::Logger::IsEnabled
static bool IsEnabled()
Definition: logger.hpp:231
rad
Definition: actionCallback.hpp:21
rad::Logger::LevelToString
static std::string LevelToString(LogLevel level)
Definition: logger.hpp:240
rad::GetLogger
log4cplus::Logger & GetLogger()
Definition: logger.cpp:44
rad::Logger::EnableConsole
static bool & EnableConsole()
Definition: logger.hpp:221
rad::LOG_MAX_LEVEL
const int LOG_MAX_LEVEL
Definition: logger.hpp:168
rad::LOG_LEVEL_ERROR
@ LOG_LEVEL_ERROR
Definition: logger.hpp:153
rad::Logger::ModuleName
static std::string & ModuleName()
Definition: logger.hpp:235
rad::Logger::EnableLogMon
static bool & EnableLogMon()
Definition: logger.hpp:226
rad::LOG_LEVEL_EVENT
@ LOG_LEVEL_EVENT
Definition: logger.hpp:156
rad::LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
Definition: logger.hpp:159
rad::Logger::m_output
std::ostringstream m_output
Definition: logger.hpp:191
rad::LOG_LEVEL_STATE
@ LOG_LEVEL_STATE
Definition: logger.hpp:155
rad::Logger::~Logger
virtual ~Logger()
Definition: logger.hpp:198
rad::SM_LOGGER_NAME
const std::string SM_LOGGER_NAME
Definition: logger.hpp:80
rad::LogEventFunction
void LogEventFunction(const std::string &a)
Definition: logger.hpp:297
rad::LogStateFunction
void LogStateFunction(const std::string &a, const std::string &b)
Definition: logger.hpp:291
rad::Logger::ProcName
static std::string & ProcName()
rad::LogInitializer::operator=
LogInitializer & operator=(const LogInitializer &)=delete
rad::LOG_MAX_LEN
const int LOG_MAX_LEN
Definition: logger.hpp:169