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
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include <time.h>
#include "Log.h"
cMCLogger * cMCLogger::s_MCLogger = NULL;
#ifdef _WIN32
HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
cMCLogger* cMCLogger::GetInstance()
{
return s_MCLogger;
}
cMCLogger::cMCLogger(void)
{
AString FileName;
Printf(FileName, "LOG_%d.txt", (int)time(NULL));
m_Log = new cLog(FileName);
m_Log->Log("--- Started Log ---");
s_MCLogger = this;
}
cMCLogger::cMCLogger(const AString & a_FileName)
{
m_Log = new cLog(a_FileName);
}
cMCLogger::~cMCLogger()
{
m_Log->Log("--- Stopped Log ---");
delete m_Log;
if (this == s_MCLogger)
s_MCLogger = NULL;
}
void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
{
switch( a_LogType )
{
case 0:
LOG("%s", a_Text);
break;
case 1:
LOGINFO("%s", a_Text);
break;
case 2:
LOGWARN("%s", a_Text);
break;
case 3:
LOGERROR("%s", a_Text);
break;
default:
LOG("(#%d#: %s", a_LogType, a_Text);
break;
}
}
void cMCLogger::Log(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csGrayOnBlack);
m_Log->Log(a_Format, a_ArgList);
}
void cMCLogger::Info(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csYellowOnBlack);
m_Log->Log(a_Format, a_ArgList);
}
void cMCLogger::Warn(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csRedOnBlack);
m_Log->Log(a_Format, a_ArgList);
}
void cMCLogger::Error(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csBlackOnRed);
m_Log->Log(a_Format, a_ArgList);
}
void cMCLogger::SetColor(eColorScheme a_Scheme)
{
#ifdef _WIN32
WORD Attrib = 0x07; // by default, gray on black
switch (a_Scheme)
{
case csGrayOnBlack: Attrib = 0x07; break;
case csYellowOnBlack: Attrib = 0x0e; break;
case csRedOnBlack: Attrib = 0x0c; break;
case csBlackOnRed: Attrib = 0xc0; break;
default: ASSERT(!"Unhandled color scheme");
}
SetConsoleTextAttribute(g_Console, Attrib);
#elif defined(__linux) && !defined(ANDROID_NDK)
switch (a_Scheme)
{
case csGrayOnBlack: printf("\x1b[0m"); break;
case csYellowOnBlack: printf("\x1b[33;1m"); break;
case csRedOnBlack: printf("\x1b[31;1m"); break;
case csBlackOnRed: printf("\x1b[7;31;1m"); break;
default: ASSERT(!"Unhandled color scheme");
}
#endif
}
//////////////////////////////////////////////////////////////////////////
// Global functions
void LOG(const char* a_Format, ...)
{
va_list argList;
va_start(argList, a_Format);
cMCLogger::GetInstance()->Log( a_Format, argList );
va_end(argList);
}
void LOGINFO(const char* a_Format, ...)
{
va_list argList;
va_start(argList, a_Format);
cMCLogger::GetInstance()->Info( a_Format, argList );
va_end(argList);
}
void LOGWARN(const char* a_Format, ...)
{
va_list argList;
va_start(argList, a_Format);
cMCLogger::GetInstance()->Warn( a_Format, argList );
va_end(argList);
}
void LOGERROR(const char* a_Format, ...)
{
va_list argList;
va_start(argList, a_Format);
cMCLogger::GetInstance()->Error( a_Format, argList );
va_end(argList);
}
|