-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLogging.cpp
More file actions
243 lines (205 loc) · 6.34 KB
/
Copy pathLogging.cpp
File metadata and controls
243 lines (205 loc) · 6.34 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
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
#include "hacklib/Logging.h"
#include "hacklib/Main.h"
#include <chrono>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
static constexpr auto ANSIESC_RESET = "\x1b[0m";
static constexpr auto ANSIESC_COLOR_RED = "\x1b[31m";
static constexpr auto ANSIESC_COLOR_GREEN = "\x1b[32m";
static constexpr auto ANSIESC_COLOR_YELLOW = "\x1b[33m";
static constexpr auto ANSIESC_COLOR_BLUE = "\x1b[34m";
static constexpr auto ANSIESC_COLOR_PURPLE = "\x1b[35m";
static constexpr auto ANSIESC_COLOR_CYAN = "\x1b[36m";
static constexpr auto ANSIESC_COLOR_WHITE = "\x1b[37m";
static constexpr auto ANSIESC_BG_COLOR_RED = "\x1b[41m";
static constexpr auto ANSIESC_BG_COLOR_GREEN = "\x1b[42m";
static constexpr auto ANSIESC_BG_COLOR_YELLOW = "\x1b[43m";
static constexpr auto ANSIESC_BG_COLOR_BLUE = "\x1b[44m";
static constexpr auto ANSIESC_BG_COLOR_PURPLE = "\x1b[45m";
static constexpr auto ANSIESC_BG_COLOR_CYAN = "\x1b[46m";
static constexpr auto ANSIESC_BG_COLOR_WHITE = "\x1b[47m";
static constexpr auto ANSIESC_COLOR_8BIT_PREFIX = "\x1b[38;5;";
static constexpr auto ANSIESC_BG_COLOR_8BIT_PREFIX = "\x1b[48;5;";
hl::LogConfig& getGlobalConfig()
{
static hl::LogConfig cfg;
return cfg;
}
class FormatStr
{
public:
FormatStr(const char* format, va_list vl)
{
va_list vl_copy;
va_copy(vl_copy, vl);
const int size = vsnprintf(nullptr, 0, format, vl);
m_str = new char[size + 1];
vsnprintf(m_str, size + 1, format, vl_copy);
va_end(vl_copy);
}
FormatStr(const FormatStr&) = delete;
FormatStr& operator=(const FormatStr&) = delete;
FormatStr(FormatStr&&) = delete;
FormatStr& operator=(FormatStr&&) = delete;
~FormatStr() { delete[] m_str; }
[[nodiscard]] const char* str() const { return m_str; }
private:
char* m_str;
};
static std::string GetTimeStr()
{
char strtime[256];
auto timept = std::chrono::system_clock::now();
auto timetp = std::chrono::system_clock::to_time_t(timept);
strftime(strtime, sizeof(strtime), "%X", std::localtime(&timetp));
return strtime;
}
static std::string GetCodeStr(const char* file, const char* func, int line)
{
std::string fileStr = file;
fileStr = fileStr.substr(fileStr.find_last_of('\\') + 1);
fileStr = fileStr.substr(fileStr.find_last_of('/') + 1);
std::string str;
str += '[';
str += fileStr;
str += ':';
str += std::to_string(line);
str += '|';
str += func;
str += ']';
return str;
}
static void LogString(const std::string& str, bool raw, hl::Color color)
{
const auto& cfg = getGlobalConfig();
std::string logStr;
if (!raw && cfg.logTime)
{
logStr = GetTimeStr() + " " + str;
}
else
{
logStr = str;
}
if (cfg.logToFile)
{
std::ofstream logfile(cfg.fileName, std::ios::out | std::ios::app);
logfile << logStr;
}
if (cfg.logFunc)
{
std::string colorPrefix;
auto fgColor = color & hl::Color::MaskFG;
auto bgColor = color & hl::Color::MaskBG;
bool requiresReset = false;
if (fgColor != hl::Color::Default)
{
colorPrefix += ANSIESC_COLOR_8BIT_PREFIX;
auto raw = static_cast<uint32_t>(fgColor);
if (raw < 0x10)
raw -= 1;
colorPrefix += std::to_string(raw) + 'm';
requiresReset = true;
}
if (bgColor != hl::Color::Default)
{
colorPrefix += ANSIESC_BG_COLOR_8BIT_PREFIX;
auto raw = static_cast<uint32_t>(bgColor) >> 8;
if (raw < 0x10)
raw -= 1;
colorPrefix += std::to_string(raw) + 'm';
// Avoid buggy colors on line breaks.
std::string sanitizedLogStr;
size_t lastPos = 0;
while (lastPos < logStr.size())
{
size_t pos = logStr.find('\n', lastPos);
if (pos == std::string::npos)
{
sanitizedLogStr += colorPrefix + logStr.substr(lastPos);
requiresReset = true;
break;
}
sanitizedLogStr += colorPrefix + logStr.substr(lastPos, pos - lastPos) + ANSIESC_RESET + '\n';
requiresReset = false;
lastPos = pos + 1;
}
logStr = std::move(sanitizedLogStr);
}
else
{
logStr = colorPrefix + logStr;
}
if (requiresReset)
{
logStr += ANSIESC_RESET;
}
cfg.logFunc(logStr);
}
}
void hl::DefaultLogFunc(const std::string& str)
{
fputs(str.c_str(), stdout);
}
void hl::ConfigLog(const LogConfig& config)
{
auto& cfg = getGlobalConfig();
cfg = config;
if (cfg.fileName == "")
{
cfg.fileName = hl::GetCurrentModulePath() + "_log.txt";
}
}
void hl::LogDebug(hl::Color color, const char* file, const char* func, int line, const char* format, ...)
{
va_list vl;
va_start(vl, format);
const FormatStr str(format, vl);
LogString(GetCodeStr(file, func, line) + " " + str.str(), false, color);
va_end(vl);
}
void hl::LogError(hl::Color color, const char* file, const char* func, int line, const char* format, ...)
{
va_list vl;
va_start(vl, format);
const FormatStr str(format, vl);
LogString(GetCodeStr(file, func, line) + " ERROR: " + str.str(), false, color);
va_end(vl);
}
void hl::LogError(hl::Color color, const char* format, ...)
{
va_list vl;
va_start(vl, format);
const FormatStr str(format, vl);
LogString(std::string("ERROR: ") + str.str(), false, color);
va_end(vl);
}
void hl::LogRaw(hl::Color color, const char* format, ...)
{
va_list vl;
va_start(vl, format);
const FormatStr str(format, vl);
LogString(str.str(), true, color);
va_end(vl);
}
#ifdef WIN32
std::string hl::ErrorString(int systemCode)
{
DWORD code = static_cast<DWORD>(systemCode);
LPVOID buf = NULL;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL);
std::string out = (char*)buf;
LocalFree(buf);
return out;
}
#else
std::string hl::ErrorString(int systemCode)
{
std::string out = std::strerror(systemCode);
return out;
}
#endif