SystemLogger.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #include <rtm/SystemLogger.h>
00020 #include <rtm/Manager.h>
00021 
00022 #if defined(_MSC_VER)
00023 #define snprintf _snprintf
00024 #endif
00025 
00026 namespace RTC
00027 {
00028   const char* Logger::m_levelString[] =
00029     {
00030       " SILENT: ",
00031       " FATAL: ",
00032       " ERROR: ",
00033       " WARNING: ",
00034       " INFO: ",
00035       " DEBUG: ",
00036       " TRACE: ",
00037       " VERBOSE: ",
00038       " PARANOID: "
00039     };
00040 
00041   Logger::Logger(const char* name)
00042     : ::coil::LogStream(&(Manager::instance().getLogStreamBuf()),
00043                         RTL_SILENT, RTL_PARANOID, RTL_SILENT),
00044       m_name(name), m_dateFormat("%b %d %H:%M:%S.%Q"),
00045       m_msEnable(0), m_usEnable(0)
00046   {
00047     setLevel(Manager::instance().getLogLevel().c_str());
00048     m_msEnable = coil::replaceString(m_dateFormat, "%Q", "#m#");
00049     m_usEnable = coil::replaceString(m_dateFormat, "%q", "#u#");
00050   }
00051 
00052   Logger::Logger(LogStreamBuf* streambuf)
00053     : ::coil::LogStream(streambuf,
00054                         RTL_SILENT, RTL_PARANOID,  RTL_SILENT),
00055       m_name("unknown"), m_dateFormat("%b %d %H:%M:%S.%Q"),
00056       m_msEnable(0), m_usEnable(0)
00057   {
00058     m_msEnable = coil::replaceString(m_dateFormat, "%Q", "#m#");
00059     m_usEnable = coil::replaceString(m_dateFormat, "%q", "#u#");
00060   }
00061 
00062   Logger::~Logger(void)
00063   {
00064   }
00065 
00073   bool Logger::setLevel(const char* level)
00074   {
00075     return coil::LogStream::setLevel(strToLevel(level));
00076   }
00077 
00085   void Logger::setDateFormat(const char* format)
00086   {
00087     m_dateFormat = std::string(format);
00088     m_msEnable = coil::replaceString(m_dateFormat, "%Q", "#m#");
00089     m_usEnable = coil::replaceString(m_dateFormat, "%q", "#u#");
00090   }
00091 
00099   void Logger::setName(const char* name)
00100   {
00101     m_name = name;
00102   }
00103 
00111   void Logger::header(int level)
00112   {
00113     *this << getDate() + m_levelString[level] + m_name + ": ";
00114   }
00115 
00123   std::string Logger::getDate(void)
00124   {
00125     const int maxsize = 256;
00126     char buf[maxsize];
00127     coil::TimeValue tm(coil::gettimeofday());
00128 
00129     time_t timer;
00130     struct tm* date;
00131 
00132     timer = tm.sec();
00133     date = gmtime(&timer);
00134       
00135     strftime(buf, sizeof(buf), m_dateFormat.c_str(), date);
00136     std::string fmt(buf);
00137 
00138     if (m_msEnable > 0)
00139       {
00140         char msec[4];
00141 #ifdef WIN32
00142         _snprintf(msec, 4, "%03d", (int)(tm.usec() / 1000));
00143 #else
00144         snprintf(msec, 4, "%03d", (int)(tm.usec() / 1000));
00145 #endif
00146         coil::replaceString(fmt, "#m#", msec);
00147       }
00148     if (m_usEnable > 0)
00149       {
00150         char usec[4];
00151 #ifdef WIN32
00152         _snprintf(usec, 4, "%03d",
00153                  (int)(tm.usec() - ((tm.usec() / 1000) * 1000)));
00154 #else
00155         snprintf(usec, 4, "%03d",
00156                  (int)(tm.usec() - ((tm.usec() / 1000) * 1000)));
00157 #endif
00158         coil::replaceString(fmt, "#u#", usec);
00159       }
00160 
00161     return fmt;
00162   }
00163 
00171   int Logger::strToLevel(const char* level)
00172   {
00173     std::string lv(level);
00174     if      (lv == "SILENT")
00175       return RTL_SILENT;
00176     else if (lv == "FATAL")
00177       return RTL_FATAL;
00178     else if (lv == "ERROR")
00179       return RTL_ERROR;
00180     else if (lv == "WARN")
00181       return RTL_WARN;
00182     else if (lv == "INFO")
00183       return RTL_INFO;
00184     else if (lv == "DEBUG")
00185       return RTL_DEBUG;
00186     else if (lv == "TRACE")
00187       return RTL_TRACE;
00188     else if (lv == "VERBOSE")
00189       return RTL_VERBOSE;
00190     else if (lv == "PARANOID")
00191       return RTL_PARANOID;
00192     else
00193       return RTL_SILENT;
00194   }
00195 
00196 }; // namespace RTC


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:39