-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
78 lines (70 loc) · 4.34 KB
/
log.c
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
#include "log.h"
static bool LOG_ENABLED = false;
void enable_log(void) { LOG_ENABLED = true; }
void disable_log(void) { LOG_ENABLED = false; }
bool log_enabled(void) { return LOG_ENABLED; }
#ifdef NLOG
#define DEF_LOG_FN(NAME, _PREFIX) \
void NAME(const char *format, ...) { (void)format; } \
void NAME##sl(const char *format, ...) { (void)format; } \
void f##NAME(const char *format, ...) { (void)format; } \
void f##NAME##sl(const char *format, ...) { (void)format; }
#else
#define DEF_LOG_FN(NAME, PREFIX) \
void NAME(const char *format, ...) { \
if (!LOG_ENABLED) { \
return; \
} \
va_list v; \
va_start(v, format); \
fprintf(stderr, "%s" PR_RESET, PREFIX); \
vfprintf(stderr, format, v); \
fprintf(stderr, "\n"); \
va_end(v); \
} \
void NAME##sl(const char *format, ...) { \
if (!LOG_ENABLED) { \
return; \
} \
va_list v; \
va_start(v, format); \
fprintf(stderr, "%s" PR_RESET, PREFIX); \
vfprintf(stderr, format, v); \
va_end(v); \
} \
void f##NAME(FILE *file, const char *format, ...) { \
if (!LOG_ENABLED) { \
return; \
} \
va_list v; \
va_start(v, format); \
fprintf(file, "%s" PR_RESET, PREFIX); \
vfprintf(file, format, v); \
fprintf(file, "\n"); \
va_end(v); \
} \
void f##NAME##sl(FILE *file, const char *format, ...) { \
if (!LOG_ENABLED) { \
return; \
} \
va_list v; \
va_start(v, format); \
fprintf(file, "%s" PR_RESET, PREFIX); \
vfprintf(file, format, v); \
va_end(v); \
}
#endif
#define PR_RESET "\x1B[0m"
#define PR_RED "\x1B[31m"
#define PR_GREEN "\x1B[32m"
#define PR_YELLOW "\x1B[33m"
#define PR_BLUE "\x1B[34m"
#define PR_MAGENTA "\x1B[35m"
#define PR_CYAN "\x1B[36m"
#define PR_WHITE "\x1B[37m"
DEF_LOG_FN(err, PR_RED "ERROR: ")
DEF_LOG_FN(warn, PR_YELLOW "WARN: ")
DEF_LOG_FN(info, PR_GREEN "INFO: ")
DEF_LOG_FN(debug, PR_WHITE "DEBUG: ")
DEF_LOG_FN(trace, PR_WHITE "TRACE: ")
DEF_LOG_FN(slog, "")