-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.h
123 lines (104 loc) · 4.13 KB
/
Console.h
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
#pragma once
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define LOG_INFO 0
#define LOG_WARNING 1
#define LOG_ERROR 2
int LOGLEVEL = 1;
namespace Console {
void RedirectStdout()
{
// hide the main window
if (!AllocConsole()) {
MessageBoxA(NULL, "AllocConsole() failed!", "Error", MB_ICONERROR);
return;
}
// std::cout, std::clog, std::cerr, std::cin
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();
// std::wcout, std::wclog, std::wcerr, std::wcin
HANDLE hConOut = CreateFileA(("CONOUT$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hConIn = CreateFileA(("CONIN$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
SetStdHandle(STD_OUTPUT_HANDLE, hConOut);
SetStdHandle(STD_ERROR_HANDLE, hConOut);
SetStdHandle(STD_INPUT_HANDLE, hConIn);
std::wcout.clear();
std::wclog.clear();
std::wcerr.clear();
std::wcin.clear();
}
void PrintRainbow(const std::string& text) {
int colors[] = {
FOREGROUND_RED | FOREGROUND_INTENSITY, // Red
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, // Yellow
FOREGROUND_GREEN | FOREGROUND_INTENSITY, // Green
FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, // Cyan
FOREGROUND_BLUE | FOREGROUND_INTENSITY, // Blue
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY // Magenta
};
int numColors = sizeof(colors) / sizeof(colors[0]);
for (size_t i = 0; i < text.size(); ++i) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[i % numColors]);
std::cout << text[i];
}
std::cout << std::endl;
// Reset console color
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // White
}
void Print(const std::string& message, int level = LOG_INFO) {
if (level < LOGLEVEL)
return;
std::string prefix;
switch (level) {
case LOG_INFO:
prefix = "[INFO] ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // White
break;
case LOG_WARNING:
prefix = "[WARNING] ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); // Yellow
break;
case LOG_ERROR:
prefix = "[ERROR] ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); // Red
break;
default:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); // Green
prefix = "[INFO] ";
break;
}
// Print message with color
std::cout << prefix << message << std::endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // White
}
void CycleLogLevel() {
LOGLEVEL = (LOGLEVEL + 1) % 3;
std::cout << "Log level set to " << LOGLEVEL << ": 0 INFO - 1 WARN - 2 ERR"<< std::endl;
}
void HideConsole() {
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
void ShowConsole() {
ShowWindow(GetConsoleWindow(), SW_SHOW);
}
void ToggleConsole() {
if (IsWindowVisible(GetConsoleWindow())) {
HideConsole();
}
else {
ShowConsole();
}
}
}