-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbg.hpp
315 lines (281 loc) · 6.57 KB
/
dbg.hpp
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
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <utility>
#include <queue>
#include <stack>
#ifdef __GNUC__
#include <cxxabi.h>
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
#define dbg(...) debugFunc(__FILE__, __LINE__, __func__, #__VA_ARGS__ __VA_OPT__(,) __VA_ARGS__)
template<typename T>
static std::string getDemangledTypename()
{
#ifdef __GNUC__
const char *typeName = typeid(T).name();
int status;
char *demangledName = abi::__cxa_demangle(typeName, NULL, NULL, &status);
std::string temp;
if(status == 0)
{
temp = demangledName;
free(demangledName);
}
return temp;
#endif
return "";
}
template<size_t index, typename... Args>
void tuple_printer(std::ostream &s, const std::tuple<Args...> &x)
{
s << std::get<index>(x);
if constexpr(sizeof...(Args) != index + 1)
{
s << ", ";
tuple_printer<index + 1>(s, x);
}
}
template<typename... Args>
std::ostream& operator<<(std::ostream &s, const std::tuple<Args...> &x)
{
s << '(';
tuple_printer<0>(s, x);
s << ')';
return s;
}
template<typename A, typename B>
static std::ostream &operator<<(std::ostream &str, const std::pair<A, B> &p)
{
str << "(" << p.first << ", " << p.second << ")";
return str;
}
// template<typename C,
// typename T = decltype(std::declval<const C&>().begin()) >
// std::ostream& operator<<(std::ostream& str, const C& t)
// {
// str << "{";
// bool first = true;
// for(auto x : t)
// {
// if(!first)
// str << ", ";
// str << x;
// first = false;
// }
// str << "}";
// return str;
// }
template<typename T>
static std::ostream &operator<<(std::ostream &str, const std::vector<T> &v)
{
str << "[";
bool first = true;
for(const T& x : v)
{
if(!first)
str << ", ";
str << x;
first = false;
}
str << "]";
return str;
}
template<typename T>
static std::ostream &operator<<(std::ostream &str, const std::set<T> &s)
{
str << "{";
bool first = true;
for(const T& x : s)
{
if(!first)
str << ", ";
str << x;
first = false;
}
str << "}";
return str;
}
template<typename Key, typename Value>
static std::ostream &operator<<(std::ostream &str, const std::map<Key, Value> &m)
{
str << "{";
bool first = true;
for(const auto& x : m)
{
if(!first)
str << ", ";
str << x;
first = false;
}
str << "}";
return str;
}
template<typename T>
static std::ostream &operator<<(std::ostream &str, const std::queue<T> &q)
{
str << "[";
std::queue<T> temp = q;
bool first = true;
while(!temp.empty())
{
if(!first)
str << ", ";
str << temp.front();
temp.pop();
first = false;
}
str << "]";
return str;
}
template<typename T>
static std::ostream &operator<<(std::ostream &str, const std::stack<T> &s)
{
str << "[";
std::stack<T> temp = s;
bool first = true;
while(!temp.empty())
{
if(!first)
str << ", ";
str << temp.top();
temp.pop();
first = false;
}
str << "]";
return str;
}
template<typename T>
static std::ostream &operator<<(std::ostream &str, const std::priority_queue<T> &pq)
{
str << "[";
std::priority_queue<T> temp = pq;
bool first = true;
while(!temp.empty())
{
if(!first)
str << ", ";
str << temp.top();
temp.pop();
first = false;
}
str << "]";
return str;
}
static volatile sig_atomic_t intOcurred;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
std::vector<int> splitArgnames(const char *argNames)
{
std::vector<int> ret;
ret.push_back(0);
int parenthesis = 0, brackets = 0, sqbrackets = 0;
bool quot = false;
size_t i;
for(i = 0; argNames[i] != '\0'; ++i)
{
if(argNames[i] == ',' && parenthesis == 0 && brackets == 0 && sqbrackets == 0 && !quot)
{
ret.push_back(i + 1);
}
parenthesis += (argNames[i] == '(' ? 1 : (argNames[i] == ')' ? -1 : 0));
brackets += (argNames[i] == '{' ? 1 : (argNames[i] == '}' ? -1 : 0));
sqbrackets += (argNames[i] == '[' ? 1 : (argNames[i] == ']' ? -1 : 0));
quot = (argNames[i] == '\"' ? !quot : quot);
}
ret.push_back(i + 1);
return ret;
}
static void logArg(const char *argNames, const std::vector<int> &splitIndices, int curIndex)
{
}
#pragma GCC diagnostic pop
template<typename First, typename... Args>
static void logArg(const char *argNames, const std::vector<int> &splitIndices, int curIndex, First&& first, Args&&... args)
{
std::string_view curArgName(argNames + splitIndices[curIndex], (splitIndices[curIndex + 1] - splitIndices[curIndex] - 1));
std::cerr << curArgName << "(" << getDemangledTypename<decltype(first)>() << ")" << " = " << first;
if(sizeof...(Args))
{
std::cerr << ", ";
logArg(argNames, splitIndices, curIndex + 1, std::forward<Args>(args)...);
}
}
template<typename... Args>
void debugFunc(const char *file, unsigned int line, const char *funcName, const char *argNames, Args&&... argValues)
{
std::cout << std::flush;
std::cerr << std::flush;
#ifdef _WIN32
HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hErr, &csbInfo);
SetConsoleTextAttribute(hErr, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
std::cerr << "\u001b[31m";
#endif
intOcurred = 0;
void (*prevSIGINTHandler) (int) = signal(SIGINT, [](int sig)
{
intOcurred = 1;
});
//Actual printing of stuff goes here
std::cerr << "[" << file << ":" << line << " @ " << funcName << "] ";
std::vector<int> argPos = splitArgnames(argNames);
logArg(argNames, argPos, 0, std::forward<Args>(argValues)...);
std::cerr << std::endl;
/*for(size_t i = 0; i + 1 < argPos.size(); ++i)
{
std::cerr << std::string_view(argNames + argPos[i], (argPos[i + 1] - argPos[i] - 1)) << std::endl;
}*/
//End of print
#ifdef _WIN32
SetConsoleTextAttribute(hErr, csbInfo.wAttributes);
#else
std::cerr << "\u001b[0m";
#endif
signal(SIGINT, prevSIGINTHandler);
if(intOcurred)
raise(SIGINT);
}
#if false
void (*defSIGINTHandler) (int);
void (*defSIGSEGVHandler) (int);
void (*defSIGFPEHandler) (int);
void (*defSIGABRTHandler) (int);
static void _sigHandler(int sigN)
{
//Installing a signal handler to avoid color contamination to console
if(sigN == SIGINT)
return defSIGINTHandler(sigN);
else if(sigN == SIGSEGV)
return defSIGSEGVHandler(sigN);
else if(sigN == SIGFPE)
return defSIGFPEHandler(sigN);
else if(sigN == SIGABRT)
return defSIGABRTHandler(sigN);
//exit(EXIT_FAILURE);
}
static void _final()
{
}
struct _init
{
_init()
{
atexit(_final);
defSIGINTHandler = signal(SIGINT, _sigHandler);
defSIGSEGVHandler = signal(SIGSEGV, _sigHandler);
defSIGFPEHandler = signal(SIGSEGV, _sigHandler);
defSIGABRTHandler = signal(SIGABRT, _sigHandler);
}
} _initInstance;
#endif