13 #include "llvm/SmallString.h"
14 #include "llvm/raw_ostream.h"
16 inline std::string NowTime();
34 llvm::raw_ostream& Get(TLogLevel level = logINFO);
37 static TLogLevel& ReportingLevel();
38 static std::string ToString(TLogLevel level);
39 static TLogLevel FromString(
const std::string& level);
42 llvm::SmallString<128> buf;
43 llvm::raw_svector_ostream oss{buf};
47 Log& operator=(
const Log&);
52 inline llvm::raw_ostream& Log::Get(TLogLevel level) {
53 oss <<
"- " << NowTime();
54 oss <<
" " << ToString(level) <<
": ";
55 oss << std::string(level > logDEBUG ? level - logDEBUG : 0,
'\t');
61 llvm::errs() << oss.str();
64 inline TLogLevel& Log::ReportingLevel() {
65 static TLogLevel reportingLevel = logDEBUG4;
66 return reportingLevel;
69 inline std::string Log::ToString(TLogLevel level) {
70 static const char*
const buffer[] = {
"NONE",
"ERROR",
"WARNING",
71 "INFO",
"DEBUG",
"DEBUG1",
72 "DEBUG2",
"DEBUG3",
"DEBUG4"};
76 inline TLogLevel Log::FromString(
const std::string& level) {
77 if (level ==
"DEBUG4")
return logDEBUG4;
78 if (level ==
"DEBUG3")
return logDEBUG3;
79 if (level ==
"DEBUG2")
return logDEBUG2;
80 if (level ==
"DEBUG1")
return logDEBUG1;
81 if (level ==
"DEBUG")
return logDEBUG;
82 if (level ==
"INFO")
return logINFO;
83 if (level ==
"WARNING")
return logWARNING;
84 if (level ==
"ERROR")
return logERROR;
85 if (level ==
"NONE")
return logNONE;
86 Log().Get(logWARNING) <<
"Unknown logging level '" << level
87 <<
"'. Using INFO level as default.";
93 #define FILE_LOG(level) \
94 if (level > FILELog::ReportingLevel()) \
99 inline std::string NowTime() {
100 llvm::SmallString<128> buf;
101 llvm::raw_svector_ostream oss(buf);
103 using namespace std::chrono;
104 auto now = system_clock::now().time_since_epoch();
107 auto count = duration_cast<hours>(now).count() % 24;
108 if (count < 10) oss <<
"0";
112 count = duration_cast<minutes>(now).count() % 60;
113 if (count < 10) oss <<
"0";
117 count = duration_cast<seconds>(now).count() % 60;
118 if (count < 10) oss <<
"0";
122 oss << duration_cast<milliseconds>(now).count() % 1000;