forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.cpp
221 lines (172 loc) · 4.48 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
#include "debug.h"
#include <time.h>
#include <cstdlib>
#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
// ---------------------------------------------------------------------
static int debugLevel = DL_ALL;
static int debugClass = DC_ALL;
// Normal functions {{{1
// ---------------------------------------------------------------------
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);
}
void limitDebugLevel( int i )
{
dout() << "Set debug level to: " << (DebugLevel)i;
debugLevel = i;
}
void limitDebugClass( int i )
{
dout() << "Set debug class to: " << (DebugClass)i;
debugClass = i;
}
// Debug only {{{1
// ---------------------------------------------------------------------
#ifdef ENABLE_LOGGING
#define TRACE_SIZE 20
void* tracePtrs[TRACE_SIZE];
// Debug Includes {{{2
// ---------------------------------------------------------------------
#include <fstream>
#include <streambuf>
// Null OStream {{{2
// ---------------------------------------------------------------------
struct NullBuf : public std::streambuf
{
NullBuf() {}
int overflow(int c) { return c; }
};
struct DebugFile
{
DebugFile();
~DebugFile();
std::ofstream & currentTime();
std::ofstream file;
};
static NullBuf nullBuf;
static std::ostream nullStream(&nullBuf);
// DebugFile OStream Wrapper {{{2
// ---------------------------------------------------------------------
static DebugFile debugFile;
DebugFile::DebugFile()
{
file.open("data/debug.txt", std::ios::out | std::ios::app );
file << "\n\n-----------------------------------------\n";
currentTime() << " : Starting log.";
}
DebugFile::~DebugFile()
{
file << "\n";
currentTime() << " : Log shutdown.\n";
file << "-----------------------------------------\n\n";
file.close();
}
// 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 ";
}
return out;
}
std::ofstream & DebugFile::currentTime()
{
struct tm *current;
timeval tv;
gettimeofday(&tv, NULL);
current = localtime(&tv.tv_sec);
file << current->tm_hour << ":" << current->tm_min << ":" <<
current->tm_sec << "." << int(tv.tv_usec/1000 + 0.5);
return file;
}
std::ostream & dout(DebugLevel lev,DebugClass cl)
{
if( (lev & debugLevel) && (cl & debugClass) )
{
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)
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;
}
// Non Debug Only {{{1
// ---------------------------------------------------------------------
#else // If NOT defined ENABLE_LOGGING
DebugVoid dout(DebugLevel,DebugClass)
{ return DebugVoid(); }
#endif // END If NOT defined ENABLE_LOGGING
// vim:tw=72:sw=1:fdm=marker:fdl=0: