limal
Logger.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | _ _ _ _ __ _ |
4 | | | | | | \_/ | / \ | | |
5 | | | | | | |_| | / /\ \ | | |
6 | | |__ | | | | | | / ____ \ | |__ |
7 | |____||_| |_| |_|/ / \ \|____| |
8 | |
9 | core library |
10 | |
11 | (C) SUSE Linux Products GmbH |
12 \----------------------------------------------------------------------/
13 
14  File: Logger.hpp
15 
16  Author: Marius Tomaschewski
17  Maintainer: Marius Tomaschewski
18 
19 /-*/
26 #ifndef LIMAL_LOGGER_HPP
27 #define LIMAL_LOGGER_HPP
28 
29 #include <blocxx/Array.hpp>
30 #include <blocxx/String.hpp>
31 #include <blocxx/StringStream.hpp>
32 #include <blocxx/LogConfig.hpp>
33 #include <blocxx/Logger.hpp>
34 #include <blocxx/CommonFwd.hpp>
35 #include <blocxx/LogAppender.hpp>
36 #include <blocxx/AppenderLogger.hpp>
37 #include <limal/config.h>
38 
39 
55 #define LIMAL_LOG(logger, level, message) \
56 do \
57 { \
58  int err = errno; \
59  if( (logger).isEnabledFor(level)) \
60  { \
61  (logger).logMessage((level), (message), \
62  __FILE__, __LINE__, \
63  BLOCXX_LOGGER_PRETTY_FUNCTION); \
64  } \
65  errno = err; \
66 } while(0) // note the missing semicolon
67 
68 
84 #define LIMAL_SLOG(logger, level, message) \
85 do \
86 { \
87  int err = errno; \
88  if( (logger).isEnabledFor(level)) \
89  { \
90  blocxx::OStringStream _buf; \
91  _buf << message; \
92  (logger).logMessage((level), _buf.toString(), \
93  __FILE__, __LINE__, \
94  BLOCXX_LOGGER_PRETTY_FUNCTION); \
95  } \
96  errno = err; \
97 } while(0) // note the missing semicolon
98 
99 
114 #define LIMAL_LOG_FATAL(logger, logMessage) \
115  LIMAL_LOG(logger, blocxx::E_FATAL_ERROR_LEVEL, logMessage)
116 
117 #define LIMAL_SLOG_FATAL(logger, logMessage) \
118  LIMAL_SLOG(logger, blocxx::E_FATAL_ERROR_LEVEL, logMessage)
119 
120 
135 #define LIMAL_LOG_ERROR(logger, logMessage) \
136  LIMAL_LOG(logger, blocxx::E_ERROR_LEVEL, logMessage)
137 
138 #define LIMAL_SLOG_ERROR(logger, logMessage) \
139  LIMAL_SLOG(logger, blocxx::E_ERROR_LEVEL, logMessage)
140 
141 
156 #define LIMAL_LOG_INFO(logger, logMessage) \
157  LIMAL_LOG(logger, blocxx::E_INFO_LEVEL, logMessage)
158 
159 #define LIMAL_SLOG_INFO(logger, logMessage) \
160  LIMAL_SLOG(logger, blocxx::E_INFO_LEVEL, logMessage)
161 
162 
177 #define LIMAL_LOG_DEBUG(logger, logMessage) \
178  LIMAL_LOG(logger, blocxx::E_DEBUG_LEVEL, logMessage)
179 
180 #define LIMAL_SLOG_DEBUG(logger, logMessage) \
181  LIMAL_SLOG(logger, blocxx::E_DEBUG_LEVEL, logMessage)
182 
183 
184 namespace LIMAL_NAMESPACE
185 {
186 
275 class Logger
276 {
277 public:
288  typedef blocxx::ELogLevel ELogLevel;
289 
322  static blocxx::LoggerRef createCerrLogger(
323  const blocxx::String &component,
324  const blocxx::Array<blocxx::String> &components,
325  const blocxx::Array<blocxx::String> &categories,
326  const blocxx::String &messageFormat
327  );
328 
360  static blocxx::LoggerRef createSyslogLogger(
361  const blocxx::String &component,
362  const blocxx::Array<blocxx::String> &components,
363  const blocxx::Array<blocxx::String> &categories,
364  const blocxx::String &messageFormat,
365  const blocxx::String &identity,
366  const blocxx::String &facility
367  );
368 
369 
404  static blocxx::LoggerRef createFileLogger(
405  const blocxx::String &component,
406  const blocxx::Array<blocxx::String> &components,
407  const blocxx::Array<blocxx::String> &categories,
408  const blocxx::String &messageFormat,
409  const blocxx::String &filename,
410  blocxx::UInt64 maxLogFileSize = 0,
411  blocxx::UInt32 maxBackupIndex = 0
412  );
413 
443  static blocxx::LoggerRef createNullLogger(
444  const blocxx::String &component,
445  const blocxx::Array<blocxx::String> &components,
446  const blocxx::Array<blocxx::String> &categories,
447  const blocxx::String &messageFormat
448  );
449 
458  Logger(const blocxx::String &component = "");
459 
460 
464  ~Logger();
465 
466 
474  inline static bool
475  setDefaultLogger(const blocxx::LoggerRef &ref)
476  {
478  }
479 
489  inline static bool
490  setThreadLogger(const blocxx::LoggerRef &ref)
491  {
493  }
494 
499  inline static blocxx::LoggerRef getDefaultLogger()
500  {
502  }
503 
510  inline static blocxx::LoggerRef getCurrentLogger()
511  {
513  }
514 
526  void
527  logMessage(ELogLevel level,
528  const blocxx::String &message,
529  const char *filename = 0,
530  int fileline = -1,
531  const char *methodname = 0) const;
532 
533 
545  void
546  logMessage(const blocxx::String &category,
547  const blocxx::String &message,
548  const char *filename = 0,
549  int fileline = -1,
550  const char *methodname = 0) const;
551 
552 
559  bool
560  isEnabledFor(const ELogLevel level) const;
561 
562 
570  bool
571  isEnabledFor(const blocxx::String &category) const;
572 
573 
574 private:
575  static bool setDefaultFromLoggerRef(const blocxx::LoggerRef &ref);
576  static bool setThreadFromLoggerRef(const blocxx::LoggerRef &ref);
577  static blocxx::LoggerRef getDefaultAsLoggerRef();
578  static blocxx::LoggerRef getCurrentAsLoggerRef();
579 
580  blocxx::String m_component;
581 
582 };
583 
584 
585 } // LIMAL_NAMESPACE
586 
587 
588 #endif // LIMAL_LOGGER_HPP
589 /* vim: set ts=8 sts=8 sw=8 ai noet: */
590