-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.c
217 lines (182 loc) · 5.48 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "log.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <assert.h>
#define ALEN(v) (sizeof(v) / sizeof((v)[0]))
#define UNUSED __attribute__((unused))
static bool colorize = false;
static bool do_syslog = true;
static enum log_class log_level = LOG_CLASS_NONE;
static const struct {
const char name[8];
const char log_prefix[7];
uint8_t color;
int syslog_equivalent;
} log_level_map[] = {
[LOG_CLASS_NONE] = {"none", "none", 5, -1},
[LOG_CLASS_ERROR] = {"error", " err", 31, LOG_ERR},
[LOG_CLASS_WARNING] = {"warning", "warn", 33, LOG_WARNING},
[LOG_CLASS_INFO] = {"info", "info", 97, LOG_INFO},
[LOG_CLASS_DEBUG] = {"debug", " dbg", 36, LOG_DEBUG},
};
void
log_init(enum log_colorize _colorize, bool _do_syslog,
enum log_facility syslog_facility, enum log_class _log_level)
{
static const int facility_map[] = {
[LOG_FACILITY_USER] = LOG_USER,
[LOG_FACILITY_DAEMON] = LOG_DAEMON,
};
colorize = _colorize == LOG_COLORIZE_NEVER ? false : _colorize == LOG_COLORIZE_ALWAYS ? true : isatty(STDERR_FILENO);
do_syslog = _do_syslog;
log_level = _log_level;
int slvl = log_level_map[_log_level].syslog_equivalent;
if (do_syslog && slvl != -1) {
openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]);
setlogmask(LOG_UPTO(slvl));
}
}
void
log_deinit(void)
{
if (do_syslog)
closelog();
}
static void
_log(enum log_class log_class, const char *module, const char *file, int lineno,
const char *fmt, int sys_errno, va_list va)
{
assert(log_class > LOG_CLASS_NONE);
assert(log_class < ALEN(log_level_map));
if (log_class > log_level)
return;
const char *prefix = log_level_map[log_class].log_prefix;
unsigned int class_clr = log_level_map[log_class].color;
char clr[16];
snprintf(clr, sizeof(clr), "\033[%um", class_clr);
fprintf(stderr, "%s%s%s: ", colorize ? clr : "", prefix, colorize ? "\033[0m" : "");
if (colorize)
fputs("\033[2m", stderr);
fprintf(stderr, "%s:%d: ", file, lineno);
if (colorize)
fputs("\033[0m", stderr);
vfprintf(stderr, fmt, va);
if (sys_errno != 0)
fprintf(stderr, ": %s (%d)", strerror(sys_errno), sys_errno);
fputc('\n', stderr);
}
static void
_sys_log(enum log_class log_class, const char *module,
const char UNUSED *file, int UNUSED lineno,
const char *fmt, int sys_errno, va_list va)
{
assert(log_class > LOG_CLASS_NONE);
assert(log_class < ALEN(log_level_map));
if (!do_syslog)
return;
/* Map our log level to syslog's level */
int level = log_level_map[log_class].syslog_equivalent;
char msg[4096];
int n = vsnprintf(msg, sizeof(msg), fmt, va);
assert(n >= 0);
if (sys_errno != 0 && (size_t)n < sizeof(msg))
snprintf(msg + n, sizeof(msg) - n, ": %s", strerror(sys_errno));
syslog(level, "%s: %s", module, msg);
}
void
log_msg_va(enum log_class log_class, const char *module,
const char *file, int lineno, const char *fmt, va_list va)
{
va_list va2;
va_copy(va2, va);
_log(log_class, module, file, lineno, fmt, 0, va);
_sys_log(log_class, module, file, lineno, fmt, 0, va2);
va_end(va2);
}
void
log_msg(enum log_class log_class, const char *module,
const char *file, int lineno, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
log_msg_va(log_class, module, file, lineno, fmt, va);
va_end(va);
}
void
log_errno_va(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, va_list va)
{
log_errno_provided_va(log_class, module, file, lineno, errno, fmt, va);
}
void
log_errno(enum log_class log_class, const char *module,
const char *file, int lineno,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
log_errno_va(log_class, module, file, lineno, fmt, va);
va_end(va);
}
void
log_errno_provided_va(enum log_class log_class, const char *module,
const char *file, int lineno, int errno_copy,
const char *fmt, va_list va)
{
va_list va2;
va_copy(va2, va);
_log(log_class, module, file, lineno, fmt, errno_copy, va);
_sys_log(log_class, module, file, lineno, fmt, errno_copy, va2);
va_end(va2);
}
void
log_errno_provided(enum log_class log_class, const char *module,
const char *file, int lineno, int errno_copy,
const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
log_errno_provided_va(log_class, module, file, lineno, errno_copy, fmt, va);
va_end(va);
}
static size_t
map_len(void)
{
size_t len = ALEN(log_level_map);
#ifndef _DEBUG
/* Exclude "debug" entry for non-debug builds */
len--;
#endif
return len;
}
int
log_level_from_string(const char *str)
{
if (str[0] == '\0')
return -1;
for (int i = 0, n = map_len(); i < n; i++)
if (strcmp(str, log_level_map[i].name) == 0)
return i;
return -1;
}
const char *
log_level_string_hint(void)
{
static char buf[64];
if (buf[0] != '\0')
return buf;
for (size_t i = 0, pos = 0, n = map_len(); i < n; i++) {
const char *entry = log_level_map[i].name;
const char *delim = (i + 1 < n) ? ", " : "";
pos += snprintf(buf + pos, sizeof(buf) - pos, "'%s'%s", entry, delim);
}
return buf;
}