This repository was archived by the owner on Aug 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLog.h
More file actions
executable file
·61 lines (49 loc) · 1.45 KB
/
Log.h
File metadata and controls
executable file
·61 lines (49 loc) · 1.45 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
#ifndef LOG_H_
#define LOG_H_
#pragma warning(disable:4996)
#include <windows.h>
#include <cstdio>
#include <clocale>
#include <ctime>
#include "Str.h"
class Log {
public:
static void Init(const char* s = "Debug Console") {
if (!GetStdHandle(STD_OUTPUT_HANDLE)) {
AllocConsole();
SetConsoleTitleA(s);
//SetConsoleCtrlHandler(ctrlHandler, true);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
freopen("CONIN$", "r", stdin);
}
setlocale(0, "");
}
static void Close() {
fclose(stdout);
fclose(stdin);
fclose(stderr);
FreeConsole();
}
static void d(const char* s) { color(9); printf("%s\n", str(curTime() + "[Debug]: " + s)); }
static void i(const char* s) { color(7); printf("%s\n", str(curTime() + "[INFO]: " + s)); }
static void w(const char* s) { color(6); printf("%s\n", str(curTime() + "[WARN]: " + s)); }
static void e(const char* s) { color(12); printf("%s\n", str(curTime() + "[ERR]: " + s)); }
private:
// disable instantiating a Log() object.
Log() {}
static const char* curTime() {
static char buffer[16];
long long t = time(nullptr);
strftime(buffer, 16, "[%T]", localtime(&t));
return buffer;
}
static void color(const char& fg, const char& bg = 0) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), bg << 4 | fg);
}
static int ctrlHandler(DWORD t) {
if (t == 0) printf("^C\n");
return 1;
}
};
#endif // !LOG_H_