-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.h
133 lines (113 loc) · 2.91 KB
/
Log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma once
#include <thread>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
inline std::string NowTime();
inline std::string CurrentTid();
enum TLogLevel
{
logNONE,
logERROR,
logWARNING,
logINFO,
logDEBUG,
logDEBUG1,
logDEBUG2,
logDEBUG3,
logDEBUG4
};
class Log
{
public:
Log();
virtual ~Log();
std::ostringstream& Get(TLogLevel level = logINFO);
public:
static TLogLevel& ReportingLevel();
static std::string ToString(TLogLevel level);
static TLogLevel FromString(const std::string& level);
protected:
std::ostringstream os;
private:
Log(const Log&);
Log& operator=(const Log&);
};
inline Log::Log()
{
}
inline std::ostringstream& Log::Get(TLogLevel level)
{
os << "- " << NowTime() << " ";
os << "[" << CurrentTid() << "]";
os << ToString(level) << ": ";
os << std::string(level > logDEBUG ? level - logDEBUG : 0, '\t');
return os;
}
inline Log::~Log()
{
os << std::endl;
std::cerr << os.str();
}
inline TLogLevel& Log::ReportingLevel()
{
static TLogLevel reportingLevel = logDEBUG;
return reportingLevel;
}
inline std::string Log::ToString(TLogLevel level)
{
static const char* const buffer[] = { "NONE", "ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3",
"DEBUG4" };
return buffer[level];
}
inline TLogLevel Log::FromString(const std::string& level)
{
if(level == "DEBUG4")
return logDEBUG4;
if(level == "DEBUG3")
return logDEBUG3;
if(level == "DEBUG2")
return logDEBUG2;
if(level == "DEBUG1")
return logDEBUG1;
if(level == "DEBUG")
return logDEBUG;
if(level == "INFO")
return logINFO;
if(level == "WARNING")
return logWARNING;
if(level == "ERROR")
return logERROR;
if(level == "NONE")
return logNONE;
Log().Get(logWARNING) << "Unknown logging level '" << level << "'. Using INFO level as default.";
return logINFO;
}
typedef Log FILELog;
#define FILE_LOG(level) \
if(level > FILELog::ReportingLevel()) \
; \
else \
Log().Get(level)
inline std::string NowTime()
{
std::stringstream ss;
ss << std::setfill('0') << std::setw(2);
using namespace std::chrono;
auto now = system_clock::now().time_since_epoch();
ss << duration_cast<hours>(now).count() % 24 << ":" << duration_cast<minutes>(now).count() % 60 << ":"
<< duration_cast<seconds>(now).count() % 60 << "." << duration_cast<milliseconds>(now).count() % 1000;
return ss.str();
}
inline std::string CurrentTid()
{
std::thread::id this_id = std::this_thread::get_id();
// pthread_t tid = pthread_self();
// printf("tid:%x\n",tid);
std::stringstream ss;
ss << std::setfill('0') << std::setw(14) << this_id;
return ss.str();
}