-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog.cpp
265 lines (224 loc) · 5.75 KB
/
log.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <log.h>
#include <regex>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <execinfo.h>
#include <inttypes.h>
#include <sys/time.h>
static bool startTimeReady;
static struct timeval startTime;
static void initTime() {
if(false==startTimeReady) {
gettimeofday(&startTime, 0);
startTimeReady = true;
}
}
struct PreMainInit {
PreMainInit() {
initTime();
}
} preMainInit;
bool gQuiet;
uint64_t gProgIdHi;
uint64_t gProgIdLo;
uint8_t gMode = ' ';
#define GLOBAL(x, y) \
x g##y; \
static pthread_mutex_t y##Lock = PTHREAD_MUTEX_INITIALIZER; \
void lock##y() { LOG_FTL(0!=pthread_mutex_lock(&(y##Lock)), "failed to acquire lock for " #y); } \
void unlock##y() { LOG_FTL(0!=pthread_mutex_unlock(&(y##Lock)), "failed to release lock for " #y); } \
GLOBAL( bool, Logging)
GLOBAL( bool, InitDone)
GLOBAL(uint64_t, TIDPool)
#undef GLOBAL
#define LOG_OUT stdout
static void initLog() {
if(gInitDone) {
return;
}
lockInitDone();
if(false==gInitDone) {
gInitDone = true;
initTime();
setvbuf(stdout, (char *) NULL, _IONBF, 0);
setvbuf(stderr, (char *) NULL, _IONBF, 0);
ssize_t sz = sizeof(gProgIdHi);
int f = open("/dev/urandom", O_RDONLY);
LOG_FTL(sz!=read(f, &gProgIdHi, sz), "rng fails");
LOG_FTL(sz!=read(f, &gProgIdLo, sz), "rng fails");
gProgIdLo <<= 16; // make room for thread id
close(f);
}
LOG_DBG(
"progId = 0x%" PRIX64 "%" PRIX64 "",
gProgIdHi,
gProgIdLo
);
unlockInitDone();
}
int Log::threadId() {
static __thread int gTID = -1;
if(gTID<0) {
lockTIDPool();
gTID = ++gTIDPool;
unlockTIDPool();
}
return gTID;
}
static inline int getStackDepth() {
#if defined(NDEBUG)
return 1;
#else
void *buffer[1024];
return backtrace(buffer, 1024);
#endif
}
static const char *logMsgNames[] = {
"log",
"nfo",
"wrn",
"ftl",
0
};
static void vMsg(
Log::Mode mode,
const char *fileName,
const char *functionName,
int lineNumber,
const char *format,
va_list argPtr
) {
initLog();
if(gQuiet) {
return;
}
const char *logMsg = logMsgNames[mode];
if(Log::kDbg==mode) {
static const char *filter;
static auto initDone = false;
static std::regex *regexp = 0;
if(!initDone) {
filter = getenv("LOG");
if(filter) {
std::string r(filter);
r = ".*" + r + ".*";
regexp = new std::regex(
r.c_str(),
std::regex_constants::optimize |
std::regex_constants::icase
);
}
}
bool matches = false;
if(0!=regexp) {
auto m0 = std::regex_match(logMsg, *regexp);
auto m1 = std::regex_match(fileName, *regexp);
auto m2 = std::regex_match(functionName, *regexp);
auto m3 = format ? std::regex_match(format, *regexp) : false;
matches = (m0 || m1 || m2 || m3);
}
if(!matches) {
return;
}
}
struct timeval t;
gettimeofday(&t, 0);
const char *fName = strlen(fileName) + fileName;
while(fileName<fName && fName[0]!='/') {
--fName;
}
if(fName[0]=='/') {
++fName;
}
lockLogging();
static auto prevTime = -1.0;
auto deltaTime = 0.0;
auto dTime = (t.tv_sec + 1e-6*t.tv_usec);
if(0.0<=prevTime) {
deltaTime = (dTime - prevTime);
}
prevTime = dTime;
fprintf(
LOG_OUT,
"%3d.%06d(%+12.6f):%s:%c%08X:T%2d:D%3d:L%4d:%s:%s: ",
(int)(t.tv_sec - startTime.tv_sec),
(int)t.tv_usec,
deltaTime,
logMsg,
gMode,
(int)gProgIdHi,
Log::threadId(),
getStackDepth()-1,
lineNumber,
fName,
functionName
);
if(0!=format) {
vfprintf(LOG_OUT, format, argPtr);
}
if(Log::kFatal==mode && 0!=errno) {
perror(" ====> system error = ");
}
fputc('\n', LOG_OUT);
fflush(LOG_OUT);
if(Log::kFatal==mode) {
abort();
}
unlockLogging();
}
void Log::msg(
Log::Mode mode,
const char *fileName,
const char *functionName,
int lineNumber,
const char *format,
...
) {
va_list arg;
va_start(arg, format);
vMsg(
mode,
fileName,
functionName,
lineNumber,
format,
arg
);
va_end(arg);
}
void Log::assrt(
const char *fileName,
const char *functionName,
int lineNumber,
bool bCondition,
const char *condition,
const char *format,
...
) {
if(bCondition) {
return;
}
va_list arg;
va_start(arg, format);
Log::msg(
Log::kInfo,
fileName,
functionName,
lineNumber,
"assertion failing: %s",
condition
);
vMsg(
Log::kFatal,
fileName,
functionName,
lineNumber,
format,
arg
);
va_end(arg);
}