- Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathlog.cpp
387 lines (318 loc) · 12.9 KB
/
log.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms, as
* designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
* 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, version 2.0, 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.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include<string>
#include<stdio.h>
#include<stdarg.h>
#include<time.h>
#include<string.h>
#include<vector>
#include<glib/gstdio.h>
#include"base/c++helpers.h"
#include"base/log.h"
#include"base/wb_memory.h"
#include"base/file_utilities.h"
#include"base/file_functions.h"// TODO: these two file libs should really be only one.
#include"base/string_utilities.h"
usingnamespacebase;
staticconstchar* LevelText[] = {"", "ERR", "WRN", "INF", "DB1", "DB2", "DB3"};
/*static*/const std::string Logger::_logLevelNames[] = {"none", "error", "warning", "info",
"debug1", "debug2", "debug3"};
/*static*/bool Logger::_logLevelSpecifiedByUser = false;
//--------------------------------------------------------------------------------------------------
structLogger::LoggerImpl {
LoggerImpl() {
// Default values for all available log levels.
_levels[enumIndex(Logger::LogLevel::Disabled)] = false; // Disable None level.
_levels[enumIndex(Logger::LogLevel::Error)] = true;
_levels[enumIndex(Logger::LogLevel::Warning)] = true;
_levels[enumIndex(Logger::LogLevel::Info)] = true; // Includes all g_message calls.
#if !defined(DEBUG) && !defined(_DEBUG)
_levels[enumIndex(Logger::LogLevel::Debug)] = false; // General debug messages.
_levels[enumIndex(Logger::LogLevel::Debug2)] = false; // Verbose debug messages.
#else
_levels[enumIndex(Logger::LogLevel::Debug)] = true;
_levels[enumIndex(Logger::LogLevel::Debug2)] = true;
#endif
_levels[enumIndex(Logger::LogLevel::Debug3)] = false; // Really chatty, should be switched on only on demand.
}
boollevel_is_enabled(const Logger::LogLevel level) const {
return _levels[enumIndex(level)];
}
std::string _dir;
std::string _filename;
bool _levels[Logger::logLevelCount];
bool _new_line_pending; // Set to true when the last logged entry ended with a new line.
bool _std_err_log;
};
Logger::LoggerImpl* Logger::_impl = nullptr;
//--------------------------------------------------------------------------------------------------
std::string Logger::log_filename() {
return _impl ? _impl->_filename : "";
}
//--------------------------------------------------------------------------------------------------
std::string Logger::log_dir() {
return _impl ? _impl->_dir : "";
}
//--------------------------------------------------------------------------------------------------
Logger::Logger(constbool stderr_log, const std::string& target_file) {
if (!_impl)
_impl = newLogger::LoggerImpl();
_impl->_std_err_log = stderr_log;
if (!target_file.empty()) {
_impl->_filename = target_file;
FILE_scope_ptr fp = base_fopen(_impl->_filename.c_str(), "w");
}
}
//--------------------------------------------------------------------------------------------------
Logger::Logger(const std::string& dir, constbool stderr_log, const std::string& file_name, int limit) {
std::vector<std::string> filenames;
// Creates the file names array
filenames.push_back(strfmt("%s.log", file_name.data()));
for (intindex = 1; index < limit; index++)
filenames.push_back(strfmt("%s.%d.log", file_name.data(), index));
if (!_impl)
_impl = newLogger::LoggerImpl();
_impl->_std_err_log = stderr_log;
_impl->_new_line_pending = true;
if (!dir.empty() && !file_name.empty()) {
_impl->_dir = base::joinPath(dir.c_str(), "log", "");
_impl->_filename = base::joinPath(_impl->_dir.c_str(), filenames[0].c_str(), "");
try {
create_directory(_impl->_dir, 0700, true);
} catch (const file_error& e) {
// We need to catch the exception here, otherwise WB will be aborted
fprintf(stderr, "Exception in logger: %s\n", e.what());
}
// Rotate log files: wb.log -> wb.1.log, wb.1.log -> wb.2.log, ...
for (int i = limit - 1; i > 0; --i) {
try {
std::string filename = base::joinPath(_impl->_dir.c_str(), filenames[i].c_str(), "");
if (file_exists(filename))
remove(filename);
std::string filename2 = base::joinPath(_impl->_dir.c_str(), filenames[i - 1].c_str(), "");
if (file_exists(filename2))
rename(filename2, filename);
} catch (...) {
// we do not care for rename exceptions here!
}
}
// truncate log file we do not need gigabytes of logs
FILE_scope_ptr fp = base_fopen(_impl->_filename.c_str(), "w");
}
}
//--------------------------------------------------------------------------------------------------
voidLogger::enable_level(const LogLevel level) {
if (enumIndex(level) < logLevelCount)
_impl->_levels[enumIndex(level)] = true;
}
//--------------------------------------------------------------------------------------------------
voidLogger::disable_level(const LogLevel level) {
if (enumIndex(level) < logLevelCount)
_impl->_levels[enumIndex(level)] = false;
}
//--------------------------------------------------------------------------------------------------
voidlocal_free(char* d) {
g_free(d);
}
//--------------------------------------------------------------------------------------------------
/**
* Logs the given text with the given domain to the current log file.
* Note: it should be pretty safe to use utf-8 encoded text too here, though avoid log messages
* which are several thousands of chars long.
*/
voidLogger::logv(LogLevel level, constchar* const domain, constchar* format, va_list args) {
scope_ptr<char, local_free> buffer(g_strdup_vprintf(format, args));
// Print to stderr if no logger is created (yet).
if (!_impl) {
fprintf(stderr, "%s", buffer.get());
fflush(stderr);
return;
}
consttime_t t = time(NULL);
structtmtm;
#ifdef _MSC_VER
localtime_s(&tm, &t);
#else
localtime_r(&t, &tm);
#endif
FILE_scope_ptr fp = _impl->_filename.empty() ? NULL : base_fopen(_impl->_filename.c_str(), "a");
if (fp) {
if (_impl->_new_line_pending)
fprintf(fp, "%02u:%02u:%02u [%3s][%15s]: ", tm.tm_hour, tm.tm_min, tm.tm_sec, LevelText[enumIndex(level)],
domain);
fwrite(buffer, 1, strlen(buffer.get()), fp);
}
// No explicit newline here. If messages are composed (e.g. python errors)
// they get split over several lines and lose all their formatting.
// Additionally for messages with implicit newline (which are most) we get many empty lines.
if (_impl->_std_err_log) {
#if defined(_MSC_VER)
HANDLE hConsole = 0;
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
if ((level == LogLevel::Error) || (level == LogLevel::Warning)) {
hConsole = GetStdHandle(STD_ERROR_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes;
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
if (level == LogLevel::Error)
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
elseif (level == LogLevel::Warning)
SetConsoleTextAttribute(hConsole, FOREGROUND_INTENSITY);
}
#elif !defined(__APPLE__)
if (level == LogLevel::Error)
fprintf(stderr, "\e[1;31m");
elseif (level == LogLevel::Warning)
fprintf(stderr, "\e[1m");
#endif
#ifdef _MSC_VER
if (_impl->_new_line_pending) {
char* tmp = g_strdup_printf("%02u:%02u:%02u [%3s][%15s]: ", tm.tm_hour, tm.tm_min, tm.tm_sec,
LevelText[enumIndex(level)], domain);
OutputDebugStringA(tmp);
g_free(tmp);
}
// if you want the program to stop when a specific log msg is printed, put a bp in the next line and set condition
// to log_msg_serial==#
OutputDebugStringA(buffer.get());
#endif
// We need the data in stderr even in Windows, so that the output can be read from other tools.
if (_impl->_new_line_pending)
fprintf(stderr, "%02u:%02u:%02u [%3s][%15s]: ", tm.tm_hour, tm.tm_min, tm.tm_sec, LevelText[enumIndex(level)],
domain);
// If you want the program to stop when a specific log msg is printed, put a bp in the next line
// and set condition to log_msg_serial==#
fprintf(stderr, "%s", buffer.get());
#if defined(_MSC_VER)
if ((level == LogLevel::Error) || (level == LogLevel::Warning))
SetConsoleTextAttribute(hConsole, wOldColorAttrs);
#elif !defined(__APPLE__)
if (level == LogLevel::Error || level == LogLevel::Warning)
fprintf(stderr, "\e[0m");
#endif
}
constchar ending_char = buffer[strlen(buffer) - 1];
_impl->_new_line_pending = (ending_char == '\n') || (ending_char == '\r');
}
//--------------------------------------------------------------------------------------------------
voidLogger::log(const Logger::LogLevel level, constchar* const domain, constchar* format, ...) {
if (_impl->level_is_enabled(level)) {
va_list args;
va_start(args, format);
logv(level, domain, format, args);
va_end(args);
}
}
//--------------------------------------------------------------------------------------------------
voidLogger::log_exc(const LogLevel level, constchar* const domain, constchar* msg, const std::exception& exc) {
log(level, domain, "%s: Exception: %s\n", msg, exc.what());
}
//--------------------------------------------------------------------------------------------------
voidLogger::log_throw(const LogLevel level, constchar* const domain, constchar* format, ...) {
if (_impl->level_is_enabled(level)) {
va_list args;
va_start(args, format);
logv(level, domain, format, args);
va_end(args);
throwstd::logic_error("");
}
}
//--------------------------------------------------------------------------------------------------
std::string Logger::get_state() {
std::string state = "";
if (_impl) {
for (std::size_t i = 0; i < logLevelCount; ++i) {
state += _impl->level_is_enabled((Logger::LogLevel)i) ? "1" : "0";
}
}
return state;
}
//--------------------------------------------------------------------------------------------------
voidLogger::set_state(const std::string& state) {
if (_impl && state.length() >= logLevelCount) {
for (std::size_t i = 0; i < logLevelCount; ++i) {
constchar level = state[i];
if (level == '1') {
Logger::enable_level((Logger::LogLevel)i);
} elseif (level == '0') {
Logger::disable_level((Logger::LogLevel)i);
}
}
}
}
//--------------------------------------------------------------------------------------------------
/**
* Returns the most chatty log level which is currently active.
*/
std::string Logger::active_level() {
if (_impl == NULL)
return"none";
int i;
for (i = logLevelCount - 1; i >= 0; i--)
if (_impl->level_is_enabled((LogLevel)i))
break;
switch (i) {
case0:
return"none";
case1:
return"error";
case2:
return"warning";
case3:
return"info";
case4:
return"debug1";
case5:
return"debug2";
case6:
return"debug3";
}
return"none";
}
//--------------------------------------------------------------------------------------------------
/**
* Used to set a log level, which implicitly enables all less-chatty levels too.
* E.g. setting "info" not only enables info, but also warning and error etc.
*/
boolLogger::active_level(const std::string& value) {
if (_impl == NULL)
returnfalse;
int levelIndex = logLevelCount - 1;
while (levelIndex >= 0 && !same_string(value, _logLevelNames[levelIndex]))
levelIndex--;
if (levelIndex < 0)
returnfalse; // Invalid value given. Ignore it.
for (int i = 0; i < int(logLevelCount); ++i) {
if (levelIndex >= i)
enable_level((LogLevel)i);
else
disable_level((LogLevel)i);
}
returntrue;
}
//--------------------------------------------------------------------------------------------------
voidLogger::log_to_stderr(bool value) {
_impl->_std_err_log = value;
}