15 #include "llvm/SmallString.h"
16 #include "llvm/raw_ostream.h"
18 inline std::string NowTime();
36 llvm::raw_ostream& Get(TLogLevel level = logINFO);
39 static TLogLevel& ReportingLevel();
40 static std::string ToString(TLogLevel level);
41 static TLogLevel FromString(
const std::string& level);
44 llvm::SmallString<128> buf;
45 llvm::raw_svector_ostream oss{buf};
49 Log& operator=(
const Log&);
54 inline llvm::raw_ostream& Log::Get(TLogLevel level) {
55 oss <<
"- " << NowTime();
56 oss <<
" " << ToString(level) <<
": ";
57 oss << std::string(level > logDEBUG ? level - logDEBUG : 0,
'\t');
63 std::cerr << oss.str();
66 inline TLogLevel& Log::ReportingLevel() {
67 static TLogLevel reportingLevel = logDEBUG4;
68 return reportingLevel;
71 inline std::string Log::ToString(TLogLevel level) {
72 static const char*
const buffer[] = {
"NONE",
"ERROR",
"WARNING",
73 "INFO",
"DEBUG",
"DEBUG1",
74 "DEBUG2",
"DEBUG3",
"DEBUG4"};
78 inline TLogLevel Log::FromString(
const std::string& level) {
79 if (level ==
"DEBUG4")
return logDEBUG4;
80 if (level ==
"DEBUG3")
return logDEBUG3;
81 if (level ==
"DEBUG2")
return logDEBUG2;
82 if (level ==
"DEBUG1")
return logDEBUG1;
83 if (level ==
"DEBUG")
return logDEBUG;
84 if (level ==
"INFO")
return logINFO;
85 if (level ==
"WARNING")
return logWARNING;
86 if (level ==
"ERROR")
return logERROR;
87 if (level ==
"NONE")
return logNONE;
88 Log().Get(logWARNING) <<
"Unknown logging level '" << level
89 <<
"'. Using INFO level as default.";
95 #define FILE_LOG(level) \
96 if (level > FILELog::ReportingLevel()) \
101 inline std::string NowTime() {
102 llvm::SmallString<128> buf;
103 llvm::raw_svector_ostream oss(buf);
105 using namespace std::chrono;
106 auto now = system_clock::now().time_since_epoch();
109 auto count = duration_cast<hours>(now).count() % 24;
110 if (count < 10) oss <<
"0";
114 count = duration_cast<minutes>(now).count() % 60;
115 if (count < 10) oss <<
"0";
119 count = duration_cast<seconds>(now).count() % 60;
120 if (count < 10) oss <<
"0";
124 oss << duration_cast<milliseconds>(now).count() % 1000;