forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.cpp
389 lines (318 loc) · 9.34 KB
/
debug.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
388
389
#include "debug.h"
#include "path_info.h"
#include "output.h"
#include "filesystem.h"
#include "input.h"
#include <time.h>
#include <cassert>
#include <cstdlib>
#include <cstdarg>
#include <iosfwd>
#include <fstream>
#include <streambuf>
#include <sys/stat.h>
#include <exception>
#ifndef _MSC_VER
#include <sys/time.h>
#endif
#ifdef BACKTRACE
#include <execinfo.h>
#include <stdlib.h>
#endif
// Static defines {{{1
// ---------------------------------------------------------------------
#if (defined(DEBUG) || defined(_DEBUG)) && !defined(NDEBUG)
static int debugLevel = DL_ALL;
static int debugClass = DC_ALL;
#else
static int debugLevel = D_ERROR;
static int debugClass = D_MAIN;
#endif
extern bool test_mode;
/** When in @ref test_mode will be set if any debugmsg are emitted */
bool test_dirty = false;
bool debug_mode = false;
namespace
{
std::set<std::string> ignored_messages;
}
void realDebugmsg( const char *filename, const char *line, const char *funcname, const char *mes,
... )
{
assert( filename != nullptr );
assert( line != nullptr );
assert( funcname != nullptr );
va_list ap;
va_start( ap, mes );
const std::string text = vstring_format( mes, ap );
va_end( ap );
if( test_mode ) {
test_dirty = true;
std::cerr << filename << ":" << line << " [" << funcname << "] " << text << std::endl;
return;
}
DebugLog( D_ERROR, D_MAIN ) << filename << ":" << line << " [" << funcname << "] " << text;
std::string msg_key( filename );
msg_key += line;
if( ignored_messages.count( msg_key ) > 0 ) {
return;
}
if( stdscr == nullptr ) {
std::cerr << text.c_str() << std::endl;
abort();
}
fold_and_print( stdscr, 0, 0, getmaxx( stdscr ), c_ltred,
"\n \n" // Looks nicer with some space
" DEBUG : %s\n \n"
" FUNCTION : %s\n"
" FILE : %s\n"
" LINE : %s\n \n"
" Press <color_white>spacebar</color> to continue the game...\n"
" Press <color_white>I</color> (or <color_white>i</color>) to also ignore this particular message in the future...",
text.c_str(), funcname, filename, line );
for( bool stop = false; !stop; ) {
switch( inp_mngr.get_input_event().get_first_input() ) {
case 'i':
case 'I':
ignored_messages.insert( msg_key );
/* fallthrough */
case ' ':
stop = true;
break;
}
}
werase( stdscr );
refresh();
}
// Normal functions {{{1
// ---------------------------------------------------------------------
void limitDebugLevel( int level_bitmask )
{
DebugLog( DL_ALL, DC_ALL ) << "Set debug level to: " << level_bitmask;
debugLevel = level_bitmask;
}
void limitDebugClass( int class_bitmask )
{
DebugLog( DL_ALL, DC_ALL ) << "Set debug class to: " << class_bitmask;
debugClass = class_bitmask;
}
// Debug only {{{1
// ---------------------------------------------------------------------
#define TRACE_SIZE 20
void *tracePtrs[TRACE_SIZE];
// Debug Includes {{{2
// ---------------------------------------------------------------------
// Null OStream {{{2
// ---------------------------------------------------------------------
struct NullBuf : public std::streambuf {
NullBuf() {}
int overflow( int c ) override {
return c;
}
};
// DebugFile OStream Wrapper {{{2
// ---------------------------------------------------------------------
struct DebugFile {
DebugFile();
~DebugFile();
void init( std::string filename );
void deinit();
std::ofstream ¤tTime();
std::ofstream file;
std::string filename;
};
static NullBuf nullBuf;
static std::ostream nullStream( &nullBuf );
// DebugFile OStream Wrapper {{{2
// ---------------------------------------------------------------------
static DebugFile debugFile;
DebugFile::DebugFile()
{
}
DebugFile::~DebugFile()
{
if( file.is_open() ) {
deinit();
}
}
void DebugFile::deinit()
{
file << "\n";
currentTime() << " : Log shutdown.\n";
file << "-----------------------------------------\n\n";
file.close();
}
void DebugFile::init( std::string filename )
{
this->filename = filename;
const std::string oldfile = filename + ".prev";
bool rename_failed = false;
struct stat buffer;
if( stat( filename.c_str(), &buffer ) == 0 ) {
// Continue with the old log file if it's smaller than 1 MiB
if( buffer.st_size >= 1024 * 1024 ) {
rename_failed = !rename_file( filename, oldfile );
}
}
file.open( filename.c_str(), std::ios::out | std::ios::app );
file << "\n\n-----------------------------------------\n";
currentTime() << " : Starting log.";
if( rename_failed ) {
DebugLog( D_ERROR, DC_ALL ) << "Moving the previous log file to " << oldfile << " failed.\n" <<
"Check the file permissions. This program will continue to use the previous log file.";
}
}
void setupDebug()
{
int level = 0;
#ifdef DEBUG_INFO
level |= D_INFO;
#endif
#ifdef DEBUG_WARNING
level |= D_WARNING;
#endif
#ifdef DEBUG_ERROR
level |= D_ERROR;
#endif
#ifdef DEBUG_PEDANTIC_INFO
level |= D_PEDANTIC_INFO;
#endif
if( level != 0 ) {
limitDebugLevel( level );
}
int cl = 0;
#ifdef DEBUG_ENABLE_MAIN
cl |= D_MAIN;
#endif
#ifdef DEBUG_ENABLE_MAP
cl |= D_MAP;
#endif
#ifdef DEBUG_ENABLE_MAP_GEN
cl |= D_MAP_GEN;
#endif
#ifdef DEBUG_ENABLE_GAME
cl |= D_GAME;
#endif
if( cl != 0 ) {
limitDebugClass( cl );
}
debugFile.init( FILENAMES["debug"] );
}
void deinitDebug()
{
debugFile.deinit();
}
// OStream Operators {{{2
// ---------------------------------------------------------------------
std::ostream &operator<<( std::ostream &out, DebugLevel lev )
{
if( lev != DL_ALL ) {
if( lev & D_INFO ) {
out << "INFO ";
}
if( lev & D_WARNING ) {
out << "WARNING ";
}
if( lev & D_ERROR ) {
out << "ERROR ";
}
if( lev & D_PEDANTIC_INFO ) {
out << "PEDANTIC ";
}
}
return out;
}
std::ostream &operator<<( std::ostream &out, DebugClass cl )
{
if( cl != DC_ALL ) {
if( cl & D_MAIN ) {
out << "MAIN ";
}
if( cl & D_MAP ) {
out << "MAP ";
}
if( cl & D_MAP_GEN ) {
out << "MAP_GEN ";
}
if( cl & D_NPC ) {
out << "NPC ";
}
if( cl & D_GAME ) {
out << "GAME ";
}
if( cl & D_SDL ) {
out << "SDL ";
}
}
return out;
}
struct time_info {
int hours;
int minutes;
int seconds;
int mseconds;
template <typename Stream>
friend Stream &operator<<( Stream &out, time_info const &t ) {
using char_t = typename Stream::char_type;
using base = std::basic_ostream<char_t>;
static_assert( std::is_base_of<base, Stream>::value, "" );
out << t.hours << ':' << t.minutes << ':' << t.seconds << '.' << t.mseconds;
return out;
}
};
#ifdef _MSC_VER
time_info get_time() noexcept
{
SYSTEMTIME time {};
GetLocalTime( &time );
return time_info { static_cast<int>( time.wHour ), static_cast<int>( time.wMinute ),
static_cast<int>( time.wSecond ), static_cast<int>( time.wMilliseconds )
};
}
#else
time_info get_time() noexcept
{
timeval tv;
gettimeofday( &tv, nullptr );
auto const tt = time_t {tv.tv_sec};
auto const current = localtime( &tt );
return time_info { current->tm_hour, current->tm_min, current->tm_sec,
static_cast<int>( tv.tv_usec / 1000.0 + 0.5 )
};
}
#endif
std::ofstream &DebugFile::currentTime()
{
return ( file << get_time() );
}
std::ostream &DebugLog( DebugLevel lev, DebugClass cl )
{
// Error are always logged, they are important,
// Messages from D_MAIN come from debugmsg and are equally important.
if( ( ( lev & debugLevel ) && ( cl & debugClass ) ) || lev & D_ERROR || cl & D_MAIN ) {
debugFile.file << std::endl;
debugFile.currentTime() << " ";
if( lev != debugLevel ) {
debugFile.file << lev;
}
if( cl != debugClass ) {
debugFile.file << cl;
}
debugFile.file << ": ";
// Backtrace on error.
#ifdef BACKTRACE
if( lev == D_ERROR ) {
int count = backtrace( tracePtrs, TRACE_SIZE );
char **funcNames = backtrace_symbols( tracePtrs, count );
for( int i = 0; i < count; ++i ) {
debugFile.file << "\n\t(" << funcNames[i] << "), ";
}
debugFile.file << "\n\t";
free( funcNames );
}
#endif
return debugFile.file;
}
return nullStream;
}
// vim:tw=72:sw=1:fdm=marker:fdl=0: