-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDebugConsole.cpp
63 lines (51 loc) · 1.61 KB
/
DebugConsole.cpp
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
#include "DebugConsole.h"
void DebugConsole::makeConsole() {
AllocConsole();
FILE* consoleOut;
freopen_s(&consoleOut, "CONIN$", "r", stdin);
}
const char* DebugConsole::getTime() {
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm now_tm;
localtime_s(&now_tm, &now_c);
std::stringstream ss;
ss << std::put_time(&now_tm, "[%H:%M:%S]");
static std::string timeStr;
timeStr = ss.str();
return timeStr.c_str();
}
void DebugConsole::abruptEnd() {
}
void DebugConsole::sendMsg(const char* msg, MESSAGE_TYPE m) {
std::string time = DebugConsole::getTime();
std::string full = time + " " + msg;
if (DebugConsole::doOutput) {
DebugConsole::out = DebugConsole::out + full + "\n";
}
if (!DebugConsole::enabled) {
return; // ignore if not enabled
}
WORD cur = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
if ((int)m == 1) {
cur = FOREGROUND_RED;
} else if((int)m == 2) {
cur = FOREGROUND_GREEN;
} else if ((int)m == 3) {
cur = FOREGROUND_BLUE;
}
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole != INVALID_HANDLE_VALUE) {
SetConsoleTextAttribute(hConsole, cur);
DWORD written;
WriteConsoleA(hConsole, full.c_str(), strlen(full.c_str()), &written, nullptr);
WriteConsoleA(hConsole, "\n", 1, &written, nullptr);
}
}
void DebugConsole::writeOutput() {
std::ofstream outFile("output.log");
if (outFile) {
outFile << DebugConsole::out << std::endl;
}
outFile.close();
}