forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.cpp
297 lines (249 loc) · 7.11 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
#include "debug.h"
#include "path_info.h"
#include "output.h"
#include "file_wrapper.h"
#include <time.h>
#include <cstdlib>
#include <cstdarg>
#include <iosfwd>
#include <fstream>
#include <streambuf>
#include <sys/stat.h>
#ifndef _MSC_VER
#include <sys/time.h>
#endif
#if !(defined _WIN32 || defined WINDOWS || defined __CYGWIN__)
#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
bool debug_mode = false;
void realDebugmsg( const char *filename, const char *line, const char *mes, ... )
{
va_list ap;
va_start( ap, mes );
const std::string text = vstring_format( mes, ap );
va_end( ap );
DebugLog( D_ERROR, D_MAIN ) << filename << ":" << line << " " << text;
fold_and_print( stdscr, 0, 0, getmaxx( stdscr ), c_ltred, "DEBUG: %s\n Press spacebar...",
text.c_str() );
while( getch() != ' ' ) {
// wait for spacebar
}
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 )
{
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;
}
std::ofstream &DebugFile::currentTime()
{
struct tm *current;
timeval tv;
time_t tt;
gettimeofday( &tv, NULL );
tt = tv.tv_sec;
current = localtime( &tt );
file << current->tm_hour << ":" << current->tm_min << ":" <<
current->tm_sec << "." << int( tv.tv_usec / 1000 + 0.5 );
return file;
}
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.
#if !(defined _WIN32 || defined WINDOWS || defined __CYGWIN__)
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: