29 #ifndef __COLA_LOG_H__ 30 #define __COLA_LOG_H__ 38 inline std::string NowTime();
40 enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};
48 std::ostringstream& Get(TLogLevel level = logINFO);
50 static TLogLevel& ReportingLevel();
51 static std::string ToString(TLogLevel level);
52 static TLogLevel FromString(
const std::string& level);
54 std::ostringstream os;
57 Log& operator =(
const Log&);
66 std::ostringstream& Log<T>::Get(TLogLevel level)
68 os <<
"- " << NowTime();
69 os <<
" " << ToString(level) <<
": ";
70 os << std::string(level > logDEBUG ? level - logDEBUG : 0,
'\t');
82 TLogLevel& Log<T>::ReportingLevel()
84 static TLogLevel reportingLevel = cola::logDEBUG4;
85 return reportingLevel;
89 std::string Log<T>::ToString(TLogLevel level)
91 static const char*
const buffer[] = {
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"DEBUG1",
"DEBUG2",
"DEBUG3",
"DEBUG4"};
96 TLogLevel Log<T>::FromString(
const std::string& level)
98 if (level ==
"DEBUG4")
100 if (level ==
"DEBUG3")
102 if (level ==
"DEBUG2")
104 if (level ==
"DEBUG1")
106 if (level ==
"DEBUG")
110 if (level ==
"WARNING")
112 if (level ==
"ERROR")
114 Log<T>().Get(logWARNING) <<
"Unknown logging level '" << level <<
"'. Using INFO level as default.";
121 static FILE*& Stream();
122 static void Output(
const std::string& msg);
125 inline FILE*& Output2FILE::Stream()
127 static FILE* pStream = stderr;
131 inline void Output2FILE::Output(
const std::string& msg)
133 FILE* pStream = Stream();
136 fprintf(pStream,
"%s", msg.c_str());
140 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 141 # if defined (BUILDING_FILELOG_DLL) 142 # define FILELOG_DECLSPEC __declspec (dllexport) 143 # elif defined (USING_FILELOG_DLL) 144 # define FILELOG_DECLSPEC __declspec (dllimport) 146 # define FILELOG_DECLSPEC 147 # endif // BUILDING_DBSIMPLE_DLL 149 # define FILELOG_DECLSPEC 152 class FILELOG_DECLSPEC FILELog :
public Log<Output2FILE> {};
155 #ifndef FILELOG_MAX_LEVEL 156 #define FILELOG_MAX_LEVEL cola::logDEBUG4 159 #define FILE_LOG(level) \ 160 if (level > FILELOG_MAX_LEVEL) ;\ 161 else if (level > cola::FILELog::ReportingLevel() || !cola::Output2FILE::Stream()) ; \ 162 else cola::FILELog().Get(level) 164 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) 168 inline std::string NowTime()
170 const int MAX_LEN = 200;
171 char buffer[MAX_LEN];
172 if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
173 "HH':'mm':'ss", buffer, MAX_LEN) == 0)
174 return "Error in NowTime()";
176 static DWORD first = GetTickCount();
177 std::stringstream result;
178 result << buffer <<
"." << std::setfill(
'0') << std::setw(3) << ((long)(GetTickCount() - first) % 1000);
184 #include <sys/time.h> 186 inline std::string NowTime()
192 strftime(buffer,
sizeof(buffer),
"%X", localtime_r(&t, &r));
194 gettimeofday(&tv, 0);
195 std::stringstream result;
196 result << buffer <<
"." << std::setfill(
'0') << std::setw(3) << ((long)tv.tv_usec / 1000);
204 #endif //__COLA_LOG_H__ libcola: Force-directed network layout subject to separation constraints library. ...
Definition: box.cpp:25