Nymph  v1.5.2
Flow-Based Data Processing Framework
KTLogger.hh
Go to the documentation of this file.
1 /*
2  * KTLogger.hh
3  * Based on KLogger.h, from KATRIN's Kasper
4  *
5  * Created on: Jan 23, 2014
6  * Author: nsoblath
7  */
8 
9 #ifndef KTLOGGER_HH_
10 #define KTLOGGER_HH_
11 
20 // UTILITY MACROS
21 
22 #ifndef LOGGER_UTILITY_MACROS_
23 #define LOGGER_UTILITY_MACROS_
24 
25 #define STRINGIFY(x) #x
26 #define TOSTRING(x) STRINGIFY(x)
27 #define __FILE_LINE__ __FILE__ "(" TOSTRING(__LINE__) ")"
28 #define __FILENAME_LINE__ (strrchr(__FILE__, '/') ? strrchr(__FILE_LINE__, '/') + 1 : __FILE_LINE__)
29 
30 #if defined(_MSC_VER)
31 #if _MSC_VER >= 1300
32 #define __FUNC__ __FUNCSIG__
33 #endif
34 #else
35 #if defined(__GNUC__)
36 #define __FUNC__ __PRETTY_FUNCTION__
37 #endif
38 #endif
39 #if !defined(__FUNC__)
40 #define __FUNC__ ""
41 #endif
42 
43 #define va_num_args(...) va_num_args_impl(__VA_ARGS__, 5,4,3,2,1)
44 #define va_num_args_impl(_1,_2,_3,_4,_5,N,...) N
45 
46 #define macro_dispatcher(func, ...) macro_dispatcher_(func, va_num_args(__VA_ARGS__))
47 #define macro_dispatcher_(func, nargs) macro_dispatcher__(func, nargs)
48 #define macro_dispatcher__(func, nargs) func ## nargs
49 
50 #endif /* LOGGER_UTILITY_MACROS_ */
51 
52 // COLOR DEFINITIONS
53 #define KTCOLOR_NORMAL "0"
54 #define KTCOLOR_BRIGHT "1"
55 #define KTCOLOR_FOREGROUND_RED "31"
56 #define KTCOLOR_FOREGROUND_GREEN "32"
57 #define KTCOLOR_FOREGROUND_YELLOW "33"
58 #define KTCOLOR_FOREGROUND_BLUE "34"
59 #define KTCOLOR_FOREGROUND_CYAN "36"
60 #define KTCOLOR_FOREGROUND_WHITE "37"
61 #define KTCOLOR_PREFIX "\033["
62 #define KTCOLOR_SUFFIX "m"
63 #define KTCOLOR_SEPARATOR ";"
64 
65 // INCLUDES
66 
67 #include <string>
68 #include <iostream>
69 #include <sstream>
70 
71 // CLASS DEFINITIONS
72 
76 namespace Nymph
77 {
78 
121  class KTLogger
122  {
123  public:
124  enum ELevel {
126  };
127 
128  public:
133  struct Location {
134  Location(const char* const fileName = "", const char* const functionName = "", int lineNumber = -1) :
135  fLineNumber(lineNumber), fFileName(fileName), fFunctionName(functionName)
136  { }
138  std::string fFileName;
139  std::string fFunctionName;
140  };
141 
142  public:
144  static KTLogger rootLogger;
145  return rootLogger;
146  }
147 
148  public:
153  KTLogger(const char* name = 0);
155  KTLogger(const std::string& name);
156 
157  virtual ~KTLogger();
158 
164  bool IsLevelEnabled(ELevel level) const;
165 
170  void SetLevel(ELevel level) const;
171 
179  void Log(ELevel level, const std::string& message, const Location& loc = Location());
180 
187  void LogTrace(const std::string& message, const Location& loc = Location())
188  {
189  Log(eTrace, message, loc);
190  }
197  void LogDebug(const std::string& message, const Location& loc = Location())
198  {
199  Log(eDebug, message, loc);
200  }
207  void LogInfo(const std::string& message, const Location& loc = Location())
208  {
209  Log(eInfo, message, loc);
210  }
217  void LogProg(const std::string& message, const Location& loc = Location())
218  {
219  Log(eProg, message, loc);
220  }
227  void LogWarn(const std::string& message, const Location& loc = Location())
228  {
229  Log(eWarn, message, loc);
230  }
237  void LogError(const std::string& message, const Location& loc = Location())
238  {
239  Log(eError, message, loc);
240  }
247  void LogFatal(const std::string& message, const Location& loc = Location())
248  {
249  Log(eFatal, message, loc);
250  }
251 
252  private:
253  struct Private;
254  Private* fPrivate;
255  };
256 
257 }
258 
259 // PRIVATE MACROS
260 
261 #define __KTDEFAULT_LOGGER ::Nymph::KTLogger::GetRootLogger()
262 
263 #define __KTLOG_LOCATION ::Nymph::KTLogger::Location(__FILE__, __FUNC__, __LINE__)
264 
265 #define __KTLOG_LOG_4(I,L,M,O) \
266  { \
267  if (I.IsLevelEnabled(::Nymph::KTLogger::e##L)) { \
268  static bool _sLoggerMarker = false; \
269  if (!O || !_sLoggerMarker) { \
270  _sLoggerMarker = true; \
271  ::std::ostringstream stream; stream << M; \
272  I.Log(::Nymph::KTLogger::e##L, stream.str(), __KTLOG_LOCATION); \
273  } \
274  } \
275  }
276 
277 #define __KTLOG_LOG_3(I,L,M) __KTLOG_LOG_4(I,L,M,false)
278 #define __KTLOG_LOG_2(L,M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,L,M,false)
279 #define __KTLOG_LOG_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Debug,M,false)
280 
281 #define __KTLOG_TRACE_2(I,M) __KTLOG_LOG_4(I,Trace,M,false)
282 #define __KTLOG_TRACE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Trace,M,false)
283 
284 #ifndef NDEBUG
285 #define __KTLOG_DEBUG_2(I,M) __KTLOG_LOG_4(I,Debug,M,false)
286 #define __KTLOG_DEBUG_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Debug,M,false)
287 #else
288 #define __KTLOG_DEBUG_2(I,M) __KTLOG_LOG_4(I,Debug,"",false)
289 #define __KTLOG_DEBUG_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Debug,"",false)
290 #endif
291 
292 #define __KTLOG_INFO_2(I,M) __KTLOG_LOG_4(I,Info,M,false)
293 #define __KTLOG_INFO_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Info,M,false)
294 
295 #define __KTLOG_PROG_2(I,M) __KTLOG_LOG_4(I,Prog,M,false)
296 #define __KTLOG_PROG_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Prog,M,false)
297 
298 #define __KTLOG_WARN_2(I,M) __KTLOG_LOG_4(I,Warn,M,false)
299 #define __KTLOG_WARN_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Warn,M,false)
300 
301 #define __KTLOG_ERROR_2(I,M) __KTLOG_LOG_4(I,Error,M,false)
302 #define __KTLOG_ERROR_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Error,M,false)
303 
304 #define __KTLOG_FATAL_2(I,M) __KTLOG_LOG_4(I,Fatal,M,false)
305 #define __KTLOG_FATAL_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Fatal,M,false)
306 
307 #define __KTLOG_ASSERT_3(I,C,M) if (!(C)) { __MTLOG_ERROR_2(I,M) }
308 #define __KTLOG_ASSERT_2(C,M) __KTLOG_ASSERT_3(__KTDEFAULT_LOGGER,C,M)
309 
310 
311 #define __KTLOG_LOG_ONCE_3(I,L,M) __KTLOG_LOG_4(I,L,M,true)
312 #define __KTLOG_LOG_ONCE_2(L,M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,L,M,true)
313 #define __KTLOG_LOG_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Debug,M,true)
314 
315 #define __KTLOG_TRACE_ONCE_2(I,M) __KTLOG_LOG_4(I,Trace,M,true)
316 #define __KTLOG_TRACE_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Trace,M,true)
317 
318 #define __KTLOG_DEBUG_ONCE_2(I,M) __KTLOG_LOG_4(I,Debug,M,true)
319 #define __KTLOG_DEBUG_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Debug,M,true)
320 
321 #define __KTLOG_INFO_ONCE_2(I,M) __KTLOG_LOG_4(I,Info,M,true)
322 #define __KTLOG_INFO_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Info,M,true)
323 
324 #define __KTLOG_PROG_ONCE_2(I,M) __KTLOG_LOG_4(I,Prog,M,true)
325 #define __KTLOG_PROG_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Prog,M,true)
326 
327 #define __KTLOG_WARN_ONCE_2(I,M) __KTLOG_LOG_4(I,Warn,M,true)
328 #define __KTLOG_WARN_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Warn,M,true)
329 
330 #define __KTLOG_ERROR_ONCE_2(I,M) __KTLOG_LOG_4(I,Error,M,true)
331 #define __KTLOG_ERROR_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Error,M,true)
332 
333 #define __KTLOG_FATAL_ONCE_2(I,M) __KTLOG_LOG_4(I,Fatal,M,true)
334 #define __KTLOG_FATAL_ONCE_1(M) __KTLOG_LOG_4(__KTDEFAULT_LOGGER,Fatal,M,true)
335 
336 
337 // PUBLIC MACROS
338 
339 #define KTLOGGER(I,K) static ::Nymph::KTLogger I(K);
340 
341 #define KTLOG(...) macro_dispatcher(__KTLOG_LOG_, __VA_ARGS__)(__VA_ARGS__)
342 #define KTTRACE(...) macro_dispatcher(__KTLOG_TRACE_, __VA_ARGS__)(__VA_ARGS__)
343 #define KTDEBUG(...) macro_dispatcher(__KTLOG_DEBUG_, __VA_ARGS__)(__VA_ARGS__)
344 #define KTINFO(...) macro_dispatcher(__KTLOG_INFO_, __VA_ARGS__)(__VA_ARGS__)
345 #define KTPROG(...) macro_dispatcher(__KTLOG_PROG_, __VA_ARGS__)(__VA_ARGS__)
346 #define KTWARN(...) macro_dispatcher(__KTLOG_WARN_, __VA_ARGS__)(__VA_ARGS__)
347 #define KTERROR(...) macro_dispatcher(__KTLOG_ERROR_, __VA_ARGS__)(__VA_ARGS__)
348 #define KTFATAL(...) macro_dispatcher(__KTLOG_FATAL_, __VA_ARGS__)(__VA_ARGS__)
349 #define KTASSERT(...) macro_dispatcher(__KTLOG_ASSERT_, __VA_ARGS__)(__VA_ARGS__)
350 
351 #define KTLOG_ONCE(...) macro_dispatcher(__KTLOG_LOG_ONCE_, __VA_ARGS__)(__VA_ARGS__)
352 #define KTTRACE_ONCE(...) macro_dispatcher(__KTLOG_TRACE_ONCE_, __VA_ARGS__)(__VA_ARGS__)
353 #define KTDEBUG_ONCE(...) macro_dispatcher(__KTLOG_DEBUG_ONCE_, __VA_ARGS__)(__VA_ARGS__)
354 #define KTINFO_ONCE(...) macro_dispatcher(__KTLOG_INFO_ONCE_, __VA_ARGS__)(__VA_ARGS__)
355 #define KTPROG_ONCE(...) macro_dispatcher(__KTLOG_PROG_ONCE_, __VA_ARGS__)(__VA_ARGS__)
356 #define KTWARN_ONCE(...) macro_dispatcher(__KTLOG_WARN_ONCE_, __VA_ARGS__)(__VA_ARGS__)
357 #define KTERROR_ONCE(...) macro_dispatcher(__KTLOG_ERROR_ONCE_, __VA_ARGS__)(__VA_ARGS__)
358 #define KTFATAL_ONCE(...) macro_dispatcher(__KTLOG_FATAL_ONCE_, __VA_ARGS__)(__VA_ARGS__)
359 
360 #endif /* KTLOGGER_HH_ */
void LogFatal(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:247
virtual ~KTLogger()
Definition: KTLogger.cc:162
bool IsLevelEnabled(ELevel level) const
Definition: KTLogger.cc:167
KTLogger(const char *name=0)
Definition: KTLogger.cc:138
void SetLevel(ELevel level) const
Definition: KTLogger.cc:172
std::string fFunctionName
Definition: KTLogger.hh:139
void LogDebug(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:197
static KTLogger & GetRootLogger()
Definition: KTLogger.hh:143
void Log(ELevel level, const std::string &message, const Location &loc=Location())
Definition: KTLogger.cc:183
Location(const char *const fileName="", const char *const functionName="", int lineNumber=-1)
Definition: KTLogger.hh:134
void LogInfo(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:207
void LogWarn(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:227
Private * fPrivate
Definition: KTLogger.hh:253
void LogError(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:237
void LogTrace(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:187
void LogProg(const std::string &message, const Location &loc=Location())
Definition: KTLogger.hh:217