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