-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.h
96 lines (83 loc) · 3.39 KB
/
log.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
#ifndef LOG_H
#define LOG_H
#define LOG_FATAL (1)
#define LOG_ERR (2)
#define LOG_WARN (3)
#define LOG_INFO (4)
#define LOG_DBG (5)
#define LOG_VERBOSE (6)
#include <stdio.h>
#include <string.h>
#include <time.h>
#define LOG_LEVEL LOG_VERBOSE
static inline void log_timestamp(FILE* stream) {
char buff[20];
struct tm *sTm;
time_t now = time (0);
sTm = gmtime (&now);
strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
fprintf(stream, "%s ", buff);
}
static inline void log_level_str(int level, FILE* stream) {
switch (level) {
case LOG_FATAL:
fprintf(stream, "[FATAL] ");
break;
case LOG_ERR:
fprintf(stream, "[ERROR] ");
break;
case LOG_WARN:
fprintf(stream, "[WARN] ");
break;
case LOG_INFO:
fprintf(stream, "[INFO] ");
break;
case LOG_DBG:
fprintf(stream, "[DEBUG] ");
break;
case LOG_VERBOSE:
fprintf(stream, "[VERBOSE] ");
break;
default:
fprintf(stream, "[UNKNOWN LOG LEVEL] ");
break;
}
}
#define LOG(level, stream, is_continuation, should_break_line, ...) do { \
if (level <= LOG_LEVEL) { \
if (!is_continuation) { \
log_level_str(level, stream); \
log_timestamp(stream); \
fprintf(stream," (%s:%d): ", __FILE__, __LINE__); \
} \
fprintf(stream, __VA_ARGS__); \
if (should_break_line) { \
fprintf(stream, "\n"); \
} \
} \
} while (0)
#define LOG_F(...) LOG(LOG_FATAL, stderr, 0, 1, __VA_ARGS__)
#define LOG_E(...) LOG(LOG_ERR, stderr, 0, 1, __VA_ARGS__)
#define LOG_W(...) LOG(LOG_WARN, stderr, 0, 1, __VA_ARGS__)
#define LOG_I(...) LOG(LOG_INFO, stdout, 0, 1, __VA_ARGS__)
#define LOG_D(...) LOG(LOG_DBG, stdout, 0, 1, __VA_ARGS__)
#define LOG_V(...) LOG(LOG_VERBOSE, stdout, 0, 1, __VA_ARGS__)
#define LOG_F_TBC(...) LOG(LOG_FATAL, stderr, 0, 0, __VA_ARGS__)
#define LOG_E_TBC(...) LOG(LOG_ERR, stderr, 0, 0, __VA_ARGS__)
#define LOG_W_TBC(...) LOG(LOG_WARN, stderr, 0, 0, __VA_ARGS__)
#define LOG_I_TBC(...) LOG(LOG_INFO, stdout, 0, 0, __VA_ARGS__)
#define LOG_D_TBC(...) LOG(LOG_DBG, stdout, 0, 0, __VA_ARGS__)
#define LOG_V_TBC(...) LOG(LOG_VERBOSE, stdout, 0, 0, __VA_ARGS__)
#define LOG_F_CTBC(...) LOG(LOG_FATAL, stderr, 1, 0, __VA_ARGS__)
#define LOG_E_CTBC(...) LOG(LOG_ERR, stderr, 1, 0, __VA_ARGS__)
#define LOG_W_CTBC(...) LOG(LOG_WARN, stderr, 1, 0, __VA_ARGS__)
#define LOG_I_CTBC(...) LOG(LOG_INFO, stdout, 1, 0, __VA_ARGS__)
#define LOG_D_CTBC(...) LOG(LOG_DBG, stdout, 1, 0, __VA_ARGS__)
#define LOG_V_CTBC(...) LOG(LOG_VERBOSE, stdout, 1, 0, __VA_ARGS__)
#define LOG_F_CFIN(...) LOG(LOG_FATAL, stderr, 1, 1, __VA_ARGS__)
#define LOG_E_CFIN(...) LOG(LOG_ERR, stderr, 1, 1, __VA_ARGS__)
#define LOG_W_CFIN(...) LOG(LOG_WARN, stderr, 1, 1, __VA_ARGS__)
#define LOG_I_CFIN(...) LOG(LOG_INFO, stdout, 1, 1, __VA_ARGS__)
#define LOG_D_CFIN(...) LOG(LOG_DBG, stdout, 1, 1, __VA_ARGS__)
#define LOG_V_CFIN(...) LOG(LOG_VERBOSE, stdout, 1, 1, __VA_ARGS__)
#endif // LOG_H