-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog.h
215 lines (179 loc) · 5.42 KB
/
Log.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once
#include <Windows.h>
#include <stdio.h>
//#define VERBOSE // Useless TODO and FIXME marcos superceeded by vs tasklist
#define TODO_CLICKABLE
#define FIXME_CLICKABLE
#ifndef _MAKE_STRING
#define _MAKE_STRING(x) #x
#endif // !_MAKE_STRING
#ifndef MAKE_STRING
#define MAKE_STRING(x) _MAKE_STRING(x)
#endif // !MAKE_STRING
// Move todo & fixme macros out of Log.h!!
#ifndef TODO
#ifndef TODO_CLICKABLE
#define TODO(x) __pragma(message("TODO: " MAKE_STRING(x) " -> " __FILE__ "@" MAKE_STRING(__LINE__)))
#else
#define TODO(x) __pragma(message(__FILE__ "(" MAKE_STRING(__LINE__) "): TODO: " MAKE_STRING(x)))
#endif // !TODO_CLICKABLE
#endif // !TODO
#ifndef FIXME
#ifndef FIXME_CLICKABLE
#define FIXME(x) __pragma(message("FIXME: " MAKE_STRING(x) " -> " __FILE__ "@" MAKE_STRING(__LINE__)))
#else
#define FIXME(x) __pragma(message(__FILE__ "(" MAKE_STRING(__LINE__) "): FIXME: " MAKE_STRING(x)))
#endif // !TODO_CLICKABLE
#endif // !TODO
#if defined(_DEBUG) || defined(VERBOSE)
#define LOG_OFFSETS
#define LOG(fmt, ...) Log::Log(__FUNCSIG__, fmt, __VA_ARGS__)
#define WARN(fmt, ...) Log::Warn(__FUNCSIG__, fmt, __VA_ARGS__)
#define LERROR(fmt, ...) Log::Error(__FUNCSIG__, fmt, __VA_ARGS__)
#else
#define LOG(fmt, ...) ((void)0)
#define WARN(fmt, ...) ((void)0)
#define LERROR(fmt, ...) ((void)0)
#endif
#ifdef LOG_OFFSETS
#define LOG_OFFSET(name, offset) Log::LogOffset(name, offset)
#else
#define LOG_OFFSET(name, offset)
#endif
typedef struct _USER_NOTIFY
{
HANDLE hCallerEvent;
CONST CHAR* szMsg;
CONST CHAR* szCompletedMsg;
} USER_NOTIFY, * PUSER_NOTIFY;
enum ConsoleColors
{
BLACK = 0x0,
BLUE = 0x1,
GREEN = 0x2,
AQUA = 0x3,
RED = 0x4,
PURPLE = 0x5,
YELLOW = 0x6,
WHITE = 0x7,
GRAY = 0x8,
LIGHT_BLUE = 0x9,
LIGHT_GREEN = 0xA,
LIGHT_AQUA = 0xB,
LIGHT_RED = 0xC,
LIGHT_PURPLE = 0xD,
LIGHT_YELLOW = 0xE,
PURE_WHITE = 0xF
};
void WINAPI UserNotify(PUSER_NOTIFY pParams);
class Log
{
public:
explicit Log(const char* szFunction, const char* szFmt, ...)
{
va_list Args;
va_start(Args, szFmt);
Write(stdout, LIGHT_GREEN, BLACK, szFunction, szFmt, Args);
va_end(Args);
}
static void Warn(const char* szFunction, const char* szFmt, ...)
{
va_list Args;
va_start(Args, szFmt);
Write(stdout, LIGHT_YELLOW, BLACK, szFunction, szFmt, Args);
va_end(Args);
}
static void Error(const char* szFunction, const char* szFmt, ...)
{
va_list Args;
va_start(Args, szFmt);
Write(stderr, LIGHT_RED, BLACK, szFunction, szFmt, Args);
va_end(Args);
}
static void Write(FILE* pStream, enum ConsoleColors Foreground, enum ConsoleColors Background, const char* szFunction, const char* szFmt, va_list Args)
{
enum ConsoleColors PreviousForeground;
enum ConsoleColors PreviousBackground;
GetConsoleColors(PreviousForeground, PreviousBackground);
fprintf(pStream, "[%s]: ", szFunction);
SetConsoleColors(Foreground, Background);
vfprintf(pStream, szFmt, Args);
SetConsoleColors(PreviousForeground, PreviousBackground);
}
static void AttachConsole(const wchar_t* szConsoleName)
{
if (!AllocConsole())
return;
SetConsoleTitleW(szConsoleName);
freopen_s(&pStdout, "CON", "w", stdout);
freopen_s(&pStderr, "CON", "w", stderr);
}
static inline void DetachConsole(void)
{
fclose(pStdout);
fclose(pStderr);
FreeConsole();
}
static void SetConsoleColors(enum ConsoleColors Foreground, enum ConsoleColors Background)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (csbi.wAttributes & 0xFF00) | Foreground | (Background << 4));
}
static void GetConsoleColors(enum ConsoleColors& Foreground, enum ConsoleColors& Background)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
Foreground = (ConsoleColors)(csbi.wAttributes & 0x0F);
Background = (ConsoleColors)((csbi.wAttributes & 0xF0) >> 4);
}
static void GotoXY(SHORT x, SHORT y)
{
COORD CursorPosition = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
}
static void AnimateWait(const char* szIn, HANDLE hWait, DWORD dwTickrate)
{
const static char animations[4] = { '|', '/', '-', '\\' };
INT iSequence = 0;
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD now;
printf(szIn);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
now = csbi.dwCursorPosition;
do {
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
now = csbi.dwCursorPosition;
printf("%c", animations[iSequence++]);
iSequence %= ARRAYSIZE(animations);
GotoXY(now.X, now.Y);
Sleep(dwTickrate);
} while (WaitForSingleObject(hWait, 0) != WAIT_OBJECT_0);
printf("\n");
}
static void AnimatePrint(const char* szIn, DWORD dwTickrate)
{
const static char animations[4] = { '|', '/', '-', '\\' };
INT iSequence = 0;
SIZE_T iLength = strlen(szIn);
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD now;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
now = csbi.dwCursorPosition;
for (SIZE_T i = 0; i < iLength; )
{
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
now = csbi.dwCursorPosition;
printf("%c", animations[iSequence++]);
iSequence %= ARRAYSIZE(animations);
GotoXY(now.X, now.Y);
if (!iSequence)
printf("%c", szIn[i++]);
Sleep(dwTickrate);
}
}
static void LogOffset(const char* szName, void* p);
private:
static FILE* pStdout;
static FILE* pStderr;
};