-
Notifications
You must be signed in to change notification settings - Fork 3
/
formatter.cc
391 lines (367 loc) · 12.6 KB
/
formatter.cc
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "elog/formatter.h"
#include "elog/config.h"
#include "elog/logger_util.h"
#include "elog/micros.h"
#include "elog/switch_helper.h"
#include "fmt/color.h"
using namespace elog;
namespace {
#define LEVEL_COUNT static_cast<int>(Levels::kLevelCount)
const char* s_level_text[LEVEL_COUNT + 1] = {"TRACE", "DEBUG", "INFO", "WARN",
"ERROR", "FATAL", "UNKOW"};
const char* s_simple_text[LEVEL_COUNT + 1] = {"TRC", "DEB", "INF", "WAR",
"ERR", "FAL", "UNK"};
fmt::color s_color[LEVEL_COUNT + 1] = {
fmt::color::light_blue, fmt::color::teal, fmt::color::green,
fmt::color::yellow, fmt::color::red, fmt::color::purple,
fmt::color::orange_red, // if not in range
};
const char* s_ansi_color[LEVEL_COUNT + 1] = {
"\033[36m", // light_blue
"\033[34m", // teal
"\033[32m", // green
"\033[33m", // yellow
"\033[31m", // red
"\033[35m", // purple
"\033[4;31m", // red and underline
};
inline const char* GET_LEVEL_TEXT(int level, bool simple = false)
{
if (simple)
{
if (level >= LEVEL_COUNT || level < 0)
return s_simple_text[LEVEL_COUNT]; // level not in range
return s_simple_text[level];
}
if (level >= LEVEL_COUNT || level < 0)
return s_level_text[LEVEL_COUNT]; // level not in range
return s_level_text[level];
}
inline fmt::color GET_COLOR_BY_LEVEL(int level)
{
if (level >= LEVEL_COUNT || level < 0)
return s_color[LEVEL_COUNT]; // level not in range
return s_color[level];
}
inline const char* GET_ANSI_COLOR_BY_LEVEL(int level)
{
if (level >= LEVEL_COUNT || level < 0)
return s_ansi_color[LEVEL_COUNT]; // level not in range
return s_ansi_color[level];
}
#define INT(x) static_cast<int>(x)
#define IS_SET(log_flag_, flags_) (INT(log_flag_) & INT(flags_))
} // namespace
void formatter::defaultFormatter(Config* config, SharedContext const& ctx,
buffer_t& buffer, Appenders outType)
{
assert(config != nullptr);
output_buf_t t_outputBuffer;
t_outputBuffer.buf = &buffer;
// RAII before after call
auto helper =
trigger_helper(&t_outputBuffer, &config->log_before, &config->log_after);
// prepare data
auto* levelText = GET_LEVEL_TEXT(ctx->level);
auto* funcName =
IS_SET(config->log_flag, Flags::kFuncName) ? ctx->func_name : nullptr;
auto* filename =
IS_SET(config->log_flag, Flags::kLongname)
? ctx->long_filename
: (IS_SET(config->log_flag, Flags::kShortname) ? ctx->short_filename
: nullptr);
Flags logFlag = config->log_flag;
// log name
if (config->log_name)
{
fmt::format_to(fmt::appender(buffer), "[{}]", config->log_name);
}
// log date
if (IS_SET(logFlag, Flags::kDate)) // y-m-d
{
fmt::format_to(fmt::appender(buffer), "[{}]",
Util::getCurDateTime(IS_SET(logFlag, Flags::kTime)));
}
// log level
fmt::format_to(fmt::appender(buffer), "[{}]", levelText);
if (IS_SET(logFlag, Flags::kThreadId))
{ // log thread id
fmt::format_to(fmt::appender(buffer), "[tid:{:d}]", ctx->tid);
}
if (filename)
{
fmt::format_to(fmt::appender(buffer), "[{}:{:d}]", filename,
ctx->line); // log file-line
}
// log func_name
if (funcName)
{
fmt::format_to(fmt::appender(buffer), "[func:{}]",
funcName); // log file-line
}
fmt::string_view text{ctx->text.data(), ctx->text.size()};
if (ctx->level >= INT(Levels::kError) && ctx->err != 0)
{ // if level >= Error,get the error info
fmt::format_to(fmt::appender(buffer), ": {} ^system error code:{}", text,
ctx->err); // 打印系统错误提示信息
}
else
{
fmt::format_to(fmt::appender(buffer), ": {}",
text); // log info
}
}
void formatter::colorfulFormatter(Config* config, SharedContext const& ctx,
buffer_t& buffer, Appenders outType)
{
assert(config != nullptr);
output_buf_t t_outputBuffer;
t_outputBuffer.buf = &buffer;
// RAII before after call
auto helper =
trigger_helper(&t_outputBuffer, &config->log_before, &config->log_after);
// prepare data
auto* levelText = GET_LEVEL_TEXT(ctx->level);
auto* funcName =
IS_SET(config->log_flag, Flags::kFuncName) ? ctx->func_name : nullptr;
auto* filename =
IS_SET(config->log_flag, Flags::kLongname)
? ctx->long_filename
: (IS_SET(config->log_flag, Flags::kShortname) ? ctx->short_filename
: nullptr);
Flags logFlag = config->log_flag;
// log name
if (config->log_name)
{
if (outType == kConsole)
{
fmt::format_to(std::back_inserter(buffer),
fg(GET_COLOR_BY_LEVEL(ctx->level)), "[{}]",
config->log_name);
}
else
{
fmt::format_to(std::back_inserter(buffer), "[{}]", config->log_name);
}
}
// log date
if (IS_SET(logFlag, Flags::kDate)) // y-m-d
{
fmt::format_to(std::back_inserter(buffer), "[{}]",
Util::getCurDateTime(IS_SET(logFlag, Flags::kTime)));
}
// log level
if (outType == Appenders::kConsole) // colorful
{
fmt::format_to(std::back_inserter(buffer),
fg(GET_COLOR_BY_LEVEL(ctx->level)), "[{}]", levelText);
}
else
{ // nocolor
fmt::format_to(std::back_inserter(buffer), "[{}]", levelText);
}
if (IS_SET(logFlag, Flags::kThreadId))
{ // log thread id
fmt::format_to(std::back_inserter(buffer), "[tid:{:d}]", ctx->tid);
}
if (filename)
{
fmt::format_to(std::back_inserter(buffer), "[{}:{:d}]", filename,
ctx->line); // log file-line
}
// log func_name
if (funcName)
{
fmt::format_to(std::back_inserter(buffer), "[func:{}]",
funcName); // log file-line
}
fmt::string_view text{ctx->text.data(), ctx->text.size()};
if (outType == Appenders::kConsole) // colorful
{
if (ctx->level >= INT(Levels::kError) && ctx->err != 0)
{ // if level >= Error,get the error info
fmt::format_to(std::back_inserter(buffer),
fg(GET_COLOR_BY_LEVEL(ctx->level)),
": {} ^system error code:{}", text,
ctx->err); // 打印系统错误提示信息
}
else
{
fmt::format_to(std::back_inserter(buffer),
fg(GET_COLOR_BY_LEVEL(ctx->level)), ": {}",
text); // log info
}
}
else
{ // nocolor
if (ctx->level >= INT(Levels::kError) && ctx->err != 0)
{ // if level >= Error,get the error info
fmt::format_to(std::back_inserter(buffer),
": {} ^system error code:{}", text,
ctx->err); // 打印提示信息
}
else
{
fmt::format_to(std::back_inserter(buffer), ": {}",
text); // log info
}
}
}
void formatter::jsonFormatter(Config* config, SharedContext const& ctx,
buffer_t& buffer, Appenders outType)
{
assert(config != nullptr);
output_buf_t t_outputBuffer;
t_outputBuffer.buf = &buffer;
// RAII before after call
auto helper =
trigger_helper(&t_outputBuffer, &config->log_before, &config->log_after);
// prepare data
auto* dateText =
Util::getCurDateTime(IS_SET(config->log_flag, Flags::kTime));
auto* levelText = GET_LEVEL_TEXT(ctx->level, true);
auto* funcName =
IS_SET(config->log_flag, Flags::kFuncName) ? ctx->func_name : nullptr;
auto* filename =
IS_SET(config->log_flag, Flags::kLongname)
? ctx->long_filename
: (IS_SET(config->log_flag, Flags::kShortname) ? ctx->short_filename
: nullptr);
t_outputBuffer.push_back('{');
if (config->log_name)
{
t_outputBuffer.formatTo(R"("name":"{}", )", config->log_name);
}
if (dateText) { t_outputBuffer.formatTo(R"("time":"{}")", dateText); }
if (levelText) { t_outputBuffer.formatTo(R"(, "level":"{}")", levelText); }
if (IS_SET(config->log_flag, Flags::kThreadId))
{
t_outputBuffer.formatTo(R"(, "tid":"{:d}")", ctx->tid);
}
if (filename)
{
if (IS_SET(config->log_flag, Flags::kLine))
{
t_outputBuffer.formatTo(R"(, "file":"{}:{:d}")", filename, ctx->line);
}
else { t_outputBuffer.formatTo(R"(, "file":"{}")", filename); }
}
if (funcName) { t_outputBuffer.formatTo(R"(, "func":"{}")", funcName); }
t_outputBuffer.formatTo(
R"(, "message":"{}")",
fmt::string_view{ctx->text.data(), ctx->text.size()});
t_outputBuffer.push_back('}');
}
// use %T:time,%t:tid,%F:filepath,%f:func, %e:error info
// %L:long levelText,%l:short levelText,%n:name,%v:message ,%c color start %C
// color end
void formatter::customStringFormatter(const char* raw_format_str,
Config* config, SharedContext const& ctx,
buffer_t& buffer, Appenders outType)
{
assert(config != nullptr);
output_buf_t outputBuffer;
outputBuffer.buf = &buffer;
// RAII before after call
auto helper =
trigger_helper(&outputBuffer, &config->log_before, &config->log_after);
StringView format_str{raw_format_str};
size_t index = format_str.find('%');
// not find format flag
if (index == std::string::npos)
{
outputBuffer.append(format_str);
return;
}
// start custom format
outputBuffer.append(format_str.substr(0, index));
for (;;)
{
auto op = format_str.substr(index, 2);
switch (OP_INT(op))
{
case "%T"_i: {
auto* dateText =
Util::getCurDateTime(IS_SET(config->log_flag, Flags::kTime));
outputBuffer.append(dateText);
break;
}
case "%t"_i: {
outputBuffer.formatTo("{:d}", ctx->tid);
break;
}
case "%F"_i: {
auto* filepath = IS_SET(config->log_flag, Flags::kLongname)
? ctx->long_filename
: (IS_SET(config->log_flag, Flags::kShortname)
? ctx->short_filename
: nullptr);
if (filepath)
{
if (IS_SET(config->log_flag, Flags::kLine))
outputBuffer.formatTo("{}:{:d}", filepath, ctx->line);
else outputBuffer.append(filepath);
}
break;
}
case "%f"_i: {
auto* funcName = IS_SET(config->log_flag, Flags::kFuncName)
? ctx->func_name
: nullptr;
if (funcName) outputBuffer.append(funcName);
break;
}
case "%e"_i: {
if (ctx->level < INT(Levels::kError) || ctx->err == 0) break;
outputBuffer.append(Util::getErrorInfo(ctx->err));
break;
}
case "%L"_i: {
auto* longLevelText = GET_LEVEL_TEXT(ctx->level);
if (longLevelText) outputBuffer.append(longLevelText);
break;
}
case "%l"_i: {
auto* shortLevelText = GET_LEVEL_TEXT(ctx->level, true);
if (shortLevelText) outputBuffer.append(shortLevelText);
break;
}
case "%v"_i: {
outputBuffer.append(ctx->text);
break;
}
case "%n"_i: {
if (config->log_name) outputBuffer.append(config->log_name);
break;
}
case "%c"_i: {
// only console support
if (outType != Appenders::kConsole) break;
// color start
outputBuffer.append(GET_ANSI_COLOR_BY_LEVEL(ctx->level));
break;
}
case "%C"_i: {
// only console support
if (outType != Appenders::kConsole) break;
// color end
outputBuffer.append("\033[0m");
break;
}
case "%%"_i: {
outputBuffer.push_back('%');
break;
}
default: {
outputBuffer.append(op);
}
}
// if the op str is last one
if (index + op.size() >= format_str.size()) { break; }
index += op.size();
// push_back until ch is '%'
while (index < format_str.size() && format_str[index] != '%')
outputBuffer.push_back(format_str[index++]);
if (index == format_str.size()) break;
}
}