48 inline std::string NowTime();
50 enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG,
51 logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};
59 std::ostringstream& Get(TLogLevel level = logINFO);
61 static TLogLevel& ReportingLevel();
62 static std::string ToString(TLogLevel level);
63 static TLogLevel FromString(
const std::string& level);
65 std::ostringstream os;
68 Log& operator =(
const Log&);
79 os <<
"- " << NowTime();
80 os <<
" " << ToString(level) <<
": ";
81 os << std::string(level > logDEBUG ? level - logDEBUG : 0,
'\t');
95 static TLogLevel reportingLevel = logDEBUG4;
96 return reportingLevel;
102 static const char*
const buffer[] = {
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"DEBUG1",
"DEBUG2",
"DEBUG3",
"DEBUG4"};
103 return buffer[level];
106 template <
typename T>
109 if (level ==
"DEBUG4")
111 if (level ==
"DEBUG3")
113 if (level ==
"DEBUG2")
115 if (level ==
"DEBUG1")
117 if (level ==
"DEBUG")
121 if (level ==
"WARNING")
123 if (level ==
"ERROR")
125 Log<T>().Get(logWARNING) <<
"Unknown logging level '" << level <<
"'. Using INFO level as default.";
132 static FILE*& Stream();
133 static void Output(
const std::string& msg);
136 inline FILE*& Output2FILE::Stream()
138 static FILE* pStream = stdout;
142 inline void Output2FILE::Output(
const std::string& msg)
144 FILE* pStream = Stream();
147 fprintf(pStream,
"%s", msg.c_str());
151 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 152 # if defined (BUILDING_FILELOG_DLL) 153 # define FILELOG_DECLSPEC __declspec (dllexport) 154 # elif defined (USING_FILELOG_DLL) 155 # define FILELOG_DECLSPEC __declspec (dllimport) 157 # define FILELOG_DECLSPEC 158 # endif // BUILDING_DBSIMPLE_DLL 160 # define FILELOG_DECLSPEC 166 #ifndef FILELOG_MAX_LEVEL 167 #define FILELOG_MAX_LEVEL logDEBUG4 170 #define FILE_LOG(level) \ 171 if (level > FILELOG_MAX_LEVEL) ;\ 172 else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \ 173 else FILELog().Get(level) 175 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 182 inline std::string NowTime()
184 const int MAX_LEN = 200;
185 char buffer[MAX_LEN];
186 if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
187 "HH':'mm':'ss", buffer, MAX_LEN) == 0)
188 return "Error in NowTime()";
190 char result[100] = {0};
191 static DWORD first = GetTickCount();
192 std::sprintf(result,
"%s.%03ld", buffer, (
long)(GetTickCount() - first) % 1000);
198 #include <sys/time.h> 200 inline std::string NowTime()
203 gettimeofday(&tv, 0);
206 strftime(buffer,
sizeof(buffer),
"%X", localtime_r(&tv.tv_sec, &r));
207 char result[100] = {0};
208 std::sprintf(result,
"%s.%06ld", buffer, (
long)tv.tv_usec);