Scarab  v2.4.8
Project 8 C++ Utility Library
logger.hh
Go to the documentation of this file.
1 /*
2  * logger.hh
3  * Based on KLogger.h, from KATRIN's Kasper
4  *
5  * Created on: Jan 21, 2014
6  * Author: nsoblath
7  */
8 
9 #ifndef SCARAB_LOGGER_HH_
10 #define SCARAB_LOGGER_HH_
11 
12 #include "macros.hh"
13 #include "scarab_api.hh"
14 
23 // UTILITY MACROS
24 
25 #ifndef LOGGER_UTILITY_MACROS_
26 #define LOGGER_UTILITY_MACROS_
27 
28 #define msvc_bug_fix(m, args) m args
29 #define expand(x) x
30 
31 //#define va_num_args(...) va_num_args_(__VA_ARGS__, 5,4,3,2,1)
32 //#define va_num_args_(...) msvc_bug_fix(va_num_args_impl, (__VA_ARGS__))
33 #define va_num_args(...) expand( va_num_args_impl(__VA_ARGS__, 5,4,3,2,1))
34 #define va_num_args_impl(_1,_2,_3,_4,_5,N,...) N
35 
36 
37 #define macro_dispatcher(func, ...) macro_dispatcher_(func, va_num_args(__VA_ARGS__))
38 #define macro_dispatcher_(func, nargs) macro_dispatcher__(func, nargs)
39 #define macro_dispatcher__(func, nargs) func ## nargs
40 
41 #endif /* LOGGER_UTILITY_MACROS_ */
42 
43 // COLOR DEFINITIONS
44 #define COLOR_NORMAL "0"
45 #define COLOR_BRIGHT "1"
46 #define COLOR_FOREGROUND_RED "31"
47 #define COLOR_FOREGROUND_GREEN "32"
48 #define COLOR_FOREGROUND_YELLOW "33"
49 #define COLOR_FOREGROUND_BLUE "34"
50 #define COLOR_FOREGROUND_CYAN "36"
51 #define COLOR_FOREGROUND_WHITE "37"
52 #define COLOR_PREFIX "\033["
53 #define COLOR_SUFFIX "m"
54 #define COLOR_SEPARATOR ";"
55 
56 // INCLUDES
57 
58 #include <cstring>
59 #include <iostream>
60 #include <sstream>
61 
62 // CLASS DEFINITIONS
63 
67 namespace scarab
68 {
69 
113  {
114  public:
115  enum ELevel {
116  eTrace = 0,
117  eDebug = 1,
118  eInfo = 2,
119  eProg = 3,
120  eWarn = 4,
121  eError = 5,
122  eFatal = 6
123  };
124 
125  public:
130  struct Location {
131  Location(const char* const fileName = "", const char* const functionName = "", int lineNumber = -1) :
132  fLineNumber(lineNumber), fFileName(fileName), fFunctionName(functionName)
133  { }
135  std::string fFileName;
136  std::string fFunctionName;
137  };
138 
139  public:
140  static logger& GetRootLogger() {
141  static logger rootLogger;
142  return rootLogger;
143  }
144 
145  public:
150  logger(const char* name = 0);
152  logger(const std::string& name);
153 
154  virtual ~logger();
155 
161  bool IsLevelEnabled(ELevel level) const;
162 
167  void SetLevel(ELevel level) const;
168 
173  void SetGlobalLevel(ELevel level) const;
174 
179  static void SetColored(bool flag);
180 
185  static void SetOutStream(std::ostream* stream);
186 
191  static void SetErrStream(std::ostream* stream);
192 
200  void Log(ELevel level, const std::string& message, const Location& loc = Location());
201 
208  void LogTrace(const std::string& message, const Location& loc = Location())
209  {
210  Log(eTrace, message, loc);
211  }
218  void LogDebug(const std::string& message, const Location& loc = Location())
219  {
220  Log(eDebug, message, loc);
221  }
228  void LogInfo(const std::string& message, const Location& loc = Location())
229  {
230  Log(eInfo, message, loc);
231  }
238  void LogProg(const std::string& message, const Location& loc = Location())
239  {
240  Log(eProg, message, loc);
241  }
248  void LogWarn(const std::string& message, const Location& loc = Location())
249  {
250  Log(eWarn, message, loc);
251  }
258  void LogError(const std::string& message, const Location& loc = Location())
259  {
260  Log(eError, message, loc);
261  }
268  void LogFatal(const std::string& message, const Location& loc = Location())
269  {
270  Log(eFatal, message, loc);
271  }
272 
273  private:
274  struct Private;
275  Private* fPrivate;
276  };
277 
278 }
279 
280 // PRIVATE MACROS
281 
282 #define __DEFAULT_LOGGER scarab::logger::GetRootLogger()
283 
284 #define __LOG_LOCATION scarab::logger::Location(__FILE__, __FUNC__, __LINE__)
285 #define __LOG_LOG_4(I,L,M,O) \
286  { \
287  if (I.IsLevelEnabled(scarab::logger::e##L)) { \
288  static bool _sLoggerMarker = false; \
289  if (!O || !_sLoggerMarker) { \
290  _sLoggerMarker = true; \
291  std::ostringstream stream; stream << M; \
292  I.Log(scarab::logger::e##L, stream.str(), __LOG_LOCATION); \
293  } \
294  } \
295  }
296 
297 
298 #define __LOG_LOG_3(I,L,M) __LOG_LOG_4(I,L,M,false)
299 #define __LOG_LOG_2(L,M) __LOG_LOG_4(__DEFAULT_LOGGER,L,M,false)
300 #define __LOG_LOG_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Debug,M,false)
301 
302 #define __LOG_TRACE_2(I,M) __LOG_LOG_4(I,Trace,M,false)
303 #define __LOG_TRACE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Trace,M,false)
304 
305 #define __LOG_DEBUG_2(I,M) __LOG_LOG_4(I,Debug,M,false)
306 #define __LOG_DEBUG_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Debug,M,false)
307 
308 #define __LOG_INFO_2(I,M) __LOG_LOG_4(I,Info,M,false)
309 #define __LOG_INFO_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Info,M,false)
310 
311 #define __LOG_PROG_2(I,M) __LOG_LOG_4(I,Prog,M,false)
312 #define __LOG_PROG_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Prog,M,false)
313 
314 #define __LOG_WARN_2(I,M) __LOG_LOG_4(I,Warn,M,false)
315 #define __LOG_WARN_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Warn,M,false)
316 
317 #define __LOG_ERROR_2(I,M) __LOG_LOG_4(I,Error,M,false)
318 #define __LOG_ERROR_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Error,M,false)
319 
320 #define __LOG_FATAL_2(I,M) __LOG_LOG_4(I,Fatal,M,false)
321 #define __LOG_FATAL_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Fatal,M,false)
322 
323 #define __LOG_ASSERT_3(I,C,M) if (!(C)) { __LOG_ERROR_2(I,M) }
324 #define __LOG_ASSERT_2(C,M) __LOG_ASSERT_3(__DEFAULT_LOGGER,C,M)
325 
326 
327 #define __LOG_LOG_ONCE_3(I,L,M) __LOG_LOG_4(I,L,M,true)
328 #define __LOG_LOG_ONCE_2(L,M) __LOG_LOG_4(__DEFAULT_LOGGER,L,M,true)
329 #define __LOG_LOG_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Debug,M,true)
330 
331 #define __LOG_TRACE_ONCE_2(I,M) __LOG_LOG_4(I,Trace,M,true)
332 #define __LOG_TRACE_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Trace,M,true)
333 
334 #define __LOG_DEBUG_ONCE_2(I,M) __LOG_LOG_4(I,Debug,M,true)
335 #define __LOG_DEBUG_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Debug,M,true)
336 
337 #define __LOG_INFO_ONCE_2(I,M) __LOG_LOG_4(I,Info,M,true)
338 #define __LOG_INFO_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Info,M,true)
339 
340 #define __LOG_PROG_ONCE_2(I,M) __LOG_LOG_4(I,Prog,M,true)
341 #define __LOG_PROG_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Prog,M,true)
342 
343 #define __LOG_WARN_ONCE_2(I,M) __LOG_LOG_4(I,Warn,M,true)
344 #define __LOG_WARN_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Warn,M,true)
345 
346 #define __LOG_ERROR_ONCE_2(I,M) __LOG_LOG_4(I,Error,M,true)
347 #define __LOG_ERROR_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Error,M,true)
348 
349 #define __LOG_FATAL_ONCE_2(I,M) __LOG_LOG_4(I,Fatal,M,true)
350 #define __LOG_FATAL_ONCE_1(M) __LOG_LOG_4(__DEFAULT_LOGGER,Fatal,M,true)
351 
352 
353 // PUBLIC MACROS
354 
355 #define LOGGER(I,K) static scarab::logger I(K);
356 
357 #ifndef _WIN32
358 
359 #define LOG(...) macro_dispatcher(__LOG_LOG_, __VA_ARGS__)(__VA_ARGS__)
360 #ifdef NDEBUG
361 #define LTRACE(...)
362 #define LDEBUG(...)
363 #else
364 #define LTRACE(...) macro_dispatcher(__LOG_TRACE_, __VA_ARGS__)(__VA_ARGS__)
365 #define LDEBUG(...) macro_dispatcher(__LOG_DEBUG_, __VA_ARGS__)(__VA_ARGS__)
366 #endif
367 #define LINFO(...) macro_dispatcher(__LOG_INFO_, __VA_ARGS__)(__VA_ARGS__)
368 #define LPROG(...) macro_dispatcher(__LOG_PROG_, __VA_ARGS__)(__VA_ARGS__)
369 #define LWARN(...) macro_dispatcher(__LOG_WARN_, __VA_ARGS__)(__VA_ARGS__)
370 #define LERROR(...) macro_dispatcher(__LOG_ERROR_, __VA_ARGS__)(__VA_ARGS__)
371 #define LFATAL(...) macro_dispatcher(__LOG_FATAL_, __VA_ARGS__)(__VA_ARGS__)
372 #define LASSERT(...) macro_dispatcher(__LOG_ASSERT_, __VA_ARGS__)(__VA_ARGS__)
373 
374 #define LOG_ONCE(...) macro_dispatcher(__LOG_LOG_ONCE_, __VA_ARGS__)(__VA_ARGS__)
375 #ifdef NDEBUG
376 #define LTRACE_ONCE(...)
377 #define LDEBUG_ONCE(...)
378 #else
379 #define LTRACE_ONCE(...) macro_dispatcher(__LOG_TRACE_ONCE_, __VA_ARGS__)(__VA_ARGS__)
380 #define LDEBUG_ONCE(...) macro_dispatcher(__LOG_DEBUG_ONCE_, __VA_ARGS__)(__VA_ARGS__)
381 #endif
382 #define LINFO_ONCE(...) macro_dispatcher(__LOG_INFO_ONCE_, __VA_ARGS__)(__VA_ARGS__)
383 #define LPROG_ONCE(...) macro_dispatcher(__LOG_PROG_ONCE_, __VA_ARGS__)(__VA_ARGS__)
384 #define LWARN_ONCE(...) macro_dispatcher(__LOG_WARN_ONCE_, __VA_ARGS__)(__VA_ARGS__)
385 #define LERROR_ONCE(...) macro_dispatcher(__LOG_ERROR_ONCE_, __VA_ARGS__)(__VA_ARGS__)
386 #define LFATAL_ONCE(...) macro_dispatcher(__LOG_FATAL_ONCE_, __VA_ARGS__)(__VA_ARGS__)
387 
388 #else /*_WIN32*/
389 
390 #define LOG(I, ...) __LOG_LOG_2(I, __VA_ARGS__)
391 #ifdef NDEBUG
392 #define LTRACE(I, ...)
393 #define LDEBUG(I, ...)
394 #else
395 #define LTRACE(I, ...) __LOG_TRACE_2(I, __VA_ARGS__)
396 #define LDEBUG(I, ...) __LOG_DEBUG_2(I, __VA_ARGS__)
397 #endif
398 #define LINFO(I, ...) __LOG_INFO_2(I, __VA_ARGS__)
399 #define LPROG(I, ...) __LOG_PROG_2(I, __VA_ARGS__)
400 #define LWARN(I, ...) __LOG_WARN_2(I, __VA_ARGS__)
401 #define LERROR(I, ...) __LOG_ERROR_2(I, __VA_ARGS__)
402 #define LFATAL(I, ...) __LOG_FATAL_2(I, __VA_ARGS__)
403 #define LASSERT(I, ...) __LOG_ASSERT_2(I, __VA_ARGS__)
404 
405 #define LOG_ONCE(I, ...) __LOG_LOG_ONCE_2(I, __VA_ARGS__)
406 #ifdef NDEBUG
407 #define LTRACE_ONCE(I, ...)
408 #define LDEBUG_ONCE(I, ...)
409 #else
410 #define LTRACE_ONCE(I, ...) __LOG_TRACE_ONCE_2(I, __VA_ARGS__)
411 #define LDEBUG_ONCE(I, ...) __LOG_DEBUG_ONCE_2(I, __VA_ARGS__)
412 #endif
413 #define LINFO_ONCE(I, ...) __LOG_INFO_ONCE_2(I, __VA_ARGS__)
414 #define LPROG_ONCE(I, ...) __LOG_PROG_ONCE_2(I, __VA_ARGS__)
415 #define LWARN_ONCE(I, ...) __LOG_WARN_ONCE_2(I, __VA_ARGS__)
416 #define LERROR_ONCE(I, ...) __LOG_ERROR_ONCE_2(I, __VA_ARGS__)
417 #define LFATAL_ONCE(I, ...) __LOG_FATAL_ONCE_2(I, __VA_ARGS__)
418 
419 #endif /*_WIN32*/
420 
421 #endif /* SCARAB_LOGGER_HH_ */
std::string fFileName
Definition: logger.hh:135
std::string fFunctionName
Definition: logger.hh:136
static logger & GetRootLogger()
Definition: logger.hh:140
void LogWarn(const std::string &message, const Location &loc=Location())
Definition: logger.hh:248
void LogInfo(const std::string &message, const Location &loc=Location())
Definition: logger.hh:228
#define SCARAB_API
Definition: scarab_api.hh:24
void LogProg(const std::string &message, const Location &loc=Location())
Definition: logger.hh:238
void LogDebug(const std::string &message, const Location &loc=Location())
Definition: logger.hh:218
void LogTrace(const std::string &message, const Location &loc=Location())
Definition: logger.hh:208
Location(const char *const fileName="", const char *const functionName="", int lineNumber=-1)
Definition: logger.hh:131
Private * fPrivate
Definition: logger.hh:274
void LogFatal(const std::string &message, const Location &loc=Location())
Definition: logger.hh:268
void LogError(const std::string &message, const Location &loc=Location())
Definition: logger.hh:258