-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.h
109 lines (98 loc) · 3.31 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
//#ifndef DEBUG_H_INCLUDED
//#define DEBUG_H_INCLUDED
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
// FONT MACRO
#define CL_FT_BLACK "\x1b[30m"
#define CL_FT_BLUE "\x1b[34m"
#define CL_FT_BROWN "\x1b[33m"
#define CL_FT_GRAY "\x1b[37m"
#define CL_FT_GREEN "\x1b[32m"
#define CL_FT_MAGENTA "\x1b[36m"
#define CL_FT_PURPLE "\x1b[35m"
#define CL_FT_RED "\x1b[31m"
#define CL_BG_BLACK "\x1b[40m"
#define CL_BG_BLUE "\x1b[44m"
#define CL_BG_BROWN "\x1b[43m"
#define CL_BG_GRAY "\x1b[47m"
#define CL_BG_GREEN "\x1b[42m"
#define CL_BG_MAGENTA "\x1b[46m"
#define CL_BG_PURPLE "\x1b[45m"
#define CL_BG_RED "\x1b[41m"
#define TY_FT_BOLD "\x1b[1m"
//#define TY_FT_BOLD "\x1b[5m"
#define TY_FT_DEFAULT "\x1b[0m"
#define TY_FT_HBOLD "\x1b[2m"
#define TY_FT_REVERSE "\x1b[7m"
#define TY_FT_UNDERLINE "\x1b[4m"
#define SET_FONT(X) printf(X)
#define printf_color(color, ...) \
{ \
SET_FONT(color); \
printf(__VA_ARGS__); \
SET_FONT(TY_FT_DEFAULT); \
}
// LOG MACRO
#if defined(DEBUG)
#define LOG(format, ...) \
{ \
char __tb[101]; \
time_t t = time(NULL); \
struct tm* tmp = localtime(&t); \
strftime(__tb, 100, "%d.%m.%Y %H:%M:%S ", tmp); \
const char* __filename__ = __FILE__; \
char* __iterator__ = (char*)__filename__ + strlen(__filename__) - 1; \
int __count__slash__ = 0; \
while (__iterator__ > __filename__ && __count__slash__ < 2) { \
if (*__iterator__ == '/') \
__count__slash__ ++; \
if (__count__slash__ != 2) \
__iterator__--; \
} \
fprintf(stderr, "%s [pid=%d] [%s:%d] ", __tb, (int)getpid(), __iterator__, __LINE__); \
fprintf(stderr, format, ##__VA_ARGS__); \
fprintf(stderr, "\n"); \
fflush(stderr); \
}
#else
#define LOG(format, ...) ;
#endif
// PERROR MACRO
#define PERROR(format, ...) \
{ \
char __tb[101]; \
time_t t = time(NULL); \
struct tm* tmp = localtime(&t); \
strftime(__tb, 100, "%d.%m.%Y %H:%M:%S ", tmp); \
const char* __filename__ = __FILE__; \
char* __iterator__ = (char*)__filename__ + strlen(__filename__) - 1; \
int __count__slash__ = 0; \
while (__iterator__ > __filename__ && __count__slash__ < 2) { \
if (*__iterator__ == '/') \
__count__slash__ ++; \
if (__count__slash__ != 2) \
__iterator__--; \
} \
fprintf(stderr, "%s [pid=%d] [%s:%d] ", __tb, (int)getpid(), __iterator__, __LINE__); \
SET_FONT(CL_FT_RED); \
fprintf(stderr, " ERROR! "); \
SET_FONT(TY_FT_DEFAULT); \
fprintf(stderr, format, ##__VA_ARGS__); \
if (errno != 0) \
fprintf(stderr, " (%s)\n", strerror(errno)); \
else \
fprintf(stderr, "\n"); \
fflush(stderr); \
}
#include <iostream>
#include <sstream>
#define _VAR_LOG(var_name, var) \
{std::stringstream buf; \
buf << var_name << " = " << var; \
std::string str_buf(buf.str()); \
LOG("%s", str_buf.c_str()); \
} \
#define VAR_LOG(var) _VAR_LOG(#var, var)
//#endif // DEBUG_H_INCLUDED