Repository navigation
Expand file tree
/
Copy pathlog.cpp
More file actions
339 lines (304 loc) · 11.4 KB
/
Copy pathlog.cpp
File metadata and controls
339 lines (304 loc) · 11.4 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 Joerg Henrichs
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "utils/log.hpp"
#include "config/user_config.hpp"
#include "network/network_config.hpp"
#include "utils/file_utils.hpp"
#include "utils/tls.hpp"
#include <cstdio>
#include <ctime>
#include <stdio.h>
#ifdef ANDROID
# include <android/log.h>
#endif
#ifdef IOS_STK
#include "../../../lib/irrlicht/source/Irrlicht/CIrrDeviceiOS.h"
#endif
#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
Log::LogLevel Log::m_min_log_level = Log::LL_VERBOSE;
bool Log::m_no_colors = false;
FILE* Log::m_file_stdout = NULL;
size_t Log::m_buffer_size = 1;
bool Log::m_console_log = true;
Synchronised<std::vector<struct Log::LineInfo> > Log::m_line_buffer;
thread_local char g_prefix[11] = {};
// ----------------------------------------------------------------------------
void Log::setPrefix(const char* prefix)
{
size_t len = strlen(prefix);
if (len > 10)
len = 10;
if (len != 0)
memcpy(g_prefix, prefix, len);
g_prefix[len] = 0;
} // setPrefix
// ----------------------------------------------------------------------------
/** Selects background/foreground colors for the message depending on
* log level. It is only called if messages are not redirected to a file.
* \param level The level for which to set the color.
*/
void Log::setTerminalColor(LogLevel level)
{
if (!m_console_log) return;
if(m_no_colors) return;
// Thanks to funto for the colouring code!
#ifdef WIN32
enum TermColor
{
TERM_BLACK, TERM_BLUE, TERM_GREEN, TERM_CYAN,
TERM_RED, TERM_MAGENTA, TERM_BROWN, TERM_LIGHTGRAY,
TERM_DARKGRAY, TERM_LIGHTBLUE, TERM_LIGHTGREEN, TERM_LIGHTCYAN,
TERM_LIGHTRED, TERM_LIGHTMAGENTA, TERM_YELLOW, TERM_WHITE
};
char color;
switch(level)
{
default:
case LL_VERBOSE: color = TERM_BLACK << 4 | TERM_LIGHTGRAY; break;
case LL_DEBUG: color = TERM_BLACK << 4 | TERM_LIGHTGRAY; break;
case LL_INFO: color = TERM_BLACK << 4 | TERM_LIGHTGRAY; break;
case LL_WARN : color = TERM_LIGHTGRAY << 4 | TERM_LIGHTRED; break;
case LL_ERROR: color = TERM_LIGHTGRAY << 4 | TERM_RED; break;
case LL_FATAL: color = TERM_RED << 4 | TERM_WHITE; break;
} // switch
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
#else
enum TermAttr
{
TERM_RESET = 0, // "normal" mode
TERM_BRIGHT = 1,// more luminosity for the foreground
TERM_DIM = 2, // less luminosity for the foreground
};
enum TermColor
{
TERM_BLACK=0, TERM_RED, TERM_GREEN, TERM_YELLOW,
TERM_BLUE, TERM_MAGENTA, TERM_CYAN, TERM_WHITE
};
int attr = TERM_BRIGHT, front_color=-1;
switch(level)
{
case LL_VERBOSE: attr = TERM_DIM; front_color=TERM_WHITE; break;
case LL_DEBUG: attr = TERM_DIM; front_color=TERM_WHITE; break;
case LL_INFO: attr = TERM_RESET; break;
case LL_WARN: attr = TERM_DIM; front_color=TERM_RED; break;
case LL_ERROR: attr = TERM_BRIGHT; front_color=TERM_RED; break;
case LL_FATAL: attr = TERM_BRIGHT; front_color=TERM_RED; break;
}
if(attr==TERM_RESET)
printf("%c[%dm", 0x1B,attr); // not necessary, but
else
{
printf("%c[%d;%dm", 0x1B,attr, front_color+30);
}
#endif
} // setTerminalColor
// ----------------------------------------------------------------------------
/** Resets the terminal color to the default, and adds a new line (if a new
* line is added as part of the message, a potential change of background
* color will also affect the next row).
*/
void Log::resetTerminalColor()
{
if (!m_console_log) return;
if(m_no_colors) return;
#ifdef WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
/*TERM_BLACK*/0 << 4 | /*TERM_LIGHTGRAY*/7);
#else
printf("%c[0;;m", 0x1B);
#endif
} // resetTerminalColor
// ----------------------------------------------------------------------------
/** This actually creates a log message. If the messages are to be buffered,
* it will be appended to the output buffer. If the buffer is full, it will
* be flushed. If the message is not to be buffered, it will be immediately
* written using writeLine().
* \param level Log level of the message to print.
* \param format A printf-like format string.
* \param va_list The values to be printed for the format.
*/
void Log::printMessage(int level, const char *component, const char *format,
VALIST args)
{
assert(level >= 0 && level <= LL_FATAL);
if (level < m_min_log_level) return;
static const char *names[] = { "debug", "verbose ", "info ",
"warn ", "error ", "fatal " };
const int MAX_LENGTH = 4096;
char line[MAX_LENGTH + 1];
int index = 0;
int remaining = MAX_LENGTH;
if (strlen(g_prefix) != 0)
{
index += snprintf(line+index, remaining, "%s ", g_prefix);
remaining = MAX_LENGTH - index > 0 ? MAX_LENGTH - index : 0;
}
#ifdef MOBILE_STK
// Mobile STK already has timestamp logging in console
std::string server_prefix = "Server";
#else
std::string server_prefix = StkTime::getLogTime();
#endif
if (NetworkConfig::get()->isNetworking() &&
NetworkConfig::get()->isServer())
{
index += snprintf (line + index, remaining,
"%s [%s] %s: ", server_prefix.c_str(),
names[level], component);
}
else
{
index += snprintf (line + index, remaining,
"[%s] %s: ", names[level], component);
}
remaining = MAX_LENGTH - index > 0 ? MAX_LENGTH - index : 0;
index += vsnprintf(line + index, remaining, format, args);
remaining = MAX_LENGTH - index > 0 ? MAX_LENGTH - index : 0;
va_end(args);
// We need to reserve an extra character for \0
index = index > MAX_LENGTH - 2 ? MAX_LENGTH - 2 : index;
snprintf(line + index, 2, "\n");
// If the data is not buffered, immediately print it:
if (m_buffer_size <= 1)
{
writeLine(line, level);
return;
}
// Now the data needs to be buffered. Add an entry to the buffer,
// and if necessary flush the buffers.
struct LineInfo li;
li.m_level = level;
li.m_line = std::string(line);
m_line_buffer.lock();
m_line_buffer.getData().push_back(li);
if (m_line_buffer.getData().size() < m_buffer_size)
{
// Buffer not yet full, don't flush data.
m_line_buffer.unlock();
return;
}
m_line_buffer.unlock();
// Because of the unlock above it can happen that another thread adds
// another line and calls flushBuffers() first before this thread can
// call it, but that doesn't really matter, when this thread will finally
// call flushBuffer() it will then just print nothing if another thread
// had been actively flushing it in between.
flushBuffers();
} // printMessage
// ----------------------------------------------------------------------------
/** Writes the specified line to the various output devices, e.g. terminal,
* log file etc. If log messages are not redirected to a file, it tries to
* select a terminal colour.
* \param line The line to write.
* \param level Message level. Only used to select terminal colour.
*/
void Log::writeLine(const char *line, int level)
{
// If we don't have a console file, write to stdout and hope for the best
if (m_buffer_size <= 1 || !m_file_stdout)
{
setTerminalColor((LogLevel)level);
if (m_console_log)
{
#ifdef ANDROID
android_LogPriority alp;
switch (level)
{
// STK is using the levels slightly different from android
// (debug lowest, verbose above it; while android reverses
// this order. So to get the same behaviour (e.g. filter
// out debug message, but still get verbose, we swap
// the order here.
case LL_VERBOSE: alp = ANDROID_LOG_DEBUG; break;
case LL_DEBUG: alp = ANDROID_LOG_VERBOSE; break;
case LL_INFO: alp = ANDROID_LOG_INFO; break;
case LL_WARN: alp = ANDROID_LOG_WARN; break;
case LL_ERROR: alp = ANDROID_LOG_ERROR; break;
case LL_FATAL: alp = ANDROID_LOG_FATAL; break;
default: alp = ANDROID_LOG_FATAL;
}
__android_log_print(alp, "SuperTuxKart", "%s", line);
#elif defined(IOS_STK)
CIrrDeviceiOS::debugPrint(line);
#else
printf("%s", line);
fflush(stdout);
#endif
}
resetTerminalColor(); // this prints a \n
}
#if defined(_MSC_FULL_VER) && defined(_DEBUG)
// We don't use utf8ToWide for performance, and debug message
// is mostly english anyway
if (m_buffer_size <= 1) OutputDebugStringA(line);
#endif
if (m_file_stdout) fprintf(m_file_stdout, "%s", line);
#ifdef WIN32
if (level >= LL_FATAL)
{
MessageBoxA(NULL, line, "SuperTuxKart - Fatal error", MB_OK);
}
#endif
} // _fluhBuffers
// ----------------------------------------------------------------------------
void Log::toggleConsoleLog(bool val)
{
m_console_log = val;
} // toggleConsoleLog
// ----------------------------------------------------------------------------
/** Flushes all stored log messages to the various output devices (thread safe).
*/
void Log::flushBuffers()
{
m_line_buffer.lock();
for (unsigned int i = 0; i < m_line_buffer.getData().size(); i++)
{
const LineInfo &li = m_line_buffer.getData()[i];
writeLine(li.m_line.c_str(), li.m_level);
}
m_line_buffer.getData().clear();
m_line_buffer.unlock();
} // flushBuffers
// ----------------------------------------------------------------------------
/** This function opens the files that will contain the output.
* \param logout : name of the file that will contain stdout output
* \param logerr : name of the file that will contain stderr output
*/
void Log::openOutputFiles(const std::string &logout)
{
m_file_stdout = FileUtils::fopenU8Path(logout, "w");
if (!m_file_stdout)
{
Log::error("main", "Can not open log file '%s'. Writing to "
"stdout instead.", logout.c_str());
}
else
{
// Disable buffering so that messages are seen asap
setvbuf(m_file_stdout, NULL, _IONBF, 0);
}
} // closeOutputFiles
// ----------------------------------------------------------------------------
/** Function to close output files */
void Log::closeOutputFiles()
{
fclose(m_file_stdout);
} // closeOutputFiles