SystemLogger.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
19 #include <rtm/SystemLogger.h>
20 #include <rtm/Manager.h>
21 
22 #if defined(_MSC_VER)
23 #define snprintf _snprintf
24 #endif
25 
26 namespace RTC
27 {
28  const char* Logger::m_levelString[] =
29  {
30  " SILENT: ",
31  " FATAL: ",
32  " ERROR: ",
33  " WARNING: ",
34  " INFO: ",
35  " DEBUG: ",
36  " TRACE: ",
37  " VERBOSE: ",
38  " PARANOID: "
39  };
40 
41  Logger::Logger(const char* name)
42  : ::coil::LogStream(&(Manager::instance().getLogStreamBuf()),
43  RTL_SILENT, RTL_PARANOID, RTL_SILENT),
44  m_name(name), m_dateFormat("%b %d %H:%M:%S.%Q"),
45  m_msEnable(0), m_usEnable(0)
46  {
47  setLevel(Manager::instance().getLogLevel().c_str());
50  }
51 
53  : ::coil::LogStream(streambuf,
55  m_name("unknown"), m_dateFormat("%b %d %H:%M:%S.%Q"),
56  m_msEnable(0), m_usEnable(0)
57  {
60  }
61 
63  {
64  }
65 
73  bool Logger::setLevel(const char* level)
74  {
76  }
77 
85  void Logger::setDateFormat(const char* format)
86  {
87  m_dateFormat = std::string(format);
90  }
91 
99  void Logger::setName(const char* name)
100  {
101  m_name = name;
102  }
103 
112  {
113  *this << getDate() + m_levelString[level] + m_name + ": ";
114  }
115 
123  std::string Logger::getDate(void)
124  {
125  const int maxsize = 256;
126  char buf[maxsize];
128 
129  time_t timer;
130  struct tm* date;
131 
132  timer = tm.sec();
133  date = gmtime(&timer);
134 
135  strftime(buf, sizeof(buf), m_dateFormat.c_str(), date);
136  std::string fmt(buf);
137 
138  if (m_msEnable > 0)
139  {
140  char msec[4];
141 #ifdef WIN32
142  _snprintf(msec, 4, "%03d", (int)(tm.usec() / 1000));
143 #else
144  snprintf(msec, 4, "%03d", (int)(tm.usec() / 1000));
145 #endif
146  coil::replaceString(fmt, "#m#", msec);
147  }
148  if (m_usEnable > 0)
149  {
150  char usec[4];
151 #ifdef WIN32
152  _snprintf(usec, 4, "%03d",
153  (int)(tm.usec() - ((tm.usec() / 1000) * 1000)));
154 #else
155  snprintf(usec, 4, "%03d",
156  (int)(tm.usec() - ((tm.usec() / 1000) * 1000)));
157 #endif
158  coil::replaceString(fmt, "#u#", usec);
159  }
160 
161  return fmt;
162  }
163 
171  int Logger::strToLevel(const char* level)
172  {
173  std::string lv(level);
174  if (lv == "SILENT")
175  return RTL_SILENT;
176  else if (lv == "FATAL")
177  return RTL_FATAL;
178  else if (lv == "ERROR")
179  return RTL_ERROR;
180  else if (lv == "WARN")
181  return RTL_WARN;
182  else if (lv == "INFO")
183  return RTL_INFO;
184  else if (lv == "DEBUG")
185  return RTL_DEBUG;
186  else if (lv == "TRACE")
187  return RTL_TRACE;
188  else if (lv == "VERBOSE")
189  return RTL_VERBOSE;
190  else if (lv == "PARANOID")
191  return RTL_PARANOID;
192  else
193  return RTL_SILENT;
194  }
195 
196 }; // namespace RTC
void setDateFormat(const char *format)
Set date/time format for adding the header.
virtual ~Logger(void)
Virtual destructor.
virtual void header(int level)
Message prefix appender function.
RT-Component.
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
log_streambuf template class
RT component logger class.
Manager class.
Definition: Manager.h:80
void setName(const char *name)
Set suffix of date/time string of header.
static Manager & instance()
Get instance of the manager.
Definition: Manager.cpp:140
TimeValue class.
Definition: TimeValue.h:40
RTComponent manager class.
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get the time and timezone.
Definition: ace/coil/Time.h:57
log_stream template class
bool setLevel(int level)
Set the log level.
unsigned int replaceString(std::string &str, const std::string from, const std::string to)
Replace string.
Definition: stringutil.cpp:317
bool setLevel(const char *level)
Set log level by string.
ostream_type & level(int level)
Acquire log stream.
std::string m_dateFormat
Definition: SystemLogger.h:341
std::string m_name
Definition: SystemLogger.h:340
Logger(const char *name="")
Constructor.
int strToLevel(const char *level)
Set the log level Set the log level corresponding to the given string.
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
static const char * m_levelString[]
Definition: SystemLogger.h:342
std::string getDate(void)
Get the current formatted date/time string Get the current datetime described by specified format...
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:26:00