]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "vlog.h" | |
19 | #include <assert.h> | |
20 | #include <ctype.h> | |
21 | #include <errno.h> | |
22 | #include <stdarg.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
68cb8aaf | 25 | #include <sys/stat.h> |
064af421 BP |
26 | #include <sys/types.h> |
27 | #include <syslog.h> | |
28 | #include <time.h> | |
29 | #include <unistd.h> | |
30 | #include "dirs.h" | |
31 | #include "dynamic-string.h" | |
32 | #include "sat-math.h" | |
8628b0b7 | 33 | #include "svec.h" |
064af421 BP |
34 | #include "timeval.h" |
35 | #include "unixctl.h" | |
36 | #include "util.h" | |
37 | ||
d98e6007 | 38 | VLOG_DEFINE_THIS_MODULE(vlog); |
064af421 BP |
39 | |
40 | /* Name for each logging level. */ | |
41 | static const char *level_names[VLL_N_LEVELS] = { | |
42 | #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) #NAME, | |
43 | VLOG_LEVELS | |
44 | #undef VLOG_LEVEL | |
45 | }; | |
46 | ||
47 | /* Syslog value for each logging level. */ | |
48 | static int syslog_levels[VLL_N_LEVELS] = { | |
49 | #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) SYSLOG_LEVEL, | |
50 | VLOG_LEVELS | |
51 | #undef VLOG_LEVEL | |
52 | }; | |
53 | ||
480ce8ab BP |
54 | /* The log modules. */ |
55 | #if USE_LINKER_SECTIONS | |
56 | extern struct vlog_module *__start_vlog_modules[]; | |
57 | extern struct vlog_module *__stop_vlog_modules[]; | |
58 | #define vlog_modules __start_vlog_modules | |
59 | #define n_vlog_modules (__stop_vlog_modules - __start_vlog_modules) | |
60 | #else | |
61 | #define VLOG_MODULE VLOG_DEFINE_MODULE__ | |
62 | #include "vlog-modules.def" | |
63 | #undef VLOG_MODULE | |
64 | ||
65 | struct vlog_module *vlog_modules[] = { | |
66 | #define VLOG_MODULE(NAME) &VLM_##NAME, | |
064af421 BP |
67 | #include "vlog-modules.def" |
68 | #undef VLOG_MODULE | |
69 | }; | |
480ce8ab BP |
70 | #define n_vlog_modules ARRAY_SIZE(vlog_modules) |
71 | #endif | |
064af421 BP |
72 | |
73 | /* Information about each facility. */ | |
74 | struct facility { | |
75 | const char *name; /* Name. */ | |
76 | char *pattern; /* Current pattern. */ | |
77 | bool default_pattern; /* Whether current pattern is the default. */ | |
78 | }; | |
79 | static struct facility facilities[VLF_N_FACILITIES] = { | |
80 | #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true}, | |
81 | VLOG_FACILITIES | |
82 | #undef VLOG_FACILITY | |
83 | }; | |
84 | ||
064af421 BP |
85 | /* VLF_FILE configuration. */ |
86 | static char *log_file_name; | |
87 | static FILE *log_file; | |
88 | ||
1e8cf0f7 BP |
89 | /* vlog initialized? */ |
90 | static bool vlog_inited; | |
91 | ||
480ce8ab | 92 | static void format_log_message(const struct vlog_module *, enum vlog_level, |
064af421 BP |
93 | enum vlog_facility, unsigned int msg_num, |
94 | const char *message, va_list, struct ds *) | |
95 | PRINTF_FORMAT(5, 0); | |
96 | ||
97 | /* Searches the 'n_names' in 'names'. Returns the index of a match for | |
98 | * 'target', or 'n_names' if no name matches. */ | |
99 | static size_t | |
d295e8e9 | 100 | search_name_array(const char *target, const char **names, size_t n_names) |
064af421 BP |
101 | { |
102 | size_t i; | |
103 | ||
104 | for (i = 0; i < n_names; i++) { | |
105 | assert(names[i]); | |
106 | if (!strcasecmp(names[i], target)) { | |
107 | break; | |
108 | } | |
109 | } | |
110 | return i; | |
111 | } | |
112 | ||
113 | /* Returns the name for logging level 'level'. */ | |
114 | const char * | |
115 | vlog_get_level_name(enum vlog_level level) | |
116 | { | |
117 | assert(level < VLL_N_LEVELS); | |
118 | return level_names[level]; | |
119 | } | |
120 | ||
121 | /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name' | |
122 | * is not the name of a logging level. */ | |
123 | enum vlog_level | |
d295e8e9 | 124 | vlog_get_level_val(const char *name) |
064af421 BP |
125 | { |
126 | return search_name_array(name, level_names, ARRAY_SIZE(level_names)); | |
127 | } | |
128 | ||
129 | /* Returns the name for logging facility 'facility'. */ | |
130 | const char * | |
d295e8e9 | 131 | vlog_get_facility_name(enum vlog_facility facility) |
064af421 BP |
132 | { |
133 | assert(facility < VLF_N_FACILITIES); | |
134 | return facilities[facility].name; | |
135 | } | |
136 | ||
137 | /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is | |
138 | * not the name of a logging facility. */ | |
139 | enum vlog_facility | |
d295e8e9 | 140 | vlog_get_facility_val(const char *name) |
064af421 BP |
141 | { |
142 | size_t i; | |
143 | ||
144 | for (i = 0; i < VLF_N_FACILITIES; i++) { | |
145 | if (!strcasecmp(facilities[i].name, name)) { | |
146 | break; | |
147 | } | |
148 | } | |
149 | return i; | |
150 | } | |
151 | ||
152 | /* Returns the name for logging module 'module'. */ | |
480ce8ab BP |
153 | const char * |
154 | vlog_get_module_name(const struct vlog_module *module) | |
064af421 | 155 | { |
480ce8ab | 156 | return module->name; |
064af421 BP |
157 | } |
158 | ||
480ce8ab BP |
159 | /* Returns the logging module named 'name', or NULL if 'name' is not the name |
160 | * of a logging module. */ | |
161 | struct vlog_module * | |
162 | vlog_module_from_name(const char *name) | |
064af421 | 163 | { |
480ce8ab BP |
164 | struct vlog_module **mp; |
165 | ||
166 | for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) { | |
167 | if (!strcasecmp(name, (*mp)->name)) { | |
168 | return *mp; | |
169 | } | |
170 | } | |
171 | return NULL; | |
064af421 BP |
172 | } |
173 | ||
174 | /* Returns the current logging level for the given 'module' and 'facility'. */ | |
175 | enum vlog_level | |
d295e8e9 | 176 | vlog_get_level(const struct vlog_module *module, enum vlog_facility facility) |
064af421 | 177 | { |
064af421 | 178 | assert(facility < VLF_N_FACILITIES); |
480ce8ab | 179 | return module->levels[facility]; |
064af421 BP |
180 | } |
181 | ||
182 | static void | |
480ce8ab | 183 | update_min_level(struct vlog_module *module) |
064af421 | 184 | { |
064af421 BP |
185 | enum vlog_facility facility; |
186 | ||
c1a543a8 | 187 | module->min_level = VLL_OFF; |
064af421 BP |
188 | for (facility = 0; facility < VLF_N_FACILITIES; facility++) { |
189 | if (log_file || facility != VLF_FILE) { | |
480ce8ab | 190 | enum vlog_level level = module->levels[facility]; |
56cee53b | 191 | if (level > module->min_level) { |
480ce8ab BP |
192 | module->min_level = level; |
193 | } | |
064af421 BP |
194 | } |
195 | } | |
064af421 BP |
196 | } |
197 | ||
198 | static void | |
480ce8ab | 199 | set_facility_level(enum vlog_facility facility, struct vlog_module *module, |
064af421 BP |
200 | enum vlog_level level) |
201 | { | |
202 | assert(facility >= 0 && facility < VLF_N_FACILITIES); | |
203 | assert(level < VLL_N_LEVELS); | |
204 | ||
480ce8ab BP |
205 | if (!module) { |
206 | struct vlog_module **mp; | |
207 | ||
208 | for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) { | |
209 | (*mp)->levels[facility] = level; | |
210 | update_min_level(*mp); | |
064af421 BP |
211 | } |
212 | } else { | |
480ce8ab | 213 | module->levels[facility] = level; |
064af421 BP |
214 | update_min_level(module); |
215 | } | |
216 | } | |
217 | ||
480ce8ab BP |
218 | /* Sets the logging level for the given 'module' and 'facility' to 'level'. A |
219 | * null 'module' or a 'facility' of VLF_ANY_FACILITY is treated as a wildcard | |
220 | * across all modules or facilities, respectively. */ | |
064af421 | 221 | void |
480ce8ab | 222 | vlog_set_levels(struct vlog_module *module, enum vlog_facility facility, |
d295e8e9 | 223 | enum vlog_level level) |
064af421 BP |
224 | { |
225 | assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY); | |
226 | if (facility == VLF_ANY_FACILITY) { | |
227 | for (facility = 0; facility < VLF_N_FACILITIES; facility++) { | |
228 | set_facility_level(facility, module, level); | |
229 | } | |
230 | } else { | |
231 | set_facility_level(facility, module, level); | |
232 | } | |
233 | } | |
234 | ||
235 | static void | |
d295e8e9 | 236 | do_set_pattern(enum vlog_facility facility, const char *pattern) |
064af421 BP |
237 | { |
238 | struct facility *f = &facilities[facility]; | |
239 | if (!f->default_pattern) { | |
240 | free(f->pattern); | |
241 | } else { | |
242 | f->default_pattern = false; | |
243 | } | |
244 | f->pattern = xstrdup(pattern); | |
245 | } | |
246 | ||
247 | /* Sets the pattern for the given 'facility' to 'pattern'. */ | |
248 | void | |
249 | vlog_set_pattern(enum vlog_facility facility, const char *pattern) | |
250 | { | |
251 | assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY); | |
252 | if (facility == VLF_ANY_FACILITY) { | |
253 | for (facility = 0; facility < VLF_N_FACILITIES; facility++) { | |
254 | do_set_pattern(facility, pattern); | |
255 | } | |
256 | } else { | |
257 | do_set_pattern(facility, pattern); | |
258 | } | |
259 | } | |
260 | ||
261 | /* Returns the name of the log file used by VLF_FILE, or a null pointer if no | |
262 | * log file has been set. (A non-null return value does not assert that the | |
263 | * named log file is in use: if vlog_set_log_file() or vlog_reopen_log_file() | |
264 | * fails, it still sets the log file name.) */ | |
265 | const char * | |
266 | vlog_get_log_file(void) | |
267 | { | |
268 | return log_file_name; | |
269 | } | |
270 | ||
271 | /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the | |
272 | * default file name if 'file_name' is null. Returns 0 if successful, | |
273 | * otherwise a positive errno value. */ | |
274 | int | |
275 | vlog_set_log_file(const char *file_name) | |
276 | { | |
277 | char *old_log_file_name; | |
480ce8ab | 278 | struct vlog_module **mp; |
064af421 BP |
279 | int error; |
280 | ||
281 | /* Close old log file. */ | |
282 | if (log_file) { | |
283 | VLOG_INFO("closing log file"); | |
284 | fclose(log_file); | |
285 | log_file = NULL; | |
286 | } | |
287 | ||
288 | /* Update log file name and free old name. The ordering is important | |
289 | * because 'file_name' might be 'log_file_name' or some suffix of it. */ | |
290 | old_log_file_name = log_file_name; | |
291 | log_file_name = (file_name | |
292 | ? xstrdup(file_name) | |
b43c6fe2 | 293 | : xasprintf("%s/%s.log", ovs_logdir(), program_name)); |
064af421 BP |
294 | free(old_log_file_name); |
295 | file_name = NULL; /* Might have been freed. */ | |
296 | ||
297 | /* Open new log file and update min_levels[] to reflect whether we actually | |
298 | * have a log_file. */ | |
299 | log_file = fopen(log_file_name, "a"); | |
480ce8ab BP |
300 | for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) { |
301 | update_min_level(*mp); | |
064af421 BP |
302 | } |
303 | ||
304 | /* Log success or failure. */ | |
305 | if (!log_file) { | |
306 | VLOG_WARN("failed to open %s for logging: %s", | |
307 | log_file_name, strerror(errno)); | |
308 | error = errno; | |
309 | } else { | |
310 | VLOG_INFO("opened log file %s", log_file_name); | |
311 | error = 0; | |
312 | } | |
313 | ||
314 | return error; | |
315 | } | |
316 | ||
317 | /* Closes and then attempts to re-open the current log file. (This is useful | |
318 | * just after log rotation, to ensure that the new log file starts being used.) | |
319 | * Returns 0 if successful, otherwise a positive errno value. */ | |
320 | int | |
321 | vlog_reopen_log_file(void) | |
322 | { | |
68cb8aaf BP |
323 | struct stat old, new; |
324 | ||
325 | /* Skip re-opening if there's nothing to reopen. */ | |
326 | if (!log_file_name) { | |
327 | return 0; | |
328 | } | |
329 | ||
330 | /* Skip re-opening if it would be a no-op because the old and new files are | |
331 | * the same. (This avoids writing "closing log file" followed immediately | |
332 | * by "opened log file".) */ | |
333 | if (log_file | |
334 | && !fstat(fileno(log_file), &old) | |
335 | && !stat(log_file_name, &new) | |
336 | && old.st_dev == new.st_dev | |
337 | && old.st_ino == new.st_ino) { | |
338 | return 0; | |
339 | } | |
340 | ||
341 | return vlog_set_log_file(log_file_name); | |
064af421 BP |
342 | } |
343 | ||
2a3e30b2 BP |
344 | /* Set debugging levels. Returns null if successful, otherwise an error |
345 | * message that the caller must free(). */ | |
064af421 BP |
346 | char * |
347 | vlog_set_levels_from_string(const char *s_) | |
348 | { | |
064af421 | 349 | char *s = xstrdup(s_); |
2a3e30b2 BP |
350 | char *save_ptr = NULL; |
351 | char *msg = NULL; | |
352 | char *word; | |
064af421 | 353 | |
2a3e30b2 BP |
354 | word = strtok_r(s, " ,:\t", &save_ptr); |
355 | if (word && !strcasecmp(word, "PATTERN")) { | |
356 | enum vlog_facility facility; | |
064af421 | 357 | |
2a3e30b2 BP |
358 | word = strtok_r(NULL, " ,:\t", &save_ptr); |
359 | if (!word) { | |
360 | msg = xstrdup("missing facility"); | |
361 | goto exit; | |
064af421 BP |
362 | } |
363 | ||
2a3e30b2 BP |
364 | facility = (!strcasecmp(word, "ANY") |
365 | ? VLF_ANY_FACILITY | |
366 | : vlog_get_facility_val(word)); | |
367 | if (facility == VLF_N_FACILITIES) { | |
368 | msg = xasprintf("unknown facility \"%s\"", word); | |
369 | goto exit; | |
370 | } | |
371 | vlog_set_pattern(facility, save_ptr); | |
372 | } else { | |
373 | struct vlog_module *module = NULL; | |
374 | enum vlog_level level = VLL_N_LEVELS; | |
375 | enum vlog_facility facility = VLF_N_FACILITIES; | |
376 | ||
377 | for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) { | |
378 | if (!strcasecmp(word, "ANY")) { | |
379 | continue; | |
380 | } else if (vlog_get_facility_val(word) != VLF_N_FACILITIES) { | |
381 | if (facility != VLF_N_FACILITIES) { | |
382 | msg = xstrdup("cannot specify multiple facilities"); | |
383 | goto exit; | |
064af421 | 384 | } |
2a3e30b2 BP |
385 | facility = vlog_get_facility_val(word); |
386 | } else if (vlog_get_level_val(word) != VLL_N_LEVELS) { | |
387 | if (level != VLL_N_LEVELS) { | |
388 | msg = xstrdup("cannot specify multiple levels"); | |
389 | goto exit; | |
390 | } | |
391 | level = vlog_get_level_val(word); | |
392 | } else if (vlog_module_from_name(word)) { | |
393 | if (module) { | |
394 | msg = xstrdup("cannot specify multiple modules"); | |
395 | goto exit; | |
396 | } | |
397 | module = vlog_module_from_name(word); | |
398 | } else { | |
399 | msg = xasprintf("no facility, level, or module \"%s\"", word); | |
400 | goto exit; | |
064af421 | 401 | } |
2a3e30b2 | 402 | } |
064af421 | 403 | |
2a3e30b2 BP |
404 | if (facility == VLF_N_FACILITIES) { |
405 | facility = VLF_ANY_FACILITY; | |
406 | } | |
407 | if (level == VLL_N_LEVELS) { | |
408 | level = VLL_DBG; | |
064af421 | 409 | } |
2a3e30b2 | 410 | vlog_set_levels(module, facility, level); |
064af421 | 411 | } |
2a3e30b2 BP |
412 | |
413 | exit: | |
064af421 | 414 | free(s); |
2a3e30b2 | 415 | return msg; |
064af421 BP |
416 | } |
417 | ||
418 | /* If 'arg' is null, configure maximum verbosity. Otherwise, sets | |
419 | * configuration according to 'arg' (see vlog_set_levels_from_string()). */ | |
420 | void | |
421 | vlog_set_verbosity(const char *arg) | |
422 | { | |
423 | if (arg) { | |
424 | char *msg = vlog_set_levels_from_string(arg); | |
425 | if (msg) { | |
426 | ovs_fatal(0, "processing \"%s\": %s", arg, msg); | |
427 | } | |
428 | } else { | |
480ce8ab | 429 | vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_DBG); |
064af421 BP |
430 | } |
431 | } | |
432 | ||
433 | static void | |
0e15264f BP |
434 | vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[], |
435 | void *aux OVS_UNUSED) | |
064af421 | 436 | { |
0e15264f BP |
437 | int i; |
438 | ||
439 | for (i = 1; i < argc; i++) { | |
440 | char *msg = vlog_set_levels_from_string(argv[i]); | |
441 | if (msg) { | |
bde9f75d | 442 | unixctl_command_reply_error(conn, msg); |
0e15264f BP |
443 | free(msg); |
444 | return; | |
445 | } | |
446 | } | |
bde9f75d | 447 | unixctl_command_reply(conn, NULL); |
064af421 BP |
448 | } |
449 | ||
450 | static void | |
0e15264f BP |
451 | vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED, |
452 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
064af421 BP |
453 | { |
454 | char *msg = vlog_get_levels(); | |
bde9f75d | 455 | unixctl_command_reply(conn, msg); |
064af421 BP |
456 | free(msg); |
457 | } | |
458 | ||
459 | static void | |
0e15264f BP |
460 | vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED, |
461 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
064af421 BP |
462 | { |
463 | if (log_file_name) { | |
464 | int error = vlog_reopen_log_file(); | |
465 | if (error) { | |
bde9f75d | 466 | unixctl_command_reply_error(conn, strerror(errno)); |
064af421 | 467 | } else { |
bde9f75d | 468 | unixctl_command_reply(conn, NULL); |
064af421 BP |
469 | } |
470 | } else { | |
bde9f75d | 471 | unixctl_command_reply_error(conn, "Logging to file not configured"); |
064af421 BP |
472 | } |
473 | } | |
474 | ||
df5d2ed9 BP |
475 | /* Initializes the logging subsystem and registers its unixctl server |
476 | * commands. */ | |
064af421 | 477 | void |
d295e8e9 | 478 | vlog_init(void) |
064af421 BP |
479 | { |
480 | time_t now; | |
481 | ||
1e8cf0f7 BP |
482 | if (vlog_inited) { |
483 | return; | |
484 | } | |
485 | vlog_inited = true; | |
486 | ||
064af421 | 487 | openlog(program_name, LOG_NDELAY, LOG_DAEMON); |
064af421 | 488 | |
c73814a3 | 489 | now = time_wall(); |
064af421 BP |
490 | if (now < 0) { |
491 | struct tm tm; | |
492 | char s[128]; | |
493 | ||
494 | localtime_r(&now, &tm); | |
495 | strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm); | |
496 | VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now); | |
497 | } | |
498 | ||
0e15264f | 499 | unixctl_command_register( |
2a3e30b2 | 500 | "vlog/set", "{spec | PATTERN:facility:pattern}", |
0e15264f BP |
501 | 1, INT_MAX, vlog_unixctl_set, NULL); |
502 | unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list, NULL); | |
503 | unixctl_command_register("vlog/reopen", "", 0, 0, | |
504 | vlog_unixctl_reopen, NULL); | |
064af421 BP |
505 | } |
506 | ||
507 | /* Closes the logging subsystem. */ | |
508 | void | |
d295e8e9 | 509 | vlog_exit(void) |
064af421 | 510 | { |
1e8cf0f7 BP |
511 | if (vlog_inited) { |
512 | closelog(); | |
513 | vlog_inited = false; | |
514 | } | |
064af421 BP |
515 | } |
516 | ||
517 | /* Print the current logging level for each module. */ | |
518 | char * | |
519 | vlog_get_levels(void) | |
520 | { | |
521 | struct ds s = DS_EMPTY_INITIALIZER; | |
480ce8ab | 522 | struct vlog_module **mp; |
8628b0b7 JP |
523 | struct svec lines = SVEC_EMPTY_INITIALIZER; |
524 | char *line; | |
525 | size_t i; | |
064af421 BP |
526 | |
527 | ds_put_format(&s, " console syslog file\n"); | |
528 | ds_put_format(&s, " ------- ------ ------\n"); | |
529 | ||
480ce8ab | 530 | for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) { |
8628b0b7 | 531 | line = xasprintf("%-16s %4s %4s %4s\n", |
480ce8ab BP |
532 | vlog_get_module_name(*mp), |
533 | vlog_get_level_name(vlog_get_level(*mp, VLF_CONSOLE)), | |
534 | vlog_get_level_name(vlog_get_level(*mp, VLF_SYSLOG)), | |
535 | vlog_get_level_name(vlog_get_level(*mp, VLF_FILE))); | |
8628b0b7 JP |
536 | svec_add_nocopy(&lines, line); |
537 | } | |
538 | ||
539 | svec_sort(&lines); | |
540 | SVEC_FOR_EACH (i, line, &lines) { | |
541 | ds_put_cstr(&s, line); | |
064af421 | 542 | } |
8628b0b7 | 543 | svec_destroy(&lines); |
064af421 BP |
544 | |
545 | return ds_cstr(&s); | |
546 | } | |
547 | ||
548 | /* Returns true if a log message emitted for the given 'module' and 'level' | |
549 | * would cause some log output, false if that module and level are completely | |
550 | * disabled. */ | |
551 | bool | |
480ce8ab | 552 | vlog_is_enabled(const struct vlog_module *module, enum vlog_level level) |
064af421 | 553 | { |
480ce8ab | 554 | return module->min_level >= level; |
064af421 BP |
555 | } |
556 | ||
557 | static const char * | |
558 | fetch_braces(const char *p, const char *def, char *out, size_t out_size) | |
559 | { | |
560 | if (*p == '{') { | |
561 | size_t n = strcspn(p + 1, "}"); | |
562 | size_t n_copy = MIN(n, out_size - 1); | |
563 | memcpy(out, p + 1, n_copy); | |
564 | out[n_copy] = '\0'; | |
565 | p += n + 2; | |
566 | } else { | |
567 | ovs_strlcpy(out, def, out_size); | |
568 | } | |
569 | return p; | |
570 | } | |
571 | ||
572 | static void | |
480ce8ab | 573 | format_log_message(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
574 | enum vlog_facility facility, unsigned int msg_num, |
575 | const char *message, va_list args_, struct ds *s) | |
576 | { | |
577 | char tmp[128]; | |
578 | va_list args; | |
579 | const char *p; | |
580 | ||
581 | ds_clear(s); | |
582 | for (p = facilities[facility].pattern; *p != '\0'; ) { | |
583 | enum { LEFT, RIGHT } justify = RIGHT; | |
584 | int pad = '0'; | |
585 | size_t length, field, used; | |
586 | ||
587 | if (*p != '%') { | |
588 | ds_put_char(s, *p++); | |
589 | continue; | |
590 | } | |
591 | ||
592 | p++; | |
593 | if (*p == '-') { | |
594 | justify = LEFT; | |
595 | p++; | |
596 | } | |
597 | if (*p == '0') { | |
598 | pad = '0'; | |
599 | p++; | |
600 | } | |
601 | field = 0; | |
be2c418b | 602 | while (isdigit((unsigned char)*p)) { |
064af421 BP |
603 | field = (field * 10) + (*p - '0'); |
604 | p++; | |
605 | } | |
606 | ||
607 | length = s->length; | |
608 | switch (*p++) { | |
609 | case 'A': | |
610 | ds_put_cstr(s, program_name); | |
611 | break; | |
612 | case 'c': | |
613 | p = fetch_braces(p, "", tmp, sizeof tmp); | |
614 | ds_put_cstr(s, vlog_get_module_name(module)); | |
615 | break; | |
616 | case 'd': | |
617 | p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp); | |
b5d29991 GS |
618 | ds_put_strftime(s, tmp, false); |
619 | break; | |
620 | case 'D': | |
621 | p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp); | |
622 | ds_put_strftime(s, tmp, true); | |
064af421 BP |
623 | break; |
624 | case 'm': | |
625 | /* Format user-supplied log message and trim trailing new-lines. */ | |
626 | length = s->length; | |
627 | va_copy(args, args_); | |
628 | ds_put_format_valist(s, message, args); | |
629 | va_end(args); | |
630 | while (s->length > length && s->string[s->length - 1] == '\n') { | |
631 | s->length--; | |
632 | } | |
633 | break; | |
634 | case 'N': | |
635 | ds_put_format(s, "%u", msg_num); | |
636 | break; | |
637 | case 'n': | |
638 | ds_put_char(s, '\n'); | |
639 | break; | |
640 | case 'p': | |
641 | ds_put_cstr(s, vlog_get_level_name(level)); | |
642 | break; | |
643 | case 'P': | |
644 | ds_put_format(s, "%ld", (long int) getpid()); | |
645 | break; | |
646 | case 'r': | |
4ae90ff9 | 647 | ds_put_format(s, "%lld", time_msec() - time_boot_msec()); |
064af421 BP |
648 | break; |
649 | default: | |
650 | ds_put_char(s, p[-1]); | |
651 | break; | |
652 | } | |
653 | used = s->length - length; | |
654 | if (used < field) { | |
655 | size_t n_pad = field - used; | |
656 | if (justify == RIGHT) { | |
657 | ds_put_uninit(s, n_pad); | |
658 | memmove(&s->string[length + n_pad], &s->string[length], used); | |
659 | memset(&s->string[length], pad, n_pad); | |
660 | } else { | |
661 | ds_put_char_multiple(s, pad, n_pad); | |
662 | } | |
663 | } | |
664 | } | |
665 | } | |
666 | ||
667 | /* Writes 'message' to the log at the given 'level' and as coming from the | |
668 | * given 'module'. | |
669 | * | |
670 | * Guaranteed to preserve errno. */ | |
671 | void | |
480ce8ab | 672 | vlog_valist(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
673 | const char *message, va_list args) |
674 | { | |
480ce8ab BP |
675 | bool log_to_console = module->levels[VLF_CONSOLE] >= level; |
676 | bool log_to_syslog = module->levels[VLF_SYSLOG] >= level; | |
677 | bool log_to_file = module->levels[VLF_FILE] >= level && log_file; | |
064af421 BP |
678 | if (log_to_console || log_to_syslog || log_to_file) { |
679 | int save_errno = errno; | |
680 | static unsigned int msg_num; | |
681 | struct ds s; | |
682 | ||
1e8cf0f7 BP |
683 | vlog_init(); |
684 | ||
064af421 BP |
685 | ds_init(&s); |
686 | ds_reserve(&s, 1024); | |
687 | msg_num++; | |
688 | ||
689 | if (log_to_console) { | |
690 | format_log_message(module, level, VLF_CONSOLE, msg_num, | |
691 | message, args, &s); | |
692 | ds_put_char(&s, '\n'); | |
693 | fputs(ds_cstr(&s), stderr); | |
694 | } | |
695 | ||
696 | if (log_to_syslog) { | |
697 | int syslog_level = syslog_levels[level]; | |
698 | char *save_ptr = NULL; | |
699 | char *line; | |
700 | ||
701 | format_log_message(module, level, VLF_SYSLOG, msg_num, | |
702 | message, args, &s); | |
703 | for (line = strtok_r(s.string, "\n", &save_ptr); line; | |
704 | line = strtok_r(NULL, "\n", &save_ptr)) { | |
705 | syslog(syslog_level, "%s", line); | |
706 | } | |
707 | } | |
708 | ||
709 | if (log_to_file) { | |
710 | format_log_message(module, level, VLF_FILE, msg_num, | |
711 | message, args, &s); | |
712 | ds_put_char(&s, '\n'); | |
713 | fputs(ds_cstr(&s), log_file); | |
714 | fflush(log_file); | |
715 | } | |
716 | ||
717 | ds_destroy(&s); | |
718 | errno = save_errno; | |
719 | } | |
720 | } | |
721 | ||
722 | void | |
480ce8ab BP |
723 | vlog(const struct vlog_module *module, enum vlog_level level, |
724 | const char *message, ...) | |
064af421 BP |
725 | { |
726 | va_list args; | |
727 | ||
728 | va_start(args, message); | |
729 | vlog_valist(module, level, message, args); | |
730 | va_end(args); | |
731 | } | |
732 | ||
279c9e03 | 733 | void |
c1a543a8 | 734 | vlog_fatal_valist(const struct vlog_module *module_, |
279c9e03 BP |
735 | const char *message, va_list args) |
736 | { | |
737 | struct vlog_module *module = (struct vlog_module *) module_; | |
738 | ||
739 | /* Don't log this message to the console to avoid redundancy with the | |
740 | * message written by the later ovs_fatal_valist(). */ | |
c1a543a8 | 741 | module->levels[VLF_CONSOLE] = VLL_OFF; |
279c9e03 | 742 | |
c1a543a8 | 743 | vlog_valist(module, VLL_EMER, message, args); |
279c9e03 BP |
744 | ovs_fatal_valist(0, message, args); |
745 | } | |
746 | ||
747 | void | |
c1a543a8 | 748 | vlog_fatal(const struct vlog_module *module, const char *message, ...) |
279c9e03 BP |
749 | { |
750 | va_list args; | |
751 | ||
752 | va_start(args, message); | |
c1a543a8 | 753 | vlog_fatal_valist(module, message, args); |
279c9e03 BP |
754 | va_end(args); |
755 | } | |
756 | ||
064af421 | 757 | bool |
480ce8ab | 758 | vlog_should_drop(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
759 | struct vlog_rate_limit *rl) |
760 | { | |
761 | if (!vlog_is_enabled(module, level)) { | |
762 | return true; | |
763 | } | |
764 | ||
765 | if (rl->tokens < VLOG_MSG_TOKENS) { | |
766 | time_t now = time_now(); | |
767 | if (rl->last_fill > now) { | |
768 | /* Last filled in the future? Time must have gone backward, or | |
769 | * 'rl' has not been used before. */ | |
770 | rl->tokens = rl->burst; | |
771 | } else if (rl->last_fill < now) { | |
772 | unsigned int add = sat_mul(rl->rate, now - rl->last_fill); | |
773 | unsigned int tokens = sat_add(rl->tokens, add); | |
774 | rl->tokens = MIN(tokens, rl->burst); | |
775 | rl->last_fill = now; | |
776 | } | |
777 | if (rl->tokens < VLOG_MSG_TOKENS) { | |
778 | if (!rl->n_dropped) { | |
779 | rl->first_dropped = now; | |
780 | } | |
e2eed6a7 | 781 | rl->last_dropped = now; |
064af421 BP |
782 | rl->n_dropped++; |
783 | return true; | |
784 | } | |
785 | } | |
786 | rl->tokens -= VLOG_MSG_TOKENS; | |
787 | ||
788 | if (rl->n_dropped) { | |
e2eed6a7 BP |
789 | time_t now = time_now(); |
790 | unsigned int first_dropped_elapsed = now - rl->first_dropped; | |
791 | unsigned int last_dropped_elapsed = now - rl->last_dropped; | |
792 | ||
064af421 | 793 | vlog(module, level, |
e2eed6a7 BP |
794 | "Dropped %u log messages in last %u seconds (most recently, " |
795 | "%u seconds ago) due to excessive rate", | |
796 | rl->n_dropped, first_dropped_elapsed, last_dropped_elapsed); | |
797 | ||
064af421 BP |
798 | rl->n_dropped = 0; |
799 | } | |
800 | return false; | |
801 | } | |
802 | ||
803 | void | |
480ce8ab | 804 | vlog_rate_limit(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
805 | struct vlog_rate_limit *rl, const char *message, ...) |
806 | { | |
807 | if (!vlog_should_drop(module, level, rl)) { | |
808 | va_list args; | |
809 | ||
810 | va_start(args, message); | |
811 | vlog_valist(module, level, message, args); | |
812 | va_end(args); | |
813 | } | |
814 | } | |
815 | ||
816 | void | |
d295e8e9 | 817 | vlog_usage(void) |
064af421 BP |
818 | { |
819 | printf("\nLogging options:\n" | |
2a3e30b2 | 820 | " -v, --verbose=[SPEC] set logging levels\n" |
064af421 BP |
821 | " -v, --verbose set maximum verbosity level\n" |
822 | " --log-file[=FILE] enable logging to specified FILE\n" | |
823 | " (default: %s/%s.log)\n", | |
b43c6fe2 | 824 | ovs_logdir(), program_name); |
064af421 | 825 | } |