Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.cpp
More file actions
Latest commit
89 lines (78 loc) · 2.46 KB
/
Copy pathlogging.cpp
File metadata and controls
89 lines (78 loc) · 2.46 KB
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
#include<string>
#include<stdio.h>
#include<atomic>
#include<experimental/source_location>
usingnamespacestd::experimental::fundamentals_v2;
#include"logging.h"
timespec firsttime;
static std::atomic<LogLevel> currentLogLevel{LogLevel::Info};
intFUNCTION_DEPTH = 0;
std::string elapsedms() __attribute__((no_instrument_function));
std::string elapsedms()
{
structtimespec up;
clock_gettime(CLOCK_BOOTTIME, &up);
if(firsttime.tv_nsec == 0)
firsttime = up;
double now = double(up.tv_sec-firsttime.tv_sec) + double(up.tv_nsec-firsttime.tv_nsec) / 1.0e9;
returnstd::to_string(now)+"s";
}
// "\033[33m[Warning]\033[0m ";
#defineRED"\033[31m"
#defineYELLOW"\033[32m"
#defineCYAN"\033[36m"
#definelogtext"%s[ %5s ] %12s %23s %23s\033[0m %s (%5d) %s"
#definelogCR"%s"
voidSetLogLevel(LogLevel level)
{
currentLogLevel.store(level);
}
LogLevel GetLogLevel()
{
return currentLogLevel.load();
}
staticconstchar* labelForLevel(LogLevel level)
{
switch(level) {
case LogLevel::Error: return"[ ERROR ]";
case LogLevel::Warning: return"[WARNING]";
case LogLevel::Info: return"[ INFO ]";
case LogLevel::Debug: return"[ DEBUG ]";
case LogLevel::Trace: return"[ TRACE ]";
}
return"[??????]";
}
staticconstchar* colorForLevel(LogLevel level)
{
switch(level) {
case LogLevel::Error: return"\033[31m"; // red
case LogLevel::Warning: return"\033[33m"; // yellow
case LogLevel::Info: return"\033[32m"; // green
case LogLevel::Debug: return""; // default
case LogLevel::Trace: return"\033[36m"; // cyan
}
return"";
}
voidLogImpl(LogLevel level, std::string intro, TBytes bytes, bool crlf, const source_location &loc)
{
std::stringstream msg;
msg << intro;
for (auto byt : bytes)
msg << std::hex << std::setfill('0') << std::setw(2)
<< std::uppercase << static_cast<int>(byt) << '';
constchar* color = colorForLevel(level);
constchar* label = labelForLevel(level);
printf("%s%s\033[0m %14s \033[32m%13s %s\033[0m (%5d) %s%s",
color, label, elapsedms().c_str(),
loc.file_name(), loc.function_name(), loc.line(),
msg.str().c_str(), crlf ? "\n" : "");
}
voidLogImpl(LogLevel level, std::string message, const source_location &loc)
{
// forward to the full overload with an empty payload
LogImpl(level,
std::move(message),
TBytes{} /*empty bytes*/,
true/*default crlf*/,
loc);
}