Repository navigation
Expand file tree
/
Copy pathConsole.cpp
More file actions
67 lines (59 loc) · 1.86 KB
/
Copy pathConsole.cpp
File metadata and controls
67 lines (59 loc) · 1.86 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 "Console.h"
#include "Logger.h"
std::atomic<bool> Console::s_Running(false);
std::thread Console::s_LogThread;
void Console::Initialize() {
AllocConsole();
FILE* fDummy;
freopen_s(&fDummy, "CONOUT$", "w", stdout);
freopen_s(&fDummy, "CONOUT$", "w", stderr);
freopen_s(&fDummy, "CONIN$", "r", stdin);
std::cout.clear();
std::clog.clear();
std::cerr.clear();
std::cin.clear();
SetConsoleTitleW(L"MapleC Log Console");
s_Running = true;
s_LogThread = std::thread(ReadLogFile);
}
void Console::Shutdown() {
s_Running = false;
if (s_LogThread.joinable()) {
s_LogThread.join();
}
FreeConsole();
}
void Console::ReadLogFile() {
std::ifstream file("MapleCLogs.txt", std::ios::in);
if (!file.is_open()) {
std::cerr << "Failed to open MapleCLogs.txt" << std::endl;
return;
}
std::string line;
while (s_Running) {
if (std::getline(file, line)) {
if (line.find("[INFO]") != std::string::npos) {
SetConsoleColor(7); // White
}
else if (line.find("[WARNING]") != std::string::npos) {
SetConsoleColor(14); // Yellow
}
else if (line.find("[ERROR]") != std::string::npos || line.find("[CRITICAL]") != std::string::npos) {
SetConsoleColor(12); // Red
}
else {
SetConsoleColor(7); // Default to white
}
std::cout << line << std::endl;
}
else {
file.clear();
file.seekg(0, std::ios::end);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
file.close();
}
void Console::SetConsoleColor(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}