-
Notifications
You must be signed in to change notification settings - Fork 8
/
log.c
340 lines (300 loc) · 7.84 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/**
* @file
* Log subsystem implementation.
*/
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#ifdef CONFIG_ERRORS_FATAL
#ifdef NDEBUG
#error CONFIG_ERRORS_FATAL and NDEBUG cannot co-exist
#endif
#include <assert.h>
#endif
#include <utils/log.h>
#include <utils/macros.h>
/** Log subsystem private data. */
static struct {
char name[32];
size_t nr_loggers;
const struct logger *loggers[8];
} priv = {
.name = "undefined",
};
/** Per-thread log message prefix. */
static __thread char priv_prefix[32];
int log_init(const char *format, ...)
{
va_list ap;
int rc;
va_start(ap, format);
rc = vsnprintf(priv.name, sizeof(priv.name), format, ap);
rc = (size_t)rc >= sizeof(priv.name) ? -ENAMETOOLONG : 0;
va_end(ap);
priv.nr_loggers = 0;
return rc;
}
int log_init_prefix(const char *format, ...)
{
va_list ap;
int rc;
va_start(ap, format);
rc = vsnprintf(priv_prefix, sizeof(priv_prefix), format, ap);
rc = (size_t)rc >= sizeof(priv_prefix) ? -ENAMETOOLONG : 0;
va_end(ap);
return rc;
}
int log_attach(const struct logger *logger, void *opaque, unsigned int flags)
{
for (size_t i = 0; i < priv.nr_loggers; i++) {
if (priv.loggers[i] == logger) {
if (!priv.loggers[i]->reinit)
return -ENOTSUP;
return priv.loggers[i]->reinit(opaque, flags);
}
}
if (priv.nr_loggers >= sizeof_array(priv.loggers))
return -ENOMEM;
if (!logger->log)
return -EINVAL;
if (logger->init) {
int rc;
rc = logger->init(opaque, flags);
if (rc)
return rc;
}
priv.loggers[priv.nr_loggers++] = logger;
return 0;
}
/** Stringify a log level. */
static const char *str_log_level(enum log_level level)
{
static const char * const str[] = {
[LOG_LEVEL_ERROR] = "ERROR",
[LOG_LEVEL_WARNING] = "WARNING",
[LOG_LEVEL_INFO] = "INFO",
[LOG_LEVEL_DEBUG] = "DEBUG",
[LOG_LEVEL_END] = "UNKNOWN",
};
if (level >= LOG_LEVEL_END)
level = LOG_LEVEL_END;
return str[level];
}
/** Stringify an error number. */
static const char *str_error_number(int error_number)
{
static __thread char buf[64];
snprintf(buf, sizeof(buf), "error_number=%d", error_number);
return buf;
}
/** Log on stdout from within the log subsystem. */
static __printf(1, 2) int internal_log(const char *format, ...)
{
va_list ap;
int rc;
char message[LOG_MESSAGE_LENGTH_MAX];
va_start(ap, format);
rc = vsnprintf(message, sizeof(message), format, ap);
va_end(ap);
printf("%s%s\n",
(size_t)rc >= sizeof(message) ? "(TRUNCATED) " : "", message);
fflush(stdout);
return 0;
}
/** Log on stdout. */
static int stdout_log(enum log_level level, const char *message)
{
(void)level;
printf("%s", message);
fflush(stdout);
return 0;
}
/**
* Generate a log message.
*
* Format:
* \<rfc-3339 datetime\>, \<plugin\>\<plugin-instance\>, \<log level\>,
* \<file\> +\<line\>: \<function\>(): [\<prefix\>\: ]...[:\<error\>]
*/
static void _log(enum log_level level, const char *file, int line,
const char *func, const char *format, va_list ap,
int error_number)
{
struct timespec ts;
time_t ts_s;
unsigned long ts_us;
struct tm tm;
void *p;
int rc;
char message[LOG_MESSAGE_LENGTH_MAX];
size_t message_length = 0;
size_t t_len;
bool logged = false;
/* time */
rc = clock_gettime(CLOCK_REALTIME, &ts);
assert(!rc);
ts_s = ts.tv_sec;
p = gmtime_r(&ts_s, &tm);
assert(p);
t_len = strftime(&message[message_length],
sizeof(message) - message_length, "%Y-%m-%dT%T", &tm);
if (!t_len) {
internal_log("strftime() failed");
rc = 0;
}
message_length += t_len;
ts_us = ts.tv_nsec / 1000;
rc = snprintf(&message[message_length],
sizeof(message) - message_length, ".%06luZ, ", ts_us);
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() us failed");
rc = 0;
}
message_length += rc;
/* name */
rc = snprintf(&message[message_length],
sizeof(message) - message_length, "%s, ", priv.name);
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() name failed");
rc = 0;
}
message_length += rc;
/* log level */
rc = snprintf(&message[message_length],
sizeof(message) - message_length,
"%s, ", str_log_level(level));
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() log level failed");
rc = 0;
}
message_length += rc;
/* file, line, func */
rc = snprintf(&message[message_length],
sizeof(message) - message_length,
"%s +%d: %s(): ", file, line, func);
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() file +line: func() failed");
rc = 0;
}
message_length += rc;
/* prefix */
if (priv_prefix[0]) {
rc = snprintf(&message[message_length],
sizeof(message) - message_length,
"%s: ", priv_prefix);
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() prefix failed");
rc = 0;
}
message_length += rc;
}
/* message */
rc = vsnprintf(&message[message_length],
sizeof(message) - message_length, format, ap);
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("vsnprintf() message failed");
rc = 0;
}
message_length += rc;
/* error message */
if (error_number) {
rc = snprintf(&message[message_length],
sizeof(message) - message_length, ": %s",
str_error_number(error_number));
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() error message failed");
rc = 0;
}
message_length += rc;
}
/* newline */
rc = snprintf(&message[message_length],
sizeof(message) - message_length,
"\n");
if ((size_t)rc >= sizeof(message) - message_length) {
internal_log("snprintf() \\n failed");
rc = 0;
}
message_length += rc;
for (size_t i = 0; i < priv.nr_loggers; i++) {
int rc;
rc = priv.loggers[i]->log(level, message);
if (!rc) {
logged = true;
continue;
}
internal_log("Could not log a message via the %s logger: %s",
priv.loggers[i]->name, str_error_number(-rc));
}
if (!logged)
stdout_log(level, message);
}
void _log_error(const char *file, int line, const char *func,
const char *format, ...)
{
va_list ap;
int error_number = errno;
#ifdef CONFIG_ERRORS_FATAL
assert(false);
#endif
va_start(ap, format);
_log(LOG_LEVEL_ERROR, file, line, func, format, ap, error_number);
va_end(ap);
errno = error_number;
}
void _log_warning(const char *file, int line, const char *func,
const char *format, ...)
{
va_list ap;
int error_number = errno;
va_start(ap, format);
_log(LOG_LEVEL_WARNING, file, line, func, format, ap, error_number);
va_end(ap);
errno = error_number;
}
void _log_info(const char *file, int line, const char *func,
const char *format, ...)
{
va_list ap;
int error_number = errno;
va_start(ap, format);
_log(LOG_LEVEL_INFO, file, line, func, format, ap, error_number);
va_end(ap);
errno = error_number;
}
void _log_debug(const char *file, int line, const char *func,
const char *format, ...)
{
va_list ap;
int error_number = errno;
va_start(ap, format);
_log(LOG_LEVEL_DEBUG, file, line, func, format, ap, error_number);
va_end(ap);
errno = error_number;
}
/** stdout logger. */
const struct logger logger_stdout = {
.name = "stdout",
.log = stdout_log,
};
/** Default logger. */
const struct logger *logger_default = &logger_stdout;