13 inline std::string NowTime();
15 enum TLogLevel {logNONE, logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};
22 std::ostringstream& Get(TLogLevel level = logINFO);
24 static TLogLevel& ReportingLevel();
25 static std::string ToString(TLogLevel level);
26 static TLogLevel FromString(
const std::string& level);
28 std::ostringstream os;
31 Log& operator =(
const Log&);
38 inline std::ostringstream& Log::Get(TLogLevel level)
40 os <<
"- " << NowTime();
41 os <<
" " << ToString(level) <<
": ";
42 os << std::string(level > logDEBUG ? level - logDEBUG : 0,
'\t');
49 fprintf(stderr,
"%s", os.str().c_str());
53 inline TLogLevel& Log::ReportingLevel()
55 static TLogLevel reportingLevel = logDEBUG4;
56 return reportingLevel;
59 inline std::string Log::ToString(TLogLevel level)
61 static const char*
const buffer[] = {
"NONE",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"DEBUG1",
"DEBUG2",
"DEBUG3",
"DEBUG4"};
65 inline TLogLevel Log::FromString(
const std::string& level)
67 if (level ==
"DEBUG4")
69 if (level ==
"DEBUG3")
71 if (level ==
"DEBUG2")
73 if (level ==
"DEBUG1")
79 if (level ==
"WARNING")
85 Log().Get(logWARNING) <<
"Unknown logging level '" << level <<
"'. Using INFO level as default.";
91 #define FILE_LOG(level) \
92 if (level > FILELog::ReportingLevel()) ; \
97 inline std::string NowTime()
101 char result[100] = {0};
102 sprintf(result,
"%d:%d:%d.%d", st.wHour , st.wMinute , st.wSecond , st.wMilliseconds);
106 inline std::string NowTime()
112 strftime(buffer,
sizeof(buffer),
"%H:%M:%S", r);
114 gettimeofday(&tv, 0);
115 char result[100] = {0};
116 sprintf(result,
"%s.%03ld", buffer, (
long)tv.tv_usec / 1000);