forked from OpenApoc/OpenApoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.cpp
336 lines (294 loc) · 8.34 KB
/
logger.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
#ifdef _WIN32
#define BACKTRACE_WINDOWS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "framework/logger.h"
#include "framework/configfile.h"
#include "framework/framework.h"
#include "library/sp.h"
#include <chrono>
#include <cstdarg>
#include <mutex>
#ifdef BACKTRACE_LIBUNWIND
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <dlfcn.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#elif defined(BACKTRACE_WINDOWS)
#include <windows.h>
// windows.h must be included before DbgHelp.h
#include <DbgHelp.h>
#endif
#ifndef LOGFILE
#define LOGFILE "openapoc_log.txt"
#endif
// Don't have interactive dialogues in unit tests
#ifdef UNIT_TEST
#undef ERROR_DIALOG
#else
#if defined(ERROR_DIALOG)
#include <SDL_messagebox.h>
#endif
#endif
#ifdef ANDROID
#include <android/log.h>
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "OpenApoc", __VA_ARGS__)
#define LOGDV(fmt, ap) __android_log_vprint(ANDROID_LOG_DEBUG, "OpenApoc", fmt, ap)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, "OpenApoc", __VA_ARGS__)
#define LOGWV(fmt, ap) __android_log_vprint(ANDROID_LOG_WARN, "OpenApoc", fmt, ap)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "OpenApoc", __VA_ARGS__)
#define LOGEV(fmt, ap) __android_log_vprint(ANDROID_LOG_ERROR, "OpenApoc", fmt, ap)
#define LOG_PATH "/storage/emulated/0/openapoc/data/"
#else
#define LOGD(...)
#define LOGDV(fmt, ap)
#define LOGW(...)
#define LOGWV(fmt, ap)
#define LOGE(...)
#define LOGEV(fmt, ap)
#define LOG_PATH
#endif
#define MAX_SYMBOL_LENGTH 1000
namespace OpenApoc
{
ConfigOptionInt stderrLogLevelOption(
"Logger", "StderrLevel",
"Loglevel to output to stderr (0 = nothing, 1 = error, 2 = warning, 3 = info, 4 = debug) ", 2);
ConfigOptionInt fileLogLevelOption(
"Logger", "FileLevel",
"Loglevel to output to file (0 = nothing, 1 = error, 2 = warning, 3 = info, 4 = debug)", 3);
ConfigOptionInt backtraceLogLevelOption(
"Logger", "BacktraceLevel",
"Loglevel to print a backtrace on (0 = nothing, 1 = error, 2 = warning, 3 = info, 4 = debug)",
1);
ConfigOptionString logFileOption("Logger", "File", "File to write log to", LOG_PATH LOGFILE);
ConfigOptionBool showDialogOnErrorOption("Logger", "ShowDialog", "Show dialog on error", true);
#if defined(BACKTRACE_LIBUNWIND)
static void print_backtrace(FILE *f)
{
unw_cursor_t cursor;
unw_context_t ctx;
unw_word_t ip;
unw_getcontext(&ctx);
unw_init_local(&cursor, &ctx);
fprintf(f, " called by:\n");
while (unw_step(&cursor) > 0)
{
Dl_info info;
unw_get_reg(&cursor, UNW_REG_IP, &ip);
if (ip == 0)
break;
dladdr(reinterpret_cast<void *>(ip), &info);
if (info.dli_sname)
{
fprintf(f, " 0x%zx %s+0x%zx (%s)\n", static_cast<uintptr_t>(ip), info.dli_sname,
static_cast<uintptr_t>(ip) - reinterpret_cast<uintptr_t>(info.dli_saddr),
info.dli_fname);
continue;
}
// If dladdr() failed, try libunwind
unw_word_t offsetInFn;
char fnName[MAX_SYMBOL_LENGTH];
if (!unw_get_proc_name(&cursor, fnName, MAX_SYMBOL_LENGTH, &offsetInFn))
{
fprintf(f, " 0x%zx %s+0x%zx (%s)\n", static_cast<uintptr_t>(ip), fnName, offsetInFn,
info.dli_fname);
continue;
}
else
fprintf(f, " 0x%zx\n", static_cast<uintptr_t>(ip));
}
}
#elif defined(BACKTRACE_WINDOWS)
#define MAX_STACK_FRAMES 100
static void print_backtrace(FILE *f)
{
static bool initialised = false;
static HANDLE process;
unsigned int frames;
void *ip[MAX_STACK_FRAMES];
SYMBOL_INFO *sym;
if (!initialised)
{
process = GetCurrentProcess();
SymInitialize(process, NULL, true);
initialised = true;
}
if (!process)
{
fprintf(f, "Failed to get process for backtrace\n");
return;
}
sym = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + MAX_SYMBOL_LENGTH * (sizeof(char)), 1);
if (!sym)
{
fprintf(f, "Failed to allocate symbol info for backtrace\n");
return;
}
sym->MaxNameLen = MAX_SYMBOL_LENGTH;
sym->SizeOfStruct = sizeof(SYMBOL_INFO);
/* Skip 2 frames (print_backtrace and the LogError function itself) */
frames = CaptureStackBackTrace(2, MAX_STACK_FRAMES, ip, NULL);
for (unsigned int frame = 0; frame < frames; frame++)
{
SymFromAddr(process, (DWORD64)(ip[frame]), 0, sym);
fprintf(f, " 0x%p %s+0x%Ix\n", ip[frame], sym->Name,
(uintptr_t)ip[frame] - (uintptr_t)sym->Address);
}
free(sym);
}
#else
#warning no backtrace enabled for this platform
static void print_backtrace(FILE *f)
{
// TODO: Other platform backtrace?
}
#endif
static FILE *outFile = nullptr;
// We store options after init as querying every LogInfo() takes a long time
LogLevel stderrLogLevel;
LogLevel fileLogLevel;
LogLevel backtraceLogLevel;
bool showDialogOnError;
bool loggerInited = false;
static std::mutex logMutex;
static std::chrono::time_point<std::chrono::high_resolution_clock> timeInit =
std::chrono::high_resolution_clock::now();
static void initLogger()
{
outFile = NULL;
// Handle Log calls befoore the settings are read, just output everything to stdout
if (!ConfigFile::getInstance().loaded())
{
stderrLogLevel = LogLevel::Debug;
fileLogLevel = LogLevel::Nothing;
backtraceLogLevel = LogLevel::Nothing;
showDialogOnError = false;
// Returning withoput setting loggerInited causes this to be called evey Log call until the
// config is parsed
return;
}
loggerInited = true;
stderrLogLevel = (LogLevel)stderrLogLevelOption.get();
fileLogLevel = (LogLevel)fileLogLevelOption.get();
backtraceLogLevel = (LogLevel)backtraceLogLevelOption.get();
showDialogOnError = showDialogOnErrorOption.get();
auto logFilePath = logFileOption.get();
if (logFilePath.empty())
{
// No log file set, disabling logging to file
fileLogLevel = LogLevel::Nothing;
return;
}
outFile = fopen(logFilePath.cStr(), "w");
if (!outFile)
{
// Failed to open log file, disabling logging to file
fileLogLevel = LogLevel::Nothing;
return;
}
}
void _logAssert(UString prefix, UString string, int line, UString file)
{
Log(LogLevel::Error, prefix,
::OpenApoc::format("Assertion \"%s\" failed at %s:%d", string.cStr(), file.cStr(), line));
exit(EXIT_FAILURE);
}
void Log(LogLevel level, UString prefix, const UString &text)
{
bool exit_app = false;
const char *level_prefix;
logMutex.lock();
if (!loggerInited)
{
initLogger();
}
bool writeToFile = (level <= fileLogLevel);
bool writeToStderr = (level <= stderrLogLevel);
if (!writeToFile && !writeToStderr)
{
// Nothing to do
return;
}
auto timeNow = std::chrono::high_resolution_clock::now();
unsigned long long clockns =
std::chrono::duration<unsigned long long, std::nano>(timeNow - timeInit).count();
switch (level)
{
case LogLevel::Info:
level_prefix = "I";
break;
case LogLevel::Warning:
level_prefix = "W";
break;
default:
level_prefix = "E";
exit_app = true;
break;
}
if (writeToFile)
{
fprintf(outFile, "%s %llu %s: %s\n", level_prefix, clockns, prefix.cStr(), text.cStr());
// On error print a backtrace to the log file
if (level <= backtraceLogLevel)
print_backtrace(outFile);
fflush(outFile);
}
if (writeToStderr)
{
fprintf(stderr, "%s %llu %s: %s\n", level_prefix, clockns, prefix.cStr(), text.cStr());
if (level <= backtraceLogLevel)
print_backtrace(stderr);
fflush(stderr);
}
#if defined(ERROR_DIALOG)
if (showDialogOnError && level == LogLevel::Error)
{
if (!Framework::tryGetInstance())
{
// No framework to have created a window, so don't try to show a dialog
}
else if (!fw().displayHasWindow())
{
// Framework created but without window, so don't try to show a dialog
}
else
{
SDL_MessageBoxData mBoxData;
mBoxData.flags = SDL_MESSAGEBOX_ERROR;
mBoxData.window = NULL; // Might happen before we get our window?
mBoxData.title = "OpenApoc ERROR";
mBoxData.message = text.cStr();
mBoxData.numbuttons = 2;
SDL_MessageBoxButtonData buttons[2];
buttons[0].flags = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
buttons[0].buttonid = 1;
buttons[0].text = "Exit";
buttons[1].flags = SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
buttons[1].buttonid = 2;
buttons[1].text = "try to limp along";
mBoxData.buttons = buttons;
mBoxData.colorScheme = NULL; // Use system settings
int but;
SDL_ShowMessageBox(&mBoxData, &but);
/* button 1 = "exit", button 2 = "try to limp along" */
if (but == 1)
{
exit_app = true;
}
else
{
exit_app = false;
}
}
}
#endif
logMutex.unlock();
if (exit_app)
{
exit(EXIT_FAILURE);
}
}
}; // namespace OpenApoc