forked from angrytongan/c2api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.cpp
More file actions
71 lines (63 loc) · 1.49 KB
/
debug.cpp
File metadata and controls
71 lines (63 loc) · 1.49 KB
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
#ifdef DEBUG
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#define FG_RED "\x1b[31m"
#define FG_YELLOW "\x1b[33m"
#define FG_NORMAL "\x1b[0m"
#define DEBUG_NORMAL 0
#define DEBUG_WARNING 1
#define DEBUG_ERROR 2
static void _debug(int type, const char *line) {
bool is_a_tty = isatty(1);
if (type == DEBUG_NORMAL) {
fprintf(stderr, "%s\n", line);
} else if (type == DEBUG_WARNING) {
if (is_a_tty) {
fprintf(stderr, FG_YELLOW"%s"FG_NORMAL"\n", line);
} else {
fprintf(stderr, "%s\n", line);
}
} else {
if (is_a_tty) {
fprintf(stderr, FG_RED"%s"FG_NORMAL"\n", line);
} else {
fprintf(stderr, "%s\n", line);
}
}
}
void debug(const char *fmt, ...) {
va_list ap;
char line[128];
va_start(ap, fmt);
vsnprintf(line, sizeof(line), fmt, ap);
va_end(ap);
_debug(DEBUG_NORMAL, line);
}
void warning(const char *fmt, ...) {
va_list ap;
char line[128];
va_start(ap, fmt);
vsnprintf(line, sizeof(line), fmt, ap);
va_end(ap);
_debug(DEBUG_WARNING, line);
}
void error(const char *fmt, ...) {
va_list ap;
char line[128];
va_start(ap, fmt);
vsnprintf(line, sizeof(line), fmt, ap);
va_end(ap);
_debug(DEBUG_ERROR, line);
}
#else /* DEBUG */
void error(const char *fmt, ...) {
return;
}
void warning(const char *fmt, ...) {
return;
}
void debug(const char *fmt, ...) {
return;
}
#endif /* DEBUG */