-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDebug.h
334 lines (298 loc) · 10.1 KB
/
Debug.h
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
#ifndef DEBUG_H
#define DEBUG_H
#include <string.h>
#include <sys/time.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <typeinfo>
#include <mutex>
#include <tuple>
#include <pthread.h>
#include <cxxabi.h>
#include <chrono>
#include <map>
class Debug
{
public:
typedef enum {
Error = 0,
Warning,
Info,
Verbose,
Trace
} Level;
Debug( Level level = Verbose, bool nobreak = false ) : mLevel( level ), mNoBreak( nobreak ) {
}
~Debug() {
if ( mLevel <= mDebugLevel ) {
if ( not mNoBreak ) {
mStream << "\n";
}
log( mLevel, mStream.str() );
}
}
template<typename T> Debug& operator+( T t )
{
if ( mLevel <= mDebugLevel ) {
mStream << t;
}
return *this;
}
template<typename T> Debug& operator<<( T t )
{
if ( mLevel <= mDebugLevel ) {
char* type = abi::__cxa_demangle(typeid(t).name(), nullptr, nullptr, nullptr);
char cap = 0;
std::string color = Debug::colored("\x1B[0m");
if ( strstr( type, "basic_string" ) ) {
cap = '\"';
color = Debug::colored("\x1B[31m");
} else if ( !strcmp( type, "char" ) ) {
cap = '\'';
color = Debug::colored("\x1B[31m");
}
std::stringstream arg_ss;
arg_ss << t;
if ( arg_ss.str()[0] >= '0' && arg_ss.str()[0] <= '9' ) {
color = Debug::colored("\x1B[36m");
}
std::stringstream ss;
ss << color;
if ( cap ) ss << cap;
ss << arg_ss.str();
if ( cap ) ss << cap;
ss << Debug::colored("\x1B[0m");
mStream << ss.str();
free(type);
}
return *this;
}
Debug& operator<<(std::ostream& (*manip)(std::ostream&)) {
(*manip)(mStream);
return *this;
}
/*
template<typename T> Debug& operator<<( uint32_t t )
{
if ( mLevel <= mDebugLevel ) {
mStream << Debug::colored("\x1B[36m") << t << Debug::colored("\x1B[0m");
}
return *this;
}
template<typename T> Debug& operator<<( uint64_t t )
{
if ( mLevel <= mDebugLevel ) {
mStream << Debug::colored("\x1B[36m") << t << Debug::colored("\x1B[0m");
}
return *this;
}
template<typename T> Debug& operator<<( const char t )
{
if ( mLevel <= mDebugLevel ) {
mStream << "'" << Debug::colored("\x1B[31m") << t << Debug::colored("\x1B[0m") << "'";
}
return *this;
}
template<typename T> Debug& operator<<( const std::string& t )
{
if ( mLevel <= mDebugLevel ) {
mStream << "\"" << Debug::colored("\x1B[31m") << t << Debug::colored("\x1B[0m") << "\"";
}
return *this;
}
template<typename T> Debug& operator<<( const char* t )
{
if ( mLevel <= mDebugLevel ) {
mStream << "\"" << Debug::colored("\x1B[31m") << t << Debug::colored("\x1B[0m") << "\"";
}
return *this;
}
*/
#pragma GCC system_header // HACK Disable unused-function warnings
template<typename... Args> void fDebug_top( const Args&... args ) {
if ( sizeof...(args) == 0 ) {
operator+( ")" );
} else {
operator+( " " );
fDebug_base( ")", true, args... );
}
}
#pragma GCC system_header // HACK Disable unused-function warnings
template<typename... Args> void fDebug_top2( const char* end, const Args&... args ) {
if ( sizeof...(args) == 0 ) {
operator+( end );
} else {
operator+( " " );
fDebug_base( end, true, args... );
}
}
#pragma GCC system_header // HACK Disable unused-function warnings
void fDebug_base( const char* end, bool f ) {
*this << " " << end;
}
template<typename Arg1, typename... Args> void fDebug_base( const char* end, bool first, const Arg1& arg1, const Args&... args ) {
char* type = abi::__cxa_demangle(typeid(arg1).name(), nullptr, nullptr, nullptr);
char cap = 0;
std::string color = Debug::colored("\x1B[0m");
if ( strstr( type, "char" ) ) {
if ( strstr( type, "*" ) || ( strstr( type, "[" ) && strstr( type, "]" ) ) || strstr( type, "string" ) ) {
cap = '\"';
color = Debug::colored("\x1B[31m");
} else {
cap = '\'';
color = Debug::colored("\x1B[31m");
}
}
free(type);
std::stringstream arg_ss;
arg_ss << arg1;
std::string arg_ss_str = arg_ss.str();
if ( arg_ss_str.length() > 0 && arg_ss_str[0] >= '0' && arg_ss_str[0] <= '9' ) {
color = Debug::colored("\x1B[36m");
}
if (!first ) {
*this << ", ";
}
std::stringstream ss;
ss << color;
if ( cap ) ss << cap;
ss << arg_ss_str;
if ( cap ) ss << cap;
ss << Debug::colored("\x1B[0m");
*this + ss.str();
fDebug_base( end, false, args... );
}
static const char* colored( const char* c ) {
#ifdef TARGET_OS_IPHONE
#else
if ( mColors ) {
return c;
}
#endif
return "";
}
static void StoreLog( bool st = false ) { mStoreLog = st; }
static const std::string& DumpLog() { return mLog; }
static void setDebugLevel( Level lvl ) { mDebugLevel = lvl; }
static void setVerbose( bool en ) { if ( en ) mDebugLevel = Verbose; }
static void setLogFile( const std::string& filename ) { mLogFile = std::ofstream(filename); }
static void setColors( bool enabled ) { mColors = enabled; }
static bool verbose() { return ( mDebugLevel >= Verbose ); }
static bool trace() { return ( mDebugLevel >= Trace ); }
static pthread_t mMainThread;
static uint64_t GetTickMicros()
{
#ifdef WIN32
return timeGetTime();
#elif __APPLE__
struct timeval cTime;
gettimeofday( &cTime, 0 );
return ( cTime.tv_sec * 1000000 ) + cTime.tv_usec;
#else
struct timespec now;
clock_gettime( CLOCK_MONOTONIC, &now );
return now.tv_sec * 1000000 + now.tv_nsec / 1000;
#endif
}
private:
static void log( int level, const std::string& s );
static bool mStoreLog;
static std::string mLog;
static std::mutex mLogMutex;
static std::ofstream mLogFile;
static bool mColors;
static Level mDebugLevel;
std::stringstream mStream;
Level mLevel;
bool mNoBreak;
};
#ifndef __DBG_CLASS
static inline std::string _dbg_className(const std::string& prettyFunction)
{
size_t parenthesis = prettyFunction.find("(");
size_t colons = prettyFunction.substr( 0, parenthesis ).rfind("::");
// if ( prettyFunction.find( "GE::" ) ) {
// colons += 2 + prettyFunction.substr(colons + 2).find( "::" );
// }
if (colons == std::string::npos)
return "<none>";
size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
size_t end = colons - begin;
return Debug::colored("\x1B[32m") + prettyFunction.substr(begin,end) + Debug::colored("\x1B[0m");
}
// using namespace GE;
#pragma GCC system_header // HACK Disable unused-function warnings
static std::string self_thread() {
if ( Debug::mMainThread == 0 ) {
Debug::mMainThread = pthread_self();
}
std::stringstream ret;
pthread_t thid = pthread_self();
char name[64] = "";
#ifdef HAVE_PTHREAD_GETNAME_NP
pthread_getname_np( thid, name, sizeof(name) );
#endif
ret << Debug::colored("\x1B[33m");
if ( Debug::mMainThread == thid ) {
ret << "[MainThread] ";
} else if ( name[0] != 0 ) {
ret << "[" << name << "] ";
} else {
ret << "[0x" << std::hex << thid << std::dec << "] ";
}
ret << Debug::colored("\x1B[0m");
return ret.str();
}
// #define _NUMARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
#pragma GCC system_header // HACK Disable unused-function warnings
static std::string _debug_date() {
#ifdef ANDROID
return std::string();
#endif
typedef std::chrono::system_clock Clock;
auto now = Clock::now();
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
auto fraction = now - seconds;
time_t cnow = Clock::to_time_t(now);
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(fraction);
time_t rawtime;
struct tm* timeinfo;
char buffer[64];
time( &rawtime );
timeinfo = localtime( &rawtime );
strftime( buffer, 63, "[%Y-%m-%d %H:%M:%S", timeinfo );
sprintf( buffer, "%s:%03d] ", buffer, milliseconds.count() );
return buffer;
}
#define __CLASS_NAME__ _dbg_className(__PRETTY_FUNCTION__)
// #define __CLASS_NAME__ __PRETTY_FUNCTION__
#define __FUNCTION_NAME__ ( std::string(Debug::colored("\x1B[94m")) + __FUNCTION__ + Debug::colored("\x1B[0m") )
static std::string _debug_level( Debug::Level level ) {
const static std::map< Debug::Level, std::string > levels = {
{ Debug::Error, std::string(Debug::colored("\x1B[1;41m")) + "ERROR" + Debug::colored("\x1B[0m") },
{ Debug::Warning, std::string(Debug::colored("\x1B[1;41;93m")) + "WARNING" + Debug::colored("\x1B[0m") },
{ Debug::Info, std::string(Debug::colored("\x1B[1;42m")) + "INFO" + Debug::colored("\x1B[0m") },
{ Debug::Verbose, std::string(Debug::colored("\x1B[1;100m")) + "VERBOSE" + Debug::colored("\x1B[0m") },
{ Debug::Trace, std::string(Debug::colored("\x1B[1;100;32m")) + "TRACE" + Debug::colored("\x1B[0m") }
};
if ( not Debug::trace() and level == Debug::Verbose ) {
return "";
}
if ( not Debug::verbose() and level == Debug::Info ) {
return "";
}
return levels.at( level ) + " ";
}
#define gTrace() Debug(Debug::Trace) + _debug_date() + self_thread() + _debug_level(Debug::Trace) + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "() "
#define gDebug() Debug(Debug::Verbose) + _debug_date() + self_thread() + _debug_level(Debug::Verbose) + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "() "
#define gInfo() Debug(Debug::Info) + _debug_date() + self_thread() + _debug_level(Debug::Info) + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "() "
#define gWarning() Debug(Debug::Warning) + _debug_date() + self_thread() + _debug_level(Debug::Warning) + " " + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "() "
#define gError() Debug(Debug::Error) + _debug_date() + self_thread() + _debug_level(Debug::Error) + " " + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "() "
#define fTrace( ... ) { Debug dbg(Debug::Trace);dbg + _debug_date() + self_thread() + _debug_level(Debug::Trace) + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "("; dbg.fDebug_top( __VA_ARGS__ ); }
#define fDebug( ... ) { Debug dbg;dbg + _debug_date() + self_thread() + _debug_level(Debug::Verbose) + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + "("; dbg.fDebug_top( __VA_ARGS__ ); }
#define aDebug( name, ... ) { Debug dbg;dbg + _debug_date() + self_thread() + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + " " + name + " = { "; dbg.fDebug_top2( "}", __VA_ARGS__ ); }
#define vDebug( name, ... ) Debug dbg;dbg + _debug_date() + self_thread() + __CLASS_NAME__ + "::" + __FUNCTION_NAME__ + " " + name + "{ "; dbg.fDebug_top2( "} ", __VA_ARGS__ ); dbg + ""
#endif // __DBG_CLASS
#endif // DEBUG_H