-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlogger.cpp
More file actions
290 lines (228 loc) · 6.14 KB
/
logger.cpp
File metadata and controls
290 lines (228 loc) · 6.14 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
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
#include "pch.h"
#include "logger.h"
#include "timeHandler.h"
#include <format>
#include <iterator>
void vsid::Logger::initialize()
{
if (running) return;
std::error_code ec;
// get dll path
char pathBuf[MAX_PATH];
GetModuleFileNameA((HINSTANCE)&__ImageBase, pathBuf, MAX_PATH);
std::filesystem::path dllPath = pathBuf;
logFilePath = dllPath.parent_path() / "logs" / "vsid.log";
std::filesystem::create_directory(logFilePath.parent_path(), ec);
if (!ec)
{
fileLockMutex = CreateMutexA(NULL, FALSE, "Local\\vsid_log_mutex");
if (fileLockMutex != NULL && GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(fileLockMutex);
fileLockMutex = NULL;
}
else if (fileLockMutex != NULL)
{
rotateLogs(); // rotate logs on startup to archive previous session logs
logFile.open(logFilePath, std::ios::app);
}
}
// create thread
running = true;
worker = std::thread(workerThread);
if (!logFile.is_open())
{
log(LogLevel::Error, "File logging disabled: Insufficient permissions to create or access log directory.");
}
else
log(LogLevel::Info, "Logger initialized successfully (File logging active).");
}
void vsid::Logger::shutdown() {
if (!running) return;
// signal thread stopping
running = false;
cv.notify_all();
// wait on closing thread
if (worker.joinable()) worker.join();
if (logFile.is_open())
{
logFile.flush();
logFile.close();
}
if(fileLockMutex)
{
CloseHandle(fileLockMutex);
fileLockMutex = NULL;
}
if (newConsole)
{
if (consoleOut)
{
fclose(consoleOut);
consoleOut = nullptr;
}
FreeConsole();
newConsole = false;
}
}
void vsid::Logger::toggleConsole()
{
std::lock_guard lock(consoleMutex);
if (!newConsole)
{
if (AllocConsole())
{
newConsole = true;
// disable close button to prevent ES closing
HWND hwnd = GetConsoleWindow();
if (hwnd != NULL)
{
HMENU hMenu = GetSystemMenu(hwnd, FALSE);
if (hMenu != NULL)
{
DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
SetWindowTextA(hwnd, "Console Logger");
}
freopen_s(&consoleOut, "CONOUT$", "w", stdout);
log(LogLevel::Info, "Console output enabled.");
}
}
else
{
log(LogLevel::Info, "Console output disabled.");
if (consoleOut)
{
fclose(consoleOut);
consoleOut = nullptr;
}
FreeConsole();
newConsole = false;
}
}
std::vector<std::string> vsid::Logger::fetchEsMsgs()
{
std::lock_guard lock(esMutex);
std::vector<std::string> msgs;
while (!esQueue.empty())
{
msgs.push_back(std::move(esQueue.front()));
esQueue.pop();
}
return msgs;
}
void vsid::Logger::log(LogLevel level, const std::string_view& msg, std::optional<DebugLevel> debugLevel, bool devOnly)
{
if (!running) return;
if (devOnly && !logDevOnly) return;
{
std::lock_guard lock(bgMutex);
bgQueue.push(LogMessage{ level, std::string(msg), debugLevel });
}
cv.notify_all();
}
void vsid::Logger::workerThread()
{
while (running || !bgQueue.empty())
{
LogMessage msg;
// wait if queue is not empty or if worker is not running
{
std::unique_lock lock(bgMutex);
cv.wait(lock, [] { return !bgQueue.empty() || !running; });
if (!running && bgQueue.empty()) break;
msg = std::move(bgQueue.front());
bgQueue.pop();
}
// write log file
if (logFile.is_open())
{
// format file log message
std::string fout = std::format("[{:%Y-%m-%d %H:%M:%S}] [{}]",
vsid::time::getUtcNow(), logLvlToString(msg.level));
if (msg.level == LogLevel::Debug && msg.debugLevel.has_value())
std::format_to(std::back_inserter(fout), " [{}]", debugLvlToString(msg.debugLevel.value()));
std::format_to(std::back_inserter(fout), " {}\n", msg.msg);
logFile << fout;
if (logFile.fail()) // revive stream if log file was opened on the disk
logFile.clear();
if(msg.level >= LogLevel::Warning)
logFile.flush();
if (logFile.tellp() >= 10 * 1024 * 1024) rotateLogs(); // rotate if log file exceeds 10 MB
}
// console output
{
std::lock_guard lock(consoleMutex);
if (consoleOut)
{
// format console log message
std::string out = std::format("[{}] [{}]",
vsid::time::toTimeString(vsid::time::getUtcNow()),
logLvlToString(msg.level));
if (msg.level == LogLevel::Debug && msg.debugLevel.has_value())
{
out += std::format(" [{}]", debugLvlToString(msg.debugLevel.value()));
}
out += std::format(" {}\n", msg.msg);
fputs(out.c_str(), consoleOut);
fflush(consoleOut);
}
}
// push to ES queue
if(msg.level > LogLevel::Debug) {
std::lock_guard lock(esMutex);
esQueue.push(std::format("[{}] {}", logLvlToString(msg.level), msg.msg));
}
}
}
void vsid::Logger::rotateLogs()
{
if (logFile.is_open())
{
logFile.flush();
logFile.close();
}
std::error_code ec;
for (int i = 3; i >= 1; --i)
{
std::filesystem::path src = logFilePath.parent_path() / std::format("{}.{}{}", logFilePath.stem().string(), i, logFilePath.extension().string());
std::filesystem::path dst = logFilePath.parent_path() / std::format("{}.{}{}", logFilePath.stem().string(), i + 1, logFilePath.extension().string());
if (std::filesystem::exists(src, ec))
{
std::filesystem::remove(dst, ec);
std::filesystem::rename(src, dst, ec);
}
}
std::filesystem::path firstLog = logFilePath.parent_path() / std::format("{}.1{}", logFilePath.stem().string(), logFilePath.extension().string());
if (std::filesystem::exists(logFilePath, ec))
{
std::filesystem::remove(firstLog, ec);
std::filesystem::rename(logFilePath, firstLog, ec);
}
logFile.open(logFilePath, std::ios::trunc);
}
void vsid::Logger::panicFlush()
{
if (!running) return;
if (logFile.is_open())
logFile.flush();
if (bgMutex.try_lock())
{
while (!bgQueue.empty())
{
LogMessage& msg = bgQueue.front();
if (logFile.is_open())
{
std::string fout = std::format("[{:%Y-%m-%d %H:%M:%S}] [CRASH DUMP] [{}]",
vsid::time::getUtcNow(), logLvlToString(msg.level));
if (msg.level == LogLevel::Debug && msg.debugLevel.has_value())
fout += std::format(" [{}]", debugLvlToString(msg.debugLevel.value()));
fout += std::format(" {}\n", msg.msg);
logFile << fout;
}
bgQueue.pop();
}
if (logFile.is_open()) logFile.flush();
bgMutex.unlock();
}
}