-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathlog.c
339 lines (297 loc) · 9.52 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
/*
Serval DNA logging
Copyright (C) 2013-2015 Serval Project Inc.
Copyright (C) 2016-2017 Flinders University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <libgen.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <unistd.h>
#include <assert.h>
#include "log.h"
#include "log_output.h"
#include "log_prolog.h"
#include "version_servald.h"
#include "instance.h"
#include "net.h"
#include "os.h"
#include "str.h"
#include "strbuf.h"
#include "strbuf_helpers.h"
#include "xprintf.h"
#include "trigger.h"
/* This thread-local variable is used to support recursion, so that log output operations like
* open() that invoke log primitives will not cause infinite recursion, but instead will log only to
* the output whose iterator they are currently servicing.
*/
static __thread struct log_output_iterator *current_iterator = NULL;
/* By default, if there is no log file configured, this is logged as an INFO message, but a server
* can set this to log it at a higher level.
*/
int serval_log_level_NoLogFileConfigured = LOG_LEVEL_INFO;
/* Primitive operations for log_output_iterator.
*/
static void log_iterator_start(struct log_output_iterator *it)
{
it->output = NULL;
memset(it, 0, sizeof *it);
gettimeofday(&it->tv, NULL);
localtime_r(&it->tv.tv_sec, &it->tm);
it->xpf = XPRINTF_NULL;
}
static int log_iterator_advance(struct log_output_iterator *it)
{
assert(it->output != SECTION_END(logoutput));
do {
it->output = it->output ? it->output + 1 : SECTION_START(logoutput);
} while (it->output != SECTION_END(logoutput) && !*it->output); // skip NULL entries
return it->output != SECTION_END(logoutput);
}
/* In case no outputters are linked, ensure that the logoutput section exists, by adding a NULL
* entry to it. Otherwise you get link errors like:
* log.c:193: error: undefined reference to '__stop_logoutput'
*/
DEFINE_LOG_OUTPUT(NULL);
/* Functions for formatting log messages.
*/
static void print_line_prefix(struct log_output_iterator *it)
{
assert(it->output);
struct log_output *out = *it->output;
assert(out);
if (out->show_pid(out))
xprintf(it->xpf, "[%5u] ", getpid());
if (out->show_time(out)) {
if (it->tv.tv_sec == 0) {
xputs("NOTIME______ ", it->xpf);
} else {
char buf[50];
if (strftime(buf, sizeof buf, "%T", &it->tm) == 0)
xputs("EMPTYTIME___ ", it->xpf);
else
xprintf(it->xpf, "%s.%03u ", buf, (unsigned int)it->tv.tv_usec / 1000);
}
}
if (strbuf_len(&log_context)) {
xputs("[", it->xpf);
xputs(strbuf_str(&log_context), it->xpf);
xputs("] ", it->xpf);
}
}
static void whence_prefix(struct log_output_iterator *it, struct __sourceloc whence)
{
assert(!XPRINTF_IS_NULL(it->xpf));
if ((whence.file && whence.file[0]) || (whence.function && whence.function[0])) {
xprint_sourceloc(it->xpf, whence);
xputs(" ", it->xpf);
}
}
/* Log output operations.
*/
static void log_open(struct log_output_iterator *it)
{
assert(it->output);
assert(*it->output);
if ((*it->output)->open)
(*it->output)->open(it);
}
static bool_t is_log_available(struct log_output_iterator *it)
{
assert(it->output);
assert(*it->output);
return !(*it->output)->is_available || (*it->output)->is_available(it);
}
static void log_start_line(struct log_output_iterator *it, int level)
{
assert(level >= LOG_LEVEL_SILENT);
assert(level <= LOG_LEVEL_FATAL);
assert(XPRINTF_IS_NULL(it->xpf));
assert(it->output);
assert(*it->output);
assert((*it->output)->start_line);
(*it->output)->start_line(it, level);
assert(!XPRINTF_IS_NULL(it->xpf));
print_line_prefix(it);
}
static void log_end_line(struct log_output_iterator *it, int level)
{
assert(level >= LOG_LEVEL_SILENT);
assert(level <= LOG_LEVEL_FATAL);
assert(!XPRINTF_IS_NULL(it->xpf));
assert(it->output);
assert(*it->output);
if ((*it->output)->end_line)
(*it->output)->end_line(it, level);
it->xpf = XPRINTF_NULL;
}
static void log_flush(struct log_output_iterator *it)
{
assert(it->output);
assert(*it->output);
if ((*it->output)->flush)
(*it->output)->flush(it);
}
static void log_close(struct log_output_iterator *it)
{
assert(it->output);
assert(*it->output);
if ((*it->output)->close)
(*it->output)->close(it);
}
static void log_capture_fd(struct log_output_iterator *it, int fd, bool_t *captured)
{
assert(it->output);
assert(*it->output);
if ((*it->output)->capture_fd)
(*it->output)->capture_fd(it, fd, captured);
}
/* Functions for use by log outputters. This is the "private" API of the logging system, as
* described in "log_output.h".
*/
const char *serval_log_level_prefix_string(int level)
{
switch (level) {
case LOG_LEVEL_FATAL: return "FATAL:";
case LOG_LEVEL_ERROR: return "ERROR:";
case LOG_LEVEL_WARN: return "WARN: ";
case LOG_LEVEL_HINT: return "HINT: ";
case LOG_LEVEL_INFO: return "INFO: ";
case LOG_LEVEL_DEBUG: return "DEBUG:";
default: return "UNKWN:";
}
}
void serval_log_output_iterator_vprintf_nl(struct log_output_iterator *it, int level, const char *fmt, va_list ap)
{
log_start_line(it, level);
vxprintf(it->xpf, fmt, ap);
log_end_line(it, level);
}
void serval_log_output_iterator_printf_nl(struct log_output_iterator *it, int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
serval_log_output_iterator_vprintf_nl(it, level, fmt, ap);
va_end(ap);
}
static void print_datetime(struct log_output_iterator *it, int level)
{
char buf[50];
if (strftime(buf, sizeof buf, "%F %T %z", &it->tm)) {
serval_log_output_iterator_printf_nl(it, level, "Local date/time: %s", buf);
(*it->output)->last_tm = it->tm;
}
}
static void print_newdate(struct log_output_iterator *it)
{
struct log_output *out = *it->output;
if ( it->tm.tm_mday != out->last_tm.tm_mday
|| it->tm.tm_mon != out->last_tm.tm_mon
|| it->tm.tm_year != out->last_tm.tm_year
)
print_datetime(it, LOG_LEVEL_INFO);
}
void serval_log_print_prolog(struct log_output_iterator *it)
{
assert(current_iterator == NULL || current_iterator == it);
struct log_output_iterator *save_current_iterator = current_iterator;
current_iterator = it;
print_datetime(it, LOG_LEVEL_INFO);
CALL_TRIGGER(log_prolog);
current_iterator = save_current_iterator;
}
// Put a dummy no-op trigger callback into the "log_prolog" trigger section, otherwise if no other
// object provides one, the link will fail with errors like:
// undefined reference to `__start_tr_log_prolog'
// undefined reference to `__stop_tr_log_prolog'
static void __dummy_on_log_prolog() {}
DEFINE_TRIGGER(log_prolog, __dummy_on_log_prolog);
/* Private helper functions for formatting and emitting log messages.
*/
static void iterator_vprintf_nl(struct log_output_iterator *it, int level, struct __sourceloc whence, const char *fmt, va_list ap)
{
assert(current_iterator);
log_start_line(it, level);
whence_prefix(it, whence);
vxprintf(it->xpf, fmt, ap);
log_end_line(it, level);
}
/* Logging primitives. This is the "public" API of the logging system, as described in "log.h".
*/
void serval_vlogf(int level, struct __sourceloc whence, const char *fmt, va_list ap)
{
if (level != LOG_LEVEL_SILENT) {
if (current_iterator) {
// This occurs if a log primitive is invoked recursively from within a log output, which is
// typically an open() operation calling serval_log_print_prolog(it). In this case, the log
// output only goes to the single output in question, not to all outputs.
iterator_vprintf_nl(current_iterator, level, whence, fmt, ap);
}
else {
struct log_output_iterator it;
log_iterator_start(&it);
while (log_iterator_advance(&it)) {
struct log_output *out = *it.output;
if (level >= out->minimum_level(out)) {
// If the open() operation recurses by invoking a log primitive directly, then print that
// message to all available outputs but avoid recursing into open() operations that are
// already being called.
if (!out->opening) {
out->opening = 1;
log_open(&it);
out->opening = 0;
}
if (is_log_available(&it)) {
current_iterator = ⁢
print_newdate(&it);
va_list ap1;
va_copy(ap1, ap);
iterator_vprintf_nl(&it, level, whence, fmt, ap1);
va_end(ap1);
log_flush(&it);
current_iterator = NULL;
}
}
}
}
}
}
void serval_log_flush()
{
assert(!current_iterator);
struct log_output_iterator it;
log_iterator_start(&it);
while (log_iterator_advance(&it))
log_flush(&it);
}
void serval_log_close()
{
assert(!current_iterator);
struct log_output_iterator it;
log_iterator_start(&it);
while (log_iterator_advance(&it))
log_close(&it);
}
bool_t serval_log_capture_fd(int fd) {
assert(!current_iterator);
struct log_output_iterator it;
log_iterator_start(&it);
bool_t captured = 0;
while (log_iterator_advance(&it))
log_capture_fd(&it, fd, &captured);
return captured;
}