-
Notifications
You must be signed in to change notification settings - Fork 4
/
logging.cpp
134 lines (104 loc) · 2.13 KB
/
logging.cpp
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
#include "logging.h"
#include <string.h>
namespace ydx
{
__thread char t_errnobuf[512];
__thread char t_time[32];
__thread time_t t_lastSecond;
const char* strerror_tl(int err_)
{
return strerror_r(err_, t_errnobuf, sizeof(t_errnobuf));
}
const char* LogLevelName[Logger::NUM_LOG_LEVELS] =
{
"[TRACE]",
"[DEBUG]",
"[INFO]",
"[WARN]",
"[ERROR]",
"[FATAL]",
};
void defaultOutput(const char* msg, int len)
{
size_t n = fwrite(msg, 1, len, stdout);
//FIXME check n
(void)n;
}
void defaultFlush()
{
fflush(stdout);
}
Logger::OutputFunc g_output = defaultOutput;
Logger::FlushFunc g_flush = defaultFlush;
Logger::LogLevel initLogLevel()
{
return Logger::INFO;
}
Logger::LogLevel g_logLevel = Logger::INFO;
}
using namespace ydx;
Logger::Logger(SourceFile file, int line)
: ctime_(::time(NULL)),
stream_(),
basename_(file)
{
level_ = INFO;
errno_ = 0;
line_ = line;
stream_ << ctime_.Format(t_time);
stream_ << LogLevelName[level_];
}
Logger::Logger(SourceFile file, int line, LogLevel level, const char* func)
: ctime_(::time(NULL)),
stream_(),
basename_(file)
{
stream_ << func << ' ';
level_ = level;
line_ = line;
stream_ << ctime_.Format(t_time);
stream_ << LogLevelName[level_];
}
Logger::Logger(SourceFile file, int line, LogLevel level)
: ctime_(::time(NULL)),
stream_(),
basename_(file)
{
level_ = level;
line_ = line;
stream_ << ctime_.Format(t_time);
stream_ << LogLevelName[level_];
}
Logger::Logger(SourceFile file, int line, bool toAbort)
: ctime_(::time(NULL)),
stream_(),
basename_(file)
{
level_ = toAbort?FATAL:ERROR;
line_ = line;
stream_ << ctime_.Format(t_time);
stream_ << LogLevelName[level_];
}
Logger::~Logger()
{
stream_ << " - " << StringPiece(basename_.data_, basename_.size_) << ':' << line_ << '\n';
const LogStream::Buffer& buf(stream().buffer());
g_output(buf.data(), buf.length());
if (level_ == FATAL)
{
g_flush();
abort();
}
}
void Logger::setLogLevel(Logger::LogLevel level)
{
g_logLevel = level;
}
void Logger::setOutput(OutputFunc out)
{
g_output = out;
}
void Logger::setFlush(FlushFunc flush)
{
g_flush = flush;
}