OpenRTM-aist  1.2.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 // COIL
25 #include <coil/Time.h>
26 #include <coil/ClockManager.h>
27 #include <coil/Logger.h>
28 #include <coil/Mutex.h>
29 #include <coil/Guard.h>
30 #include <coil/stringutil.h>
31 
32 namespace RTC
33 {
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, DEBUG, TRACE, VERBOSE, PARA)
114  };
115 
134  Logger(const char* name = "");
153  Logger(LogStreamBuf* streambuf);
165  virtual ~Logger(void);
166 
182  bool setLevel(const char* level);
183 
259  void setDateFormat(const char* format);
260 
299  void setClockType(std::string clocktype);
300 
320  void setName(const char* name);
321 
322  protected:
341  virtual void header(int level);
342 
358  std::string getDate(void);
359 
377  int strToLevel(const char* level);
378 
379  private:
380  std::string m_name;
381  std::string m_dateFormat;
382  coil::IClock* m_clock;
383  static const char* m_levelString[];
384  int m_msEnable;
385  int m_usEnable;
386  };
387 
388 
389 #ifndef NO_LOGGING
390 
405 #define RTC_LOG(LV, fmt) \
406  if (rtclog.isValid(LV)) \
407  { \
408  std::string str = ::coil::sprintf fmt; \
409  rtclog.lock(); \
410  rtclog.level(LV) << str << std::endl; \
411  rtclog.unlock(); \
412  }
413 
414 #define RTC_LOG_STR(LV, str) \
415  if (rtclog.isValid(LV)) \
416  { \
417  rtclog.lock(); \
418  rtclog.level(LV) << str << std::endl; \
419  rtclog.unlock(); \
420  }
421 
441 #define RTC_FATAL(fmt) RTC_LOG(::RTC::Logger::RTL_FATAL, fmt)
442 #define RTC_FATAL_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_FATAL, str)
443 
463 #define RTC_ERROR(fmt) RTC_LOG(::RTC::Logger::RTL_ERROR, fmt)
464 #define RTC_ERROR_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_ERROR, str)
465 
485 #define RTC_WARN(fmt) RTC_LOG(::RTC::Logger::RTL_WARN, fmt)
486 #define RTC_WARN_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_WARN, str)
487 
507 #define RTC_INFO(fmt) RTC_LOG(::RTC::Logger::RTL_INFO, fmt)
508 #define RTC_INFO_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_INFO, str)
509 
529 #define RTC_DEBUG(fmt) RTC_LOG(::RTC::Logger::RTL_DEBUG, fmt)
530 #define RTC_DEBUG_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_DEBUG, str)
531 
551 #define RTC_TRACE(fmt) RTC_LOG(::RTC::Logger::RTL_TRACE, fmt)
552 #define RTC_TRACE_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_TRACE, str)
553 
554 
574 #define RTC_VERBOSE(fmt) RTC_LOG(::RTC::Logger::RTL_VERBOSE, fmt)
575 #define RTC_VERBOSE_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_VERBOSE, str)
576 
596 #define RTC_PARANOID(fmt) RTC_LOG(::RTC::Logger::RTL_PARANOID, fmt)
597 #define RTC_PARANOID_STR(str) RTC_LOG_STR(::RTC::Logger::RTL_PARANOID, str)
598 
599 #else
600 #define RTC_FATAL(fmt)
601 #define RTC_FATAL_STR(str)
602 #define RTC_ERROR(fmt)
603 #define RTC_ERROR_STR(str)
604 #define RTC_WARN(fmt)
605 #define RTC_WARN_STR(str)
606 #define RTC_INFO(fmt)
607 #define RTC_INFO_STR(str)
608 #define RTC_DEBUG(fmt)
609 #define RTC_DEBUG_STR(str)
610 #define RTC_TRACE(fmt)
611 #define RTC_TRACE_STR(str)
612 #define RTC_VERBOSE(fmt)
613 #define RTC_VERBOSE_STR(str)
614 #define RTC_PARANOID(fmt)
615 #define RTC_PARANOID_STR(str)
616 #endif
617 
618 }; // namespace RTC
619 
620 #endif // RTC_SYSTEMLOGGER_H
virtual ~Logger(void)
Virtual destructor.
bool setLevel(const char *level)
Set log level by string.
An interface to set and get time.
Definition: ClockManager.h:52
RT-Component.
Global clock management class.
::coil::LogStreamBuffer LogStreamBuf
Definition: SystemLogger.h:34
void setClockType(std::string clocktype)
Specifying clock type to be used for logging.
::coil::LogStream LogStream
Definition: SystemLogger.h:35
Definition: SystemLogger.h:113
Definition: SystemLogger.h:110
Definition: SystemLogger.h:109
Logger(const char *name="")
Constructor.
Definition: SystemLogger.h:105
Definition: SystemLogger.h:106
void setDateFormat(const char *format)
Set date/time format for adding the header.
log_stream template class
Definition: Logger.h:540
Guard template class.
Definition: SystemLogger.h:108
Definition: SystemLogger.h:112
virtual void header(int level)
Message prefix appender function.
Logger class.
Definition: SystemLogger.h:99
log_streambuf< char > LogStreamBuffer
Definition: Logger.h:932
std::string getDate(void)
Get the current formatted date/time string Get the current datetime described by specified format...
ostream_type & level(int level)
Acquire log stream.
Definition: Logger.h:732
log_streambuf and log_stream class
Definition: SystemLogger.h:111
Definition: SystemLogger.h:107
void setName(const char *name)
Set suffix of date/time string of header.
int strToLevel(const char *level)
Set the log level Set the log level corresponding to the given string.