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  const char* color[] =
114  {
115  "\x1b[0m", // SLILENT (none)
116  "\x1b[0m\x1b[31m", // FATAL (red)
117  "\x1b[0m\x1b[35m", // ERROR (magenta)
118  "\x1b[0m\x1b[33m", // WARN (yellow)
119  "\x1b[0m\x1b[34m", // INFO (blue)
120  "\x1b[0m\x1b[32m", // DEBUG (green)
121  "\x1b[0m\x1b[36m", // TRACE (cyan)
122  "\x1b[0m\x1b[39m", // VERBOSE (default)
123  "\x1b[0m\x1b[37m" // PARANOID (white)
124  };
125  *this << color[level];
126  *this << getDate() + m_levelString[level] + m_name + ": ";
127  *this << "\x1b[0m";
128  }
129 
137  std::string Logger::getDate(void)
138  {
139  const int maxsize = 256;
140  char buf[maxsize];
142 
143  time_t timer;
144  struct tm* date;
145 
146  timer = tm.sec();
147  date = gmtime(&timer);
148 
149  strftime(buf, sizeof(buf), m_dateFormat.c_str(), date);
150  std::string fmt(buf);
151 
152  if (m_msEnable > 0)
153  {
154  char msec[4];
155 #ifdef WIN32
156  _snprintf(msec, 4, "%03d", (int)(tm.usec() / 1000));
157 #else
158  snprintf(msec, 4, "%03d", (int)(tm.usec() / 1000));
159 #endif
160  coil::replaceString(fmt, "#m#", msec);
161  }
162  if (m_usEnable > 0)
163  {
164  char usec[4];
165 #ifdef WIN32
166  _snprintf(usec, 4, "%03d",
167  (int)(tm.usec() - ((tm.usec() / 1000) * 1000)));
168 #else
169  snprintf(usec, 4, "%03d",
170  (int)(tm.usec() - ((tm.usec() / 1000) * 1000)));
171 #endif
172  coil::replaceString(fmt, "#u#", usec);
173  }
174 
175  return fmt;
176  }
177 
185  int Logger::strToLevel(const char* level)
186  {
187  std::string lv(level);
188  if (lv == "SILENT")
189  return RTL_SILENT;
190  else if (lv == "FATAL")
191  return RTL_FATAL;
192  else if (lv == "ERROR")
193  return RTL_ERROR;
194  else if (lv == "WARN")
195  return RTL_WARN;
196  else if (lv == "INFO")
197  return RTL_INFO;
198  else if (lv == "DEBUG")
199  return RTL_DEBUG;
200  else if (lv == "TRACE")
201  return RTL_TRACE;
202  else if (lv == "VERBOSE")
203  return RTL_VERBOSE;
204  else if (lv == "PARANOID")
205  return RTL_PARANOID;
206  else
207  return RTL_SILENT;
208  }
209 
210 }; // 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.
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:322
bool setLevel(const char *level)
Set log level by string.
ostream_type & level(int level)
Acquire log stream.
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
std::string m_dateFormat
Definition: SystemLogger.h:341
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
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.
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 Mon Feb 28 2022 23:00:45