-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.c
374 lines (296 loc) · 8.76 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* BIRD Library -- Logging Functions
*
* (c) 1998--2000 Martin Mares <[email protected]>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
/**
* DOC: Logging
*
* The Logging module offers a simple set of functions for writing
* messages to system logs and to the debug output. Message classes
* used by this module are described in |birdlib.h| and also in the
* user's manual.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include "log.h"
#define SYSLOG_NAME_MAX 256
static const char default_syslog_name[] = "ubpf_plugin_manager";
#include "utlist.h"
static struct log_config *current_log_list = NULL;
static char *current_syslog_name; /* NULL -> syslog closed */
static inline struct log_config *new_log_config(void) {
struct log_config *lc = calloc(1, sizeof(struct log_config));
if (!lc) return NULL;
lc->dynamic_alloc = 1;
return lc;
}
static inline int get_time(char *buf_time, size_t len) {
long millisec;
struct tm *tm_info;
struct timeval tv;
gettimeofday(&tv, NULL);
millisec = lrint(tv.tv_usec / 1000.0); // Round to nearest millisec
if (millisec >= 1000) { // Allow for rounding up to nearest second
millisec -= 1000;
tv.tv_sec++;
}
tm_info = localtime(&tv.tv_sec);
strftime(buf_time, len, "%Y-%m-%d %H:%M:%S", tm_info);
return 0;
}
#include <pthread.h>
static pthread_mutex_t log_mutex;
static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); }
static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); }
#include <sys/syslog.h>
static int syslog_priorities[] = {
LOG_DEBUG,
LOG_DEBUG,
LOG_DEBUG,
LOG_INFO,
LOG_ERR,
LOG_WARNING,
LOG_ERR,
LOG_ERR,
LOG_CRIT,
LOG_CRIT
};
static const char *class_names[] = {
"???",
"DBG",
"TRACE",
"INFO",
"RMT",
"WARN",
"ERR",
"AUTH",
"FATAL",
"BUG"
};
static inline off_t
log_size(struct log_config *l) {
struct stat st;
return (!fstat(fileno(l->rf), &st) && S_ISREG(st.st_mode)) ? st.st_size : 0;
}
static void
log_close(struct log_config *l) {
if (l->rf) {
fclose(l->rf);
}
l->rf = NULL;
l->fh = NULL;
}
static int log_open(struct log_config *l) {
l->rf = rf_open(l->filename, "a");
if (!l->rf) {
/* Well, we cannot do much in case of error as log is closed */
l->mask = 0;
return -1;
}
l->fh = l->rf;
l->pos = log_size(l);
return 0;
}
static int
log_rotate(struct log_config *l) {
log_close(l);
/* If we cannot rename the logfile, we at least try to delete it
in order to continue logging and not exceeding logfile size */
if ((rename(l->filename, l->backup) < 0) &&
(unlink(l->filename) < 0)) {
l->mask = 0;
return -1;
}
return log_open(l);
}
/**
* log_commit - commit a log message
* @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
* @buf: message to write
*
* This function writes a message prepared in the log buffer to the
* log file (as specified in the configuration). The log buffer is
* reset after that. The log message is a full line, log_commit()
* terminates it.
*
* The message class is an integer, not a first char of a string like
* in log(), so it should be written like *L_INFO.
*/
static void
log_commit(int class, buffer *buf) {
struct log_config *l;
char tbuf[TM_DATETIME_BUFFER_SIZE];
if (buf->pos == buf->end)
strcpy(buf->end - 100, " ... <too long>");
log_lock();
DL_FOREACH(current_log_list, l) {
if (!(l->mask & (1 << class)))
continue;
if (l->fh) {
if (l->terminal_flag) {
fputs("<DGB (plugin_manager)>: ", l->fh);
} else {
memset(tbuf, 0, sizeof(tbuf));
if (get_time(tbuf, TM_DATETIME_BUFFER_SIZE) == -1) {
strcpy(tbuf, "<error>");
}
if (l->limit) {
off_t msg_len = strnlen(tbuf, sizeof(tbuf)) + strnlen(class_names[class], 5) +
(buf->pos - buf->start) + 5;
if (l->pos < 0)
l->pos = log_size(l);
if (l->pos + msg_len > l->limit)
if (log_rotate(l) < 0)
continue;
l->pos += msg_len;
}
fprintf(l->fh, "%s <%s> ", tbuf, class_names[class]);
}
fputs(buf->start, l->fh);
fputc('\n', l->fh);
fflush(l->fh);
} else {
syslog(syslog_priorities[class], "%s", buf->start);
}
}
log_unlock();
buf->pos = buf->start;
}
static int
buffer_vprint(buffer *buf, const char *fmt, va_list args) {
int i = vsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
if ((i < 0) && (buf->pos < buf->end))
*buf->pos = 0;
buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
return i;
}
static void
vlog(int class, const char *msg, va_list args) {
buffer buf;
LOG_BUFFER_INIT(buf);
buffer_vprint(&buf, msg, args);
log_commit(class, &buf);
}
/**
* log - log a message
* @msg: printf-like formatting string with message class information
* prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
*
* This function formats a message according to the format string @msg
* and writes it to the corresponding log file (as specified in the
* configuration). Please note that the message is automatically
* formatted as a full line, no need to include |\n| inside.
* It is essentially a sequence of log_reset(), logn() and log_commit().
*/
void
msg_log(const char *msg, ...) {
int class = 1;
va_list args;
va_start(args, msg);
if (*msg >= 1 && *msg <= 8)
class = *msg++;
vlog(class, msg, args);
va_end(args);
}
static struct log_config *
default_log_list(int stderr_dbg) {
static struct log_config *log_list = NULL;
struct log_config *l, *tmp;
if (log_list != NULL) {
/* flush the default log list */
DL_FOREACH_SAFE(log_list, l, tmp) {
DL_DELETE(log_list, l); /* lc_syslog and lc_stderr are static vars */
}
log_list = NULL;
}
// config syslog
static struct log_config lc_syslog = {
.mask = ~0u,
};
DL_APPEND(log_list, &lc_syslog);
// end config syslog
// config debug to stderr
if (stderr_dbg) {
static struct log_config lc_stderr;
lc_stderr = (struct log_config) {
.mask = ~0u,
.terminal_flag = 1,
.fh = stderr
};
DL_APPEND(log_list, &lc_stderr);
}
// end config debug to stderr
return log_list;
}
struct log_config *add_log_entry(struct log_config **logs, const char *file_path, int mask) {
struct log_config *n_log;
n_log = new_log_config();
if (!n_log) return NULL;
DL_APPEND(*logs, n_log);
n_log->mask = mask;
n_log->filename = file_path;
return *logs;
}
static inline void append_logs(struct log_config *dest_logs, struct log_config *src_logs) {
struct log_config *l;
DL_FOREACH(src_logs, l) {
DL_APPEND(dest_logs, l);
}
}
void logs_close() {
struct log_config *l, *tmp;
if (current_log_list) {
DL_FOREACH_SAFE(current_log_list, l, tmp) {
DL_DELETE(current_log_list, l);
if (l->rf) {
log_close(l);
}
if (l->dynamic_alloc) {
free(l);
}
}
current_log_list = NULL;
}
if (current_syslog_name) {
closelog();
free(current_syslog_name);
current_syslog_name = NULL;
}
}
void
log_init(int dbg, const char *new_syslog_name, struct log_config *extra_log) {
struct log_config *l;
struct log_config *logs = NULL;
/* We should not manipulate with log list when other threads may use it */
log_lock();
logs = default_log_list(dbg);
append_logs(logs, extra_log);
/* Open the logs, needed for 'configure undo' */
DL_FOREACH(logs, l) {
if (l->filename && !l->rf) {
log_open(l);
}
}
current_log_list = logs;
/* syslog open */
if (new_syslog_name) {
current_syslog_name = strndup(new_syslog_name, SYSLOG_NAME_MAX);
} else {
current_syslog_name = strndup(default_syslog_name, sizeof(default_syslog_name));
}
openlog(current_syslog_name, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
/* Logs exchange done, let the threads log as before */
log_unlock();
}