-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
67 lines (64 loc) · 2.79 KB
/
Logger.cpp
File metadata and controls
67 lines (64 loc) · 2.79 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
#include "Logger.h"
const Color Color::red(FOREGROUND_RED);
const Color Color::green(FOREGROUND_GREEN);
const Color Color::blue(FOREGROUND_BLUE);
const Color Color::yellow(FOREGROUND_RED | FOREGROUND_GREEN);
const Color Color::purple(FOREGROUND_RED | FOREGROUND_BLUE);
const Color Color::cyan(FOREGROUND_GREEN | FOREGROUND_BLUE);
const Color Color::white(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
const Color Color::gray(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
const Color Color::lightRed(FOREGROUND_RED | FOREGROUND_INTENSITY);
const Color Color::lightGreen(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
const Color Color::lightBlue(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
const Color Color::lightYellow(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
const Color Color::lightPurple(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
const Color Color::lightCyan(FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
const Level Level::LEVEL_DEBUG(TXT("DEBUG"), Color::lightCyan);
const Level Level::LEVEL_INFO(TXT("INFO"), Color::lightGreen);
const Level Level::LEVEL_WARN(TXT("WARN"), Color::yellow);
const Level Level::LEVEL_ERROR(TXT("ERROR"), Color::lightRed);
void Log(CONST Level level, __PCTCH szSource, __PCTCH szFormat, ...) {
va_list args;
va_start(args, szFormat);
TXTCHAR msg[MAX_LOG_LEN];
vsntprintf(msg, MAX_LOG_LEN, szFormat, args);
va_end(args);
tstring szMsg(msg);
time_t currentTime = time(nullptr);
tm localTime;
localtime_s(&localTime, ¤tTime);
tcout << Color::gray << TXT("[")
<< Color::white << std::setw(2) << std::setfill(TXT('0')) << localTime.tm_hour << std::setfill(TXT(' '))
<< Color::gray << TXT(":")
<< Color::white << std::setw(2) << std::setfill(TXT('0')) << localTime.tm_min << std::setfill(TXT(' '))
<< Color::gray << TXT(":")
<< Color::white << std::setw(2) << std::setfill(TXT('0')) << localTime.tm_sec << std::setfill(TXT(' '))
<< Color::gray << TXT("] ")
<< Color::white << szSource << std::setw(15 - tcslen(szSource))
<< Color::gray << TXT(" - ") << level << Color::gray << TXT(" - ");
tstring::size_type pos = 0;
if (szMsg.size() >= 1 && szMsg[0] != COLOR_FORMAT_CHAR) {
szMsg.insert(0, 1, DEFAULT_COLOR);
szMsg.insert(0, 1, COLOR_FORMAT_CHAR);
}
while ((pos = szMsg.find(COLOR_FORMAT_CHAR, pos)) != tstring::npos) {
if (pos + 1 < szMsg.length() && isalnum(szMsg[pos + 1])) {
tstring::size_type next_pos = szMsg.find(COLOR_FORMAT_CHAR, pos + 1);
tstring sub;
if (next_pos == tstring::npos) {
sub = szMsg.substr(pos + 2);
} else {
sub = szMsg.substr(pos + 2, next_pos - pos - 2);
}
auto itr = charToColor.find(szMsg[pos + 1]);
if (itr == charToColor.end()) {
tcout << sub;
} else {
tcout << itr->second << sub;
}
pos = next_pos;
} else {
pos++;
}
}
}