-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.h
More file actions
106 lines (93 loc) · 3.05 KB
/
Log.h
File metadata and controls
106 lines (93 loc) · 3.05 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <windows.h>
#include <cstdio>
#include <ctime>
#include <string>
namespace tlog
{
namespace detail
{
inline const char* RESET = "\x1b[0m";
inline const char* GRAY = "\x1b[90m";
inline const char* GREEN = "\x1b[32m";
inline const char* LIGHTMAGENTA = "\x1b[95m";
inline const char* LIGHTYELLOW = "\x1b[93m";
inline const char* LIGHTRED = "\x1b[91m";
inline const char* RED = "\x1b[31m";
inline void EnableAnsi()
{
static bool done = false;
if (done)
return;
done = true;
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
if (GetConsoleMode(out, &mode))
SetConsoleMode(out, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
inline std::string Timestamp()
{
std::time_t now = std::time(nullptr);
std::tm tm = {};
localtime_s(&tm, &now);
char buf[16] = {0};
std::strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
return std::string(GRAY) + buf + RESET;
}
inline std::string Highlight(const std::string& text)
{
std::string out;
for (size_t i = 0; i < text.size(); ++i)
{
if (text[i] == '[')
{
const size_t close = text.find(']', i);
if (close != std::string::npos)
{
out += GRAY;
out += '[';
out += RESET;
out += text.substr(i + 1, close - i - 1);
out += GRAY;
out += ']';
out += RESET;
i = close;
continue;
}
}
out += text[i];
}
return out;
}
inline void Emit(const char* tagColor, const char* tag,
const std::string& text, const char* sep)
{
EnableAnsi();
std::printf("%s %s%s%s %s%s%s%s\n",
Timestamp().c_str(),
tagColor, tag, RESET,
GRAY, sep, RESET,
Highlight(text).c_str());
}
}
inline void success(const std::string& text, const char* sep = " ")
{
detail::Emit(detail::GREEN, "YES", text, sep);
}
inline void info(const std::string& text, const char* sep = " ")
{
detail::Emit(detail::LIGHTMAGENTA, "INF", text, sep);
}
inline void debug(const std::string& text, const char* sep = " ")
{
detail::Emit(detail::LIGHTYELLOW, "DBG", text, sep);
}
inline void error(const std::string& text, const char* sep = " ")
{
detail::Emit(detail::LIGHTRED, "ERR", text, sep);
}
inline void fatal(const std::string& text, const char* sep = " ")
{
detail::Emit(detail::RED, "FTL", text, sep);
}
}