forked from abduld/libwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbLogger.cpp
332 lines (271 loc) · 8.39 KB
/
wbLogger.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "wb.h"
wbLogger_t _logger = nullptr;
static inline wbBool wbLogEntry_hasNext(wbLogEntry_t elem) {
return wbLogEntry_getNext(elem) != nullptr;
}
static inline wbLogEntry_t wbLogEntry_new() {
wbLogEntry_t elem;
elem = wbNew(struct st_wbLogEntry_t);
wbLogEntry_setId(elem, uuid());
wbLogEntry_setSessionId(elem, sessionId());
wbLogEntry_setMessage(elem, NULL);
wbLogEntry_setMPIRank(elem, wbMPI_getRank());
wbLogEntry_setTime(elem, _hrtime());
wbLogEntry_setLevel(elem, wbLogLevel_TRACE);
wbLogEntry_setNext(elem, NULL);
wbLogEntry_setLine(elem, -1);
wbLogEntry_setFile(elem, NULL);
wbLogEntry_setFunction(elem, NULL);
return elem;
}
static inline wbLogEntry_t
wbLogEntry_initialize(wbLogLevel_t level, string msg, const char *file,
const char *fun, int line) {
wbLogEntry_t elem;
elem = wbLogEntry_new();
wbLogEntry_setLevel(elem, level);
wbLogEntry_setMessage(elem, wbString_duplicate(msg));
wbLogEntry_setLine(elem, line);
wbLogEntry_setFile(elem, file);
wbLogEntry_setFunction(elem, fun);
return elem;
}
static inline void wbLogEntry_delete(wbLogEntry_t elem) {
if (elem != nullptr) {
if (wbLogEntry_getMessage(elem) != nullptr) {
wbFree(wbLogEntry_getMessage(elem));
}
wbDelete(elem);
}
return;
}
static inline const char *getLevelName(wbLogLevel_t level) {
switch (level) {
case wbLogLevel_unknown:
return "Unknown";
case wbLogLevel_OFF:
return "Off";
case wbLogLevel_FATAL:
return "Fatal";
case wbLogLevel_ERROR:
return "Error";
case wbLogLevel_WARN:
return "Warn";
case wbLogLevel_INFO:
return "Info";
case wbLogLevel_DEBUG:
return "Debug";
case wbLogLevel_TRACE:
return "Trace";
}
return NULL;
}
static inline json11::Json wbLogEntry_toJSONObject(wbLogEntry_t elem) {
if (elem == nullptr) {
return json11::Json{};
}
json11::Json json = json11::Json::object{
{"id", wbLogEntry_getId(elem)},
{"session_id", wbLogEntry_getSessionId(elem)},
{"mpi_rank", wbLogEntry_getMPIRank(elem)},
{"level", getLevelName(wbLogEntry_getLevel(elem))},
{"file", wbLogEntry_getFile(elem)},
{"function", wbLogEntry_getFunction(elem)},
{"line", wbLogEntry_getLine(elem)},
{"time", wbLogEntry_getTime(elem)},
{"message", wbLogEntry_getMessage(elem)},
};
return json;
}
static inline string wbLogEntry_toJSON(wbLogEntry_t elem) {
if (elem == NULL) {
return "\"\"";
} else if (WB_USE_JSON11) {
json11::Json json = wbLogEntry_toJSONObject(elem);
return json.string_value();
} else {
stringstream ss;
ss << "{\n";
ss << wbString_quote("id") << ":"
<< wbString_quote(wbLogEntry_getId(elem)) << ",\n";
ss << wbString_quote("session_id") << ":"
<< wbString_quote(wbLogEntry_getSessionId(elem)) << ",\n";
ss << wbString_quote("mpi_rank") << ":"
<< wbString(wbLogEntry_getMPIRank(elem)) << ",\n";
ss << wbString_quote("level") << ":"
<< wbString_quote(getLevelName(wbLogEntry_getLevel(elem))) << ",\n";
ss << wbString_quote("message") << ":"
<< wbString_quote(wbLogEntry_getMessage(elem)) << ",\n";
ss << wbString_quote("file") << ":"
<< wbString_quote(wbLogEntry_getFile(elem)) << ",\n";
ss << wbString_quote("function") << ":"
<< wbString_quote(wbLogEntry_getFunction(elem)) << ",\n";
ss << wbString_quote("line") << ":" << wbLogEntry_getLine(elem)
<< ",\n";
ss << wbString_quote("time") << ":" << wbLogEntry_getTime(elem)
<< "\n";
ss << "}";
return ss.str();
}
return "";
}
static inline string wbLogEntry_toXML(wbLogEntry_t elem) {
if (elem != nullptr) {
stringstream ss;
ss << "<entry>\n";
ss << "<type>"
<< "LoggerElement"
<< "</type>\n";
ss << "<id>" << wbLogEntry_getId(elem) << "</id>\n";
ss << "<session_id>" << wbLogEntry_getSessionId(elem)
<< "</session_id>\n";
ss << "<level>" << wbLogEntry_getLevel(elem) << "</level>\n";
ss << "<message>" << wbLogEntry_getMessage(elem) << "</message>\n";
ss << "<file>" << wbLogEntry_getFile(elem) << "</file>\n";
ss << "<function>" << wbLogEntry_getFunction(elem) << "</function>\n";
ss << "<line>" << wbLogEntry_getLine(elem) << "</line>\n";
ss << "<time>" << wbLogEntry_getTime(elem) << "</time>\n";
ss << "</entry>\n";
return ss.str();
}
return "";
}
wbLogger_t wbLogger_new() {
wbLogger_t logger;
logger = wbNew(struct st_wbLogger_t);
wbLogger_setId(logger, uuid());
wbLogger_setSessionId(logger, sessionId());
wbLogger_setLength(logger, 0);
wbLogger_setHead(logger, NULL);
wbLogger_getLevel(logger) = wbLogLevel_TRACE;
return logger;
}
static inline void _wbLogger_setLevel(wbLogger_t logger,
wbLogLevel_t level) {
wbLogger_getLevel(logger) = level;
}
static inline void _wbLogger_setLevel(wbLogLevel_t level) {
_wbLogger_setLevel(_logger, level);
}
#define wbLogger_setLevel(level) _wbLogger_setLevel(wbLogLevel_##level)
void wbLogger_clear(wbLogger_t logger) {
if (logger != nullptr) {
wbLogEntry_t tmp;
wbLogEntry_t iter;
iter = wbLogger_getHead(logger);
while (iter != nullptr) {
tmp = wbLogEntry_getNext(iter);
wbLogEntry_delete(iter);
iter = tmp;
}
wbLogger_setLength(logger, 0);
wbLogger_setHead(logger, NULL);
}
}
void wbLogger_delete(wbLogger_t logger) {
if (logger != nullptr) {
wbLogger_clear(logger);
wbDelete(logger);
}
return;
}
void wbLogger_append(wbLogLevel_t level, string msg, const char *file,
const char *fun, int line) {
wbLogEntry_t elem;
wbLogger_t logger;
wb_init(NULL, NULL);
logger = _logger;
if (wbLogger_getLevel(logger) < level) {
return;
}
elem = wbLogEntry_initialize(level, msg, file, fun, line);
#ifdef wbLogger_printOnLog
if (wbLogger_printOnLog) {
if (level <= wbLogger_getLevel(logger) && elem) {
json11::Json json = json11::Json::object{
{"type", "logger"},
{"id", wbLogEntry_getId(elem)},
{"session_id", wbLogEntry_getSessionId(elem)},
{"data", wbLogEntry_toJSONObject(elem)}};
std::cout << json.dump() << std::endl;
}
}
#endif /* wbLogger_printOnLog */
if (wbLogger_getHead(logger) == nullptr) {
wbLogger_setHead(logger, elem);
} else {
wbLogEntry_t prev = wbLogger_getHead(logger);
while (wbLogEntry_hasNext(prev)) {
prev = wbLogEntry_getNext(prev);
}
wbLogEntry_setNext(prev, elem);
}
#if 0
if (level <= wbLogger_getLevel(logger) && elem) {
const char *levelName = getLevelName(level);
fprintf(stderr, "= LOG: %s: %s (In %s:%s on line %d). =\n", levelName,
wbLogEntry_getMessage(elem), wbLogEntry_getFile(elem),
wbLogEntry_getFunction(elem), wbLogEntry_getLine(elem));
}
#endif
wbLogger_incrementLength(logger);
#ifndef JSON_OUTPUT
printf("%s\n",wbLogEntry_getMessage(elem));
#endif /* JSON_OUTPUT */
return;
}
string wbLogger_toJSON() {
return wbLogger_toJSON(_logger);
}
static json11::Json wbLogger_toJSONObject(wbLogger_t logger) {
std::vector<json11::Json> elems{};
if (logger != nullptr) {
wbLogEntry_t iter;
stringstream ss;
for (iter = wbLogger_getHead(logger); iter != nullptr;
iter = wbLogEntry_getNext(iter)) {
elems.push_back(wbLogEntry_toJSONObject(iter));
}
}
return json11::Json(elems);
}
string wbLogger_toJSON(wbLogger_t logger) {
if (logger != nullptr) {
wbLogEntry_t iter;
stringstream ss;
for (iter = wbLogger_getHead(logger); iter != nullptr;
iter = wbLogEntry_getNext(iter)) {
ss << wbLogEntry_toJSON(iter);
if (wbLogEntry_getNext(iter) != nullptr) {
ss << ",\n";
}
}
return ss.str();
}
return "";
}
string wbLogger_toXML() {
return wbLogger_toXML(_logger);
}
string wbLogger_toXML(wbLogger_t logger) {
if (logger != nullptr) {
wbLogEntry_t iter;
stringstream ss;
ss << "<logger>\n";
ss << "<type>"
<< "Logger"
<< "</type>\n";
ss << "<id>" << wbLogger_getId(logger) << "</id>\n";
ss << "<session_id>" << wbLogger_getSessionId(logger)
<< "</session_id>\n";
ss << "<elements>\n";
for (iter = wbLogger_getHead(logger); iter != nullptr;
iter = wbLogEntry_getNext(iter)) {
ss << wbLogEntry_toXML(iter);
}
ss << "</elements>\n";
ss << "</logger>\n";
return ss.str();
}
return "";
}