OpenRTM-aist  2.1.0
SystemLogger.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef RTC_SYSTEMLOGGER_H
20 #define RTC_SYSTEMLOGGER_H
21 
22 #include <rtm/config_rtc.h>
23 
24 #include <coil/ClockManager.h>
25 #include <coil/Logger.h>
26 #include <mutex>
27 #include <coil/stringutil.h>
28 #include <coil/Properties.h>
29 
30 #include <string>
31 
32 namespace RTC
33 {
34  using LogStreamBuf = ::coil::LogStreamBuffer;
36 
99  class Logger
100  : public coil::LogStream
101  {
102  public:
103  enum
104  { // No: Write out messages include the following.
105  RTL_SILENT, // 0: ()
106  RTL_FATAL, // 1: (FATAL)
107  RTL_ERROR, // 2: (FATAL, ERROR)
108  RTL_WARN, // 3: (FATAL, ERROR, WARN)
109  RTL_INFO, // 4: (FATAL, ERROR, WARN, INFO)
110  RTL_DEBUG, // 5: (FATAL, ERROR, WARN, INFO, DEBUG)
111  RTL_TRACE, // 6: (FATAL, ERROR, WARN, INFO, DEBUG, TRACE)
112  RTL_VERBOSE, // 7: (FATAL, ERROR, WARN, INFO, DEBUG, TRACE, VERBOSE)
113  RTL_PARANOID // 8: (FATAL, ERROR, WARN, INFO,
114  // DEBUG, TRACE, VERBOSE, PARA)
115  };
116 
135  explicit Logger(const char* name = "");
154  explicit Logger(LogStreamBuf* streambuf);
166  ~Logger() override;
167 
183  bool setLevel(const char* level);
184 
260  void setDateFormat(const char* format);
261 
300  void setClockType(const std::string& clocktype);
301 
321  void setName(const char* name);
322 
323 
346  void write(int level, const std::string &mes) override;
347 
370  void write(int level, const coil::Properties &prop);
371 
394  static std::string getLevelString(int level)
395  {
396  return m_levelString[level];
397  }
398 
421  static std::string getLevelOutputString(int level)
422  {
423  return m_levelOutputString[level];
424  }
425 
426 
449  static std::string getLevelColor(int level)
450  {
451  return m_levelColor[level];
452  }
453 
454 
455  protected:
456 
457 
473  std::string getDate();
474 
492  static int strToLevel(const char* level);
493 
494 
495 
496  private:
497  std::string m_name = "unknown";
498  std::string m_dateFormat = "%b %d %H:%M:%S.%Q";
499  coil::IClock* m_clock{&coil::ClockManager::instance().getClock("system")};
500 
501 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
502 #ifdef LIBRARY_EXPORTS
503  static __declspec(dllexport) const char* const m_levelString[];
504  static __declspec(dllexport) const char* const m_levelOutputString[];
505  static __declspec(dllexport) const char* const m_levelColor[];
506 #else
507  static __declspec(dllimport) const char* const m_levelString[];
508  static __declspec(dllimport) const char* const m_levelOutputString[];
509  static __declspec(dllimport) const char* const m_levelColor[];
510 #endif
511 #else
512  static const char* const m_levelString[];
513  static const char* const m_levelOutputString[];
514  static const char* const m_levelColor[];
515 #endif
516  bool m_msEnable{false};
517  bool m_usEnable{false};
518  };
519 
520 
521 #ifndef NO_LOGGING
537 #define RTC_LOG(LV, fmt) \
538  do{ \
539  if (rtclog.isValid(LV)) \
540  { \
541  std::string str = ::coil::sprintf fmt; \
542  rtclog.lock(); \
543  rtclog.write(LV, str); \
544  rtclog.unlock(); \
545  } \
546  } while(0)
547 
548 #define RTC_LOG_STR(LV, str) \
549  do { \
550  if (rtclog.isValid(LV)) \
551  { \
552  rtclog.lock(); \
553  rtclog.write(LV, str); \
554  rtclog.unlock(); \
555  } \
556  } while(0)
557 
577 #define RTC_FATAL(fmt) RTC_LOG(::RTC::Logger::RTL_FATAL, fmt)
578 #define RTC_FATAL_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_FATAL, str)
579 
599 #define RTC_ERROR(fmt) RTC_LOG(::RTC::Logger::RTL_ERROR, fmt)
600 #define RTC_ERROR_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_ERROR, str)
601 
621 #define RTC_WARN(fmt) RTC_LOG(::RTC::Logger::RTL_WARN, fmt)
622 #define RTC_WARN_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_WARN, str)
623 
643 #define RTC_INFO(fmt) RTC_LOG(::RTC::Logger::RTL_INFO, fmt)
644 #define RTC_INFO_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_INFO, str)
645 
665 #define RTC_DEBUG(fmt) RTC_LOG(::RTC::Logger::RTL_DEBUG, fmt)
666 #define RTC_DEBUG_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_DEBUG, str)
667 
687 #define RTC_TRACE(fmt) RTC_LOG(::RTC::Logger::RTL_TRACE, fmt)
688 #define RTC_TRACE_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_TRACE, str)
689 
690 
710 #define RTC_VERBOSE(fmt) RTC_LOG(::RTC::Logger::RTL_VERBOSE, fmt)
711 #define RTC_VERBOSE_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_VERBOSE, str)
712 
732 #define RTC_PARANOID(fmt) RTC_LOG(::RTC::Logger::RTL_PARANOID, fmt)
733 #define RTC_PARANOID_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_PARANOID, str)
734 
735 #else
736 #define RTC_FATAL(fmt)
737 #define RTC_FATAL_STR(str)
738 #define RTC_ERROR(fmt)
739 #define RTC_ERROR_STR(str)
740 #define RTC_WARN(fmt)
741 #define RTC_WARN_STR(str)
742 #define RTC_INFO(fmt)
743 #define RTC_INFO_STR(str)
744 #define RTC_DEBUG(fmt)
745 #define RTC_DEBUG_STR(str)
746 #define RTC_TRACE(fmt)
747 #define RTC_TRACE_STR(str)
748 #define RTC_VERBOSE(fmt)
749 #define RTC_VERBOSE_STR(str)
750 #define RTC_PARANOID(fmt)
751 #define RTC_PARANOID_STR(str)
752 #define RTC_FATAL(fmt)
753 #define RTC_FATAL_STR(str)
754 #endif
755 
756 } // namespace RTC
757 
758 #endif // RTC_SYSTEMLOGGER_H
Logger class.
Definition: SystemLogger.h:101
static std::string getLevelColor(int level)
Definition: SystemLogger.h:449
Logger(LogStreamBuf *streambuf)
Constructor.
static int strToLevel(const char *level)
Set the log level Set the log level corresponding to the given string.
@ RTL_INFO
Definition: SystemLogger.h:109
@ RTL_DEBUG
Definition: SystemLogger.h:110
@ RTL_SILENT
Definition: SystemLogger.h:105
@ RTL_WARN
Definition: SystemLogger.h:108
@ RTL_FATAL
Definition: SystemLogger.h:106
@ RTL_VERBOSE
Definition: SystemLogger.h:112
@ RTL_ERROR
Definition: SystemLogger.h:107
@ RTL_PARANOID
Definition: SystemLogger.h:113
@ RTL_TRACE
Definition: SystemLogger.h:111
void setName(const char *name)
Set suffix of date/time string of header.
void write(int level, const std::string &mes) override
log output
void setDateFormat(const char *format)
Set date/time format for adding the header.
void write(int level, const coil::Properties &prop)
log output
std::string getDate()
Get the current formatted date/time string Get the current datetime described by specified format.
void setClockType(const std::string &clocktype)
Specifying clock type to be used for logging.
static std::string getLevelString(int level)
Definition: SystemLogger.h:394
~Logger() override
Virtual destructor.
Logger(const char *name="")
Constructor.
bool setLevel(const char *level)
Set log level by string.
static std::string getLevelOutputString(int level)
Definition: SystemLogger.h:421
RT-Component.
::coil::LogStream LogStream
Definition: SystemLogger.h:35
coil::Properties Properties
Definition: RTC.h:72
::coil::LogStreamBuffer LogStreamBuf
Definition: SystemLogger.h:34