-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.h
128 lines (110 loc) · 3.04 KB
/
Logger.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
124
125
126
127
128
#pragma once
#include <Windows.h>
#include <ctime>
#include <tchar.h>
#include <string>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#define MAX_LOG_LEN 300
#define COLOR_FORMAT_CHAR '~'
#define DEFAULT_COLOR 'g'
#ifndef LOG_LEVEL_ERROR
#define LOG_LEVEL_ERROR
#endif
#if defined(UNICODE) && !defined(FORCE_MULTIBYTE)
#define tostream std::wostream
#define tstring std::wstring
#define tcout std::wcout
#define vsntprintf _vsnwprintf_s
#define tcslen wcslen
#define TXT(str) L ## str
typedef LPCWCH __PCTCH;
typedef WCHAR TXTCHAR;
#else
#define tostream std::ostream
#define tstring std::string
#define tcout std::cout
#define vsntprintf vsnprintf
#define tcslen strlen
#define TXT(str) str
typedef LPCCH __PCTCH;
typedef CHAR TXTCHAR;
#endif
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
class Color {
private:
WORD color;
public:
Color(int color) : color(color) {}
friend tostream& operator << (tostream& os, const Color& c) {
SetConsoleTextAttribute(hConsole, c.color);
return os;
}
static const Color red, green, blue, yellow, purple, cyan, white, gray,
lightRed, lightGreen, lightBlue, lightYellow, lightPurple, lightCyan;
};
class Level {
private:
tstring name;
Color color;
public:
Level(tstring name, Color color) : name(name), color(color) {}
friend tostream& operator << (tostream& os, const Level& level) {
return os << level.color << level.name << std::setw(8 - level.name.length()) << Color::gray;
}
static const Level LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR;
};
static const std::map<TCHAR, Color> charToColor{
{TXT('a'), Color::red},
{TXT('b'), Color::green},
{TXT('c'), Color::blue},
{TXT('d'), Color::yellow},
{TXT('e'), Color::purple},
{TXT('f'), Color::cyan},
{TXT('g'), Color::white},
{TXT('h'), Color::gray},
{TXT('1'), Color::lightRed},
{TXT('2'), Color::lightGreen},
{TXT('3'), Color::lightBlue},
{TXT('4'), Color::lightYellow},
{TXT('5'), Color::lightPurple},
{TXT('6'), Color::lightCyan},
};
void Log(CONST Level level, __PCTCH szSource, __PCTCH szFormat, ...);
#ifdef LOG_LEVEL_DEBUG
#define LOG_DEBUG_ENABLED
#define LOG_INFO_ENABLED
#define LOG_WARN_ENABLED
#define LOG_ERROR_ENABLED
#elif defined(LOG_LEVEL_INFO)
#define LOG_INFO_ENABLED
#define LOG_WARN_ENABLED
#define LOG_ERROR_ENABLED
#elif defined(LOG_LEVEL_WARN)
#define LOG_WARN_ENABLED
#define LOG_ERROR_ENABLED
#elif defined(LOG_LEVEL_ERROR)
#define LOG_ERROR_ENABLED
#endif
#ifdef LOG_DEBUG_ENABLED
#define LOG_DEBUG(source, fmt, ...) Log(Level::LEVEL_DEBUG, source, fmt, ##__VA_ARGS__);
#else
#define LOG_DEBUG(source, fmt, ...)
#endif
#ifdef LOG_INFO_ENABLED
#define LOG_INFO(source, fmt, ...) Log(Level::LEVEL_INFO, source, fmt, ##__VA_ARGS__);
#else
#define LOG_INFO(source, fmt, ...)
#endif
#ifdef LOG_WARN_ENABLED
#define LOG_WARN(source, fmt, ...) Log(Level::LEVEL_WARN, source, fmt, ##__VA_ARGS__);
#else
#define LOG_WARN(source, fmt, ...)
#endif
#ifdef LOG_ERROR_ENABLED
#define LOG_ERROR(source, fmt, ...) Log(Level::LEVEL_ERROR, source, fmt, ##__VA_ARGS__);
#else
#define LOG_ERROR(source, fmt, ...)
#endif