-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.h
87 lines (75 loc) · 3.37 KB
/
logging.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
#ifndef _JIMCORE_LOGGING_H_
#define _JIMCORE_LOGGING_H_
enum log_level {
LOG_LEVEL_FATAL,
LOG_LEVEL_ERROR,
LOG_LEVEL_INFO,
LOG_LEVEL_DEBUG,
};
extern char *argv0;
extern bool daemonized;
extern bool debug;
extern enum log_level log_level;
#define _log_stdout(lev, fmt, args...) \
do { \
if (lev == LOG_LEVEL_FATAL) \
fprintf(stderr, "%s: " fmt "\n", argv0, ##args); \
else \
fprintf(stdout, "-- " fmt "\n", ##args); \
} while (0)
#define _log_syslog(lev, fmt, args...) \
do { \
int priority = LOG_ALERT; \
switch (lev) { \
case LOG_LEVEL_DEBUG: \
priority = LOG_DEBUG; \
break; \
case LOG_LEVEL_INFO: \
priority = LOG_INFO; \
break; \
case LOG_LEVEL_ERROR: \
priority = LOG_ERR; \
break; \
case LOG_LEVEL_FATAL: \
priority = LOG_ALERT; \
break; \
} \
syslog(priority, fmt, ##args); \
} while (0) \
#define _log_impl(lev, fmt, args...) \
do { \
if (daemonized) \
_log_syslog(lev, fmt, ##args); \
else \
_log_stdout(lev, fmt, ##args); \
} while (0)
#ifdef LOG_WITH_POSITION
#define _log(lev, fmt, args...) \
_log_impl(lev, "%s:%d %s(): " fmt, \
__FILE__, __LINE__, __FUNCTION__, ##args)
#else
#define _log(lev, fmt, args...) \
_log_impl(lev, fmt, ##args)
#endif
#define log_err(fmt, args...) \
_log(LOG_LEVEL_ERROR, fmt, ##args)
#define log(fmt, args...) \
_log(LOG_LEVEL_INFO, fmt, ##args)
#define vlog(fmt, args...) \
do { \
if (log_level > LOG_LEVEL_FATAL) \
_log(LOG_LEVEL_DEBUG, fmt, ##args); \
} while (0)
#define vvlog(fmt, args...) \
do { \
if (log_level > LOG_LEVEL_ERROR) \
_log(LOG_LEVEL_DEBUG, fmt, ##args); \
} while (0)
#define fatal(fmt, args...) \
do { \
_log(LOG_LEVEL_FATAL, fmt, ##args); \
if (debug) \
abort(); \
_exit(1); \
} while (0)
#endif