-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.h
128 lines (112 loc) · 3.28 KB
/
logger.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
#ifndef __LOGGER_H
#define __LOGGER_H
#include <stdio.h>
#include <time.h>
#define LOGGER_LEVEL_DISABLE 1
#define LOGGER_LEVEL_FATAL 2
#define LOGGER_LEVEL_ERROR 3
#define LOGGER_LEVEL_WARNING 4
#define LOGGER_LEVEL_INFO 5
#define LOGGER_LEVEL_DEBUG 6
#if defined(LOGGER_LEVEL) && 1 <= LOGGER_LEVEL && LOGGER_LEVEL <= 6
#define __LOGGER_LEVEL LOGGER_LEVEL
#else
#error LOGGER_LEVEL must be define before including 'logger.h'
#endif
#define __LOGGER_ENABLED(level) ((level) <= __LOGGER_LEVEL)
#define __LOGGER_ENABLED_FATAL __LOGGER_ENABLED(LOGGER_LEVEL_FATAL)
#define __LOGGER_ENABLED_ERROR __LOGGER_ENABLED(LOGGER_LEVEL_ERROR)
#define __LOGGER_ENABLED_WARNING __LOGGER_ENABLED(LOGGER_LEVEL_WARNING)
#define __LOGGER_ENABLED_INFO __LOGGER_ENABLED(LOGGER_LEVEL_INFO)
#define __LOGGER_ENABLED_DEBUG __LOGGER_ENABLED(LOGGER_LEVEL_DEBUG) && !NDEBUG
#if __LOGGER_ENABLED_FATAL
#define logger_fatal(msg) __logger_inner_fatal(__LINE__, __FILE__, msg)
#else
#define logger_fatal(msg)
#endif
#if __LOGGER_ENABLED_ERROR
#define logger_error(msg) __logger_inner_error(__LINE__, __FILE__, msg)
#else
#define logger_error(msg)
#endif
#if __LOGGER_ENABLED_WARNING
#define logger_warning(msg) __logger_inner_warning(__LINE__, __FILE__, msg)
#else
#define logger_warning(msg)
#endif
#if __LOGGER_ENABLED_INFO
#define logger_info(msg) __logger_inner_info(__LINE__, __FILE__, msg)
#else
#define logger_info(msg)
#endif
#if __LOGGER_ENABLED_DEBUG
#define logger_debug(msg) __logger_inner_debug(__LINE__, __FILE__, msg)
#else
#define logger_debug(msg)
#endif
extern inline void __logger_inner_fatal(const int line, const char* file, const char* msg) {
time_t t = time(NULL);
struct tm* ts = localtime(&t);
printf(
"\e[1m\e[38:5:88m[%02d:%02d:%02d][FATAL]\e[34m %s:%d\e[0m %s\n",
ts->tm_hour,
ts->tm_min,
ts->tm_sec,
file,
line,
msg
);
}
extern inline void __logger_inner_error(const int line, const char* file, const char* msg) {
time_t t = time(NULL);
struct tm* ts = localtime(&t);
printf(
"\e[1m\e[91m[%02d:%02d:%02d][ERROR]\e[34m %s:%d\e[0m %s\n",
ts->tm_hour,
ts->tm_min,
ts->tm_sec,
file,
line,
msg
);
}
extern inline void __logger_inner_warning(const int line, const char* file, const char* msg) {
time_t t = time(NULL);
struct tm* ts = localtime(&t);
printf(
"\e[1m\e[33m[%02d:%02d:%02d][WARNING]\e[34m %s:%d\e[0m %s\n",
ts->tm_hour,
ts->tm_min,
ts->tm_sec,
file,
line,
msg
);
}
extern inline void __logger_inner_info(const int line, const char* file, const char* msg) {
time_t t = time(NULL);
struct tm* ts = localtime(&t);
printf(
"\e[1m\e[36m[%02d:%02d:%02d][INFO]\e[34m %s:%d\e[0m %s\n",
ts->tm_hour,
ts->tm_min,
ts->tm_sec,
file,
line,
msg
);
}
extern inline void __logger_inner_debug(const int line, const char* file, const char* msg) {
time_t t = time(NULL);
struct tm* ts = localtime(&t);
printf(
"\e[1m\e[32m[%02d:%02d:%02d][DEBUG]\e[34m %s:%d\e[0m %s\n",
ts->tm_hour,
ts->tm_min,
ts->tm_sec,
file,
line,
msg
);
}
#endif