]> git.proxmox.com Git - ovs.git/blame - lib/vlog.c
datapath: compat: Fix compiler error.
[ovs.git] / lib / vlog.c
CommitLineData
064af421 1/*
4958e3ee 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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>
9eb94563 22#include <fcntl.h>
064af421
BP
23#include <stdarg.h>
24#include <stdlib.h>
25#include <string.h>
68cb8aaf 26#include <sys/stat.h>
064af421
BP
27#include <sys/types.h>
28#include <syslog.h>
29#include <time.h>
30#include <unistd.h>
a5fb0e29 31#include "async-append.h"
4958e3ee 32#include "coverage.h"
064af421
BP
33#include "dirs.h"
34#include "dynamic-string.h"
cbb13e8e 35#include "ofpbuf.h"
81d6495f 36#include "ovs-thread.h"
064af421 37#include "sat-math.h"
8628b0b7 38#include "svec.h"
064af421
BP
39#include "timeval.h"
40#include "unixctl.h"
41#include "util.h"
42
d98e6007 43VLOG_DEFINE_THIS_MODULE(vlog);
064af421 44
4958e3ee
BP
45/* ovs_assert() logs the assertion message, so using ovs_assert() in this
46 * source file could cause recursion. */
47#undef ovs_assert
48#define ovs_assert use_assert_instead_of_ovs_assert_in_this_module
49
064af421 50/* Name for each logging level. */
4b5f1d2c 51static const char *const level_names[VLL_N_LEVELS] = {
064af421
BP
52#define VLOG_LEVEL(NAME, SYSLOG_LEVEL) #NAME,
53 VLOG_LEVELS
54#undef VLOG_LEVEL
55};
56
57/* Syslog value for each logging level. */
4b5f1d2c 58static const int syslog_levels[VLL_N_LEVELS] = {
064af421
BP
59#define VLOG_LEVEL(NAME, SYSLOG_LEVEL) SYSLOG_LEVEL,
60 VLOG_LEVELS
61#undef VLOG_LEVEL
62};
63
480ce8ab
BP
64/* The log modules. */
65#if USE_LINKER_SECTIONS
66extern struct vlog_module *__start_vlog_modules[];
67extern struct vlog_module *__stop_vlog_modules[];
68#define vlog_modules __start_vlog_modules
69#define n_vlog_modules (__stop_vlog_modules - __start_vlog_modules)
70#else
71#define VLOG_MODULE VLOG_DEFINE_MODULE__
72#include "vlog-modules.def"
73#undef VLOG_MODULE
74
3f659b53 75extern struct vlog_module *vlog_modules[];
480ce8ab
BP
76struct vlog_module *vlog_modules[] = {
77#define VLOG_MODULE(NAME) &VLM_##NAME,
064af421
BP
78#include "vlog-modules.def"
79#undef VLOG_MODULE
80};
480ce8ab
BP
81#define n_vlog_modules ARRAY_SIZE(vlog_modules)
82#endif
064af421 83
97be1538
EJ
84/* Protects the 'pattern' in all "struct facility"s, so that a race between
85 * changing and reading the pattern does not cause an access to freed
86 * memory. */
87static struct ovs_rwlock pattern_rwlock = OVS_RWLOCK_INITIALIZER;
88
064af421
BP
89/* Information about each facility. */
90struct facility {
91 const char *name; /* Name. */
97be1538 92 char *pattern OVS_GUARDED_BY(pattern_rwlock); /* Current pattern. */
064af421
BP
93 bool default_pattern; /* Whether current pattern is the default. */
94};
95static struct facility facilities[VLF_N_FACILITIES] = {
96#define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
97 VLOG_FACILITIES
98#undef VLOG_FACILITY
99};
100
81d6495f 101/* Sequence number for the message currently being composed. */
2ba4f163 102DEFINE_STATIC_PER_THREAD_DATA(unsigned int, msg_num, 0);
81d6495f
BP
103
104/* VLF_FILE configuration.
105 *
106 * All of the following is protected by 'log_file_mutex', which nests inside
107 * pattern_rwlock. */
834d6caf 108static struct ovs_mutex log_file_mutex = OVS_MUTEX_INITIALIZER;
97be1538
EJ
109static char *log_file_name OVS_GUARDED_BY(log_file_mutex);
110static int log_fd OVS_GUARDED_BY(log_file_mutex) = -1;
111static struct async_append *log_writer OVS_GUARDED_BY(log_file_mutex);
888e0cf4 112static bool log_async OVS_GUARDED_BY(log_file_mutex);
064af421 113
480ce8ab 114static void format_log_message(const struct vlog_module *, enum vlog_level,
81d6495f 115 enum vlog_facility,
064af421 116 const char *message, va_list, struct ds *)
97be1538 117 PRINTF_FORMAT(4, 0) OVS_REQ_RDLOCK(&pattern_rwlock);
064af421
BP
118
119/* Searches the 'n_names' in 'names'. Returns the index of a match for
120 * 'target', or 'n_names' if no name matches. */
121static size_t
4b5f1d2c 122search_name_array(const char *target, const char *const *names, size_t n_names)
064af421
BP
123{
124 size_t i;
125
126 for (i = 0; i < n_names; i++) {
127 assert(names[i]);
128 if (!strcasecmp(names[i], target)) {
129 break;
130 }
131 }
132 return i;
133}
134
135/* Returns the name for logging level 'level'. */
136const char *
137vlog_get_level_name(enum vlog_level level)
138{
139 assert(level < VLL_N_LEVELS);
140 return level_names[level];
141}
142
143/* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
144 * is not the name of a logging level. */
145enum vlog_level
d295e8e9 146vlog_get_level_val(const char *name)
064af421
BP
147{
148 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
149}
150
151/* Returns the name for logging facility 'facility'. */
152const char *
d295e8e9 153vlog_get_facility_name(enum vlog_facility facility)
064af421
BP
154{
155 assert(facility < VLF_N_FACILITIES);
156 return facilities[facility].name;
157}
158
159/* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
160 * not the name of a logging facility. */
161enum vlog_facility
d295e8e9 162vlog_get_facility_val(const char *name)
064af421
BP
163{
164 size_t i;
165
166 for (i = 0; i < VLF_N_FACILITIES; i++) {
167 if (!strcasecmp(facilities[i].name, name)) {
168 break;
169 }
170 }
171 return i;
172}
173
174/* Returns the name for logging module 'module'. */
480ce8ab
BP
175const char *
176vlog_get_module_name(const struct vlog_module *module)
064af421 177{
480ce8ab 178 return module->name;
064af421
BP
179}
180
480ce8ab
BP
181/* Returns the logging module named 'name', or NULL if 'name' is not the name
182 * of a logging module. */
183struct vlog_module *
184vlog_module_from_name(const char *name)
064af421 185{
480ce8ab
BP
186 struct vlog_module **mp;
187
188 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
189 if (!strcasecmp(name, (*mp)->name)) {
190 return *mp;
191 }
192 }
193 return NULL;
064af421
BP
194}
195
196/* Returns the current logging level for the given 'module' and 'facility'. */
197enum vlog_level
d295e8e9 198vlog_get_level(const struct vlog_module *module, enum vlog_facility facility)
064af421 199{
064af421 200 assert(facility < VLF_N_FACILITIES);
480ce8ab 201 return module->levels[facility];
064af421
BP
202}
203
97be1538
EJ
204static void
205update_min_level(struct vlog_module *module) OVS_REQUIRES(&log_file_mutex)
064af421 206{
064af421
BP
207 enum vlog_facility facility;
208
c1a543a8 209 module->min_level = VLL_OFF;
064af421 210 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
9eb94563 211 if (log_fd >= 0 || facility != VLF_FILE) {
480ce8ab 212 enum vlog_level level = module->levels[facility];
56cee53b 213 if (level > module->min_level) {
480ce8ab
BP
214 module->min_level = level;
215 }
064af421
BP
216 }
217 }
064af421
BP
218}
219
220static void
480ce8ab 221set_facility_level(enum vlog_facility facility, struct vlog_module *module,
064af421
BP
222 enum vlog_level level)
223{
224 assert(facility >= 0 && facility < VLF_N_FACILITIES);
225 assert(level < VLL_N_LEVELS);
226
97be1538 227 ovs_mutex_lock(&log_file_mutex);
480ce8ab
BP
228 if (!module) {
229 struct vlog_module **mp;
230
231 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
232 (*mp)->levels[facility] = level;
233 update_min_level(*mp);
064af421
BP
234 }
235 } else {
480ce8ab 236 module->levels[facility] = level;
064af421
BP
237 update_min_level(module);
238 }
97be1538 239 ovs_mutex_unlock(&log_file_mutex);
064af421
BP
240}
241
480ce8ab
BP
242/* Sets the logging level for the given 'module' and 'facility' to 'level'. A
243 * null 'module' or a 'facility' of VLF_ANY_FACILITY is treated as a wildcard
244 * across all modules or facilities, respectively. */
064af421 245void
480ce8ab 246vlog_set_levels(struct vlog_module *module, enum vlog_facility facility,
d295e8e9 247 enum vlog_level level)
064af421
BP
248{
249 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
250 if (facility == VLF_ANY_FACILITY) {
251 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
252 set_facility_level(facility, module, level);
253 }
254 } else {
255 set_facility_level(facility, module, level);
256 }
257}
258
259static void
d295e8e9 260do_set_pattern(enum vlog_facility facility, const char *pattern)
064af421
BP
261{
262 struct facility *f = &facilities[facility];
81d6495f 263
97be1538 264 ovs_rwlock_wrlock(&pattern_rwlock);
064af421
BP
265 if (!f->default_pattern) {
266 free(f->pattern);
267 } else {
268 f->default_pattern = false;
269 }
270 f->pattern = xstrdup(pattern);
97be1538 271 ovs_rwlock_unlock(&pattern_rwlock);
064af421
BP
272}
273
274/* Sets the pattern for the given 'facility' to 'pattern'. */
275void
276vlog_set_pattern(enum vlog_facility facility, const char *pattern)
277{
278 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
279 if (facility == VLF_ANY_FACILITY) {
280 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
281 do_set_pattern(facility, pattern);
282 }
283 } else {
284 do_set_pattern(facility, pattern);
285 }
286}
287
064af421
BP
288/* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
289 * default file name if 'file_name' is null. Returns 0 if successful,
290 * otherwise a positive errno value. */
291int
292vlog_set_log_file(const char *file_name)
293{
81d6495f 294 char *new_log_file_name;
480ce8ab 295 struct vlog_module **mp;
81d6495f
BP
296 struct stat old_stat;
297 struct stat new_stat;
298 int new_log_fd;
299 bool same_file;
97be1538 300 bool log_close;
81d6495f
BP
301
302 /* Open new log file. */
303 new_log_file_name = (file_name
304 ? xstrdup(file_name)
305 : xasprintf("%s/%s.log", ovs_logdir(), program_name));
306 new_log_fd = open(new_log_file_name, O_WRONLY | O_CREAT | O_APPEND, 0666);
307 if (new_log_fd < 0) {
308 VLOG_WARN("failed to open %s for logging: %s",
309 new_log_file_name, ovs_strerror(errno));
310 free(new_log_file_name);
311 return errno;
312 }
313
314 /* If the new log file is the same one we already have open, bail out. */
97be1538 315 ovs_mutex_lock(&log_file_mutex);
81d6495f
BP
316 same_file = (log_fd >= 0
317 && new_log_fd >= 0
318 && !fstat(log_fd, &old_stat)
319 && !fstat(new_log_fd, &new_stat)
320 && old_stat.st_dev == new_stat.st_dev
321 && old_stat.st_ino == new_stat.st_ino);
97be1538 322 ovs_mutex_unlock(&log_file_mutex);
81d6495f
BP
323 if (same_file) {
324 close(new_log_fd);
325 free(new_log_file_name);
326 return 0;
327 }
064af421 328
81d6495f 329 /* Log closing old log file (we can't log while holding log_file_mutex). */
97be1538
EJ
330 ovs_mutex_lock(&log_file_mutex);
331 log_close = log_fd >= 0;
332 ovs_mutex_unlock(&log_file_mutex);
333 if (log_close) {
064af421 334 VLOG_INFO("closing log file");
81d6495f 335 }
a5fb0e29 336
81d6495f 337 /* Close old log file, if any, and install new one. */
97be1538 338 ovs_mutex_lock(&log_file_mutex);
81d6495f
BP
339 if (log_fd >= 0) {
340 free(log_file_name);
9eb94563 341 close(log_fd);
81d6495f 342 async_append_destroy(log_writer);
064af421
BP
343 }
344
81d6495f
BP
345 log_file_name = xstrdup(new_log_file_name);
346 log_fd = new_log_fd;
888e0cf4
BP
347 if (log_async) {
348 log_writer = async_append_create(new_log_fd);
349 }
81d6495f 350
480ce8ab
BP
351 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
352 update_min_level(*mp);
064af421 353 }
97be1538 354 ovs_mutex_unlock(&log_file_mutex);
064af421 355
81d6495f
BP
356 /* Log opening new log file (we can't log while holding log_file_mutex). */
357 VLOG_INFO("opened log file %s", new_log_file_name);
358 free(new_log_file_name);
064af421 359
81d6495f 360 return 0;
064af421
BP
361}
362
363/* Closes and then attempts to re-open the current log file. (This is useful
364 * just after log rotation, to ensure that the new log file starts being used.)
365 * Returns 0 if successful, otherwise a positive errno value. */
366int
367vlog_reopen_log_file(void)
368{
81d6495f 369 char *fn;
68cb8aaf 370
97be1538 371 ovs_mutex_lock(&log_file_mutex);
81d6495f 372 fn = log_file_name ? xstrdup(log_file_name) : NULL;
97be1538 373 ovs_mutex_unlock(&log_file_mutex);
68cb8aaf 374
81d6495f
BP
375 if (fn) {
376 int error = vlog_set_log_file(fn);
377 free(fn);
378 return error;
379 } else {
68cb8aaf
BP
380 return 0;
381 }
064af421
BP
382}
383
2a3e30b2
BP
384/* Set debugging levels. Returns null if successful, otherwise an error
385 * message that the caller must free(). */
064af421
BP
386char *
387vlog_set_levels_from_string(const char *s_)
388{
064af421 389 char *s = xstrdup(s_);
2a3e30b2
BP
390 char *save_ptr = NULL;
391 char *msg = NULL;
392 char *word;
064af421 393
2a3e30b2
BP
394 word = strtok_r(s, " ,:\t", &save_ptr);
395 if (word && !strcasecmp(word, "PATTERN")) {
396 enum vlog_facility facility;
064af421 397
2a3e30b2
BP
398 word = strtok_r(NULL, " ,:\t", &save_ptr);
399 if (!word) {
400 msg = xstrdup("missing facility");
401 goto exit;
064af421
BP
402 }
403
2a3e30b2
BP
404 facility = (!strcasecmp(word, "ANY")
405 ? VLF_ANY_FACILITY
406 : vlog_get_facility_val(word));
407 if (facility == VLF_N_FACILITIES) {
408 msg = xasprintf("unknown facility \"%s\"", word);
409 goto exit;
410 }
411 vlog_set_pattern(facility, save_ptr);
412 } else {
413 struct vlog_module *module = NULL;
414 enum vlog_level level = VLL_N_LEVELS;
415 enum vlog_facility facility = VLF_N_FACILITIES;
416
417 for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) {
418 if (!strcasecmp(word, "ANY")) {
419 continue;
420 } else if (vlog_get_facility_val(word) != VLF_N_FACILITIES) {
421 if (facility != VLF_N_FACILITIES) {
422 msg = xstrdup("cannot specify multiple facilities");
423 goto exit;
064af421 424 }
2a3e30b2
BP
425 facility = vlog_get_facility_val(word);
426 } else if (vlog_get_level_val(word) != VLL_N_LEVELS) {
427 if (level != VLL_N_LEVELS) {
428 msg = xstrdup("cannot specify multiple levels");
429 goto exit;
430 }
431 level = vlog_get_level_val(word);
432 } else if (vlog_module_from_name(word)) {
433 if (module) {
434 msg = xstrdup("cannot specify multiple modules");
435 goto exit;
436 }
437 module = vlog_module_from_name(word);
438 } else {
439 msg = xasprintf("no facility, level, or module \"%s\"", word);
440 goto exit;
064af421 441 }
2a3e30b2 442 }
064af421 443
2a3e30b2
BP
444 if (facility == VLF_N_FACILITIES) {
445 facility = VLF_ANY_FACILITY;
446 }
447 if (level == VLL_N_LEVELS) {
448 level = VLL_DBG;
064af421 449 }
2a3e30b2 450 vlog_set_levels(module, facility, level);
064af421 451 }
2a3e30b2
BP
452
453exit:
064af421 454 free(s);
2a3e30b2 455 return msg;
064af421
BP
456}
457
316bd0f8
BP
458/* Set debugging levels. Abort with an error message if 's' is invalid. */
459void
460vlog_set_levels_from_string_assert(const char *s)
461{
462 char *error = vlog_set_levels_from_string(s);
463 if (error) {
464 ovs_fatal(0, "%s", error);
465 }
466}
467
064af421
BP
468/* If 'arg' is null, configure maximum verbosity. Otherwise, sets
469 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
470void
471vlog_set_verbosity(const char *arg)
472{
473 if (arg) {
474 char *msg = vlog_set_levels_from_string(arg);
475 if (msg) {
476 ovs_fatal(0, "processing \"%s\": %s", arg, msg);
477 }
478 } else {
480ce8ab 479 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_DBG);
064af421
BP
480 }
481}
482
483static void
0e15264f
BP
484vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[],
485 void *aux OVS_UNUSED)
064af421 486{
0e15264f
BP
487 int i;
488
489 for (i = 1; i < argc; i++) {
490 char *msg = vlog_set_levels_from_string(argv[i]);
491 if (msg) {
bde9f75d 492 unixctl_command_reply_error(conn, msg);
0e15264f
BP
493 free(msg);
494 return;
495 }
496 }
bde9f75d 497 unixctl_command_reply(conn, NULL);
064af421
BP
498}
499
500static void
0e15264f
BP
501vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
502 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
064af421
BP
503{
504 char *msg = vlog_get_levels();
bde9f75d 505 unixctl_command_reply(conn, msg);
064af421
BP
506 free(msg);
507}
508
509static void
0e15264f
BP
510vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED,
511 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
064af421 512{
97be1538
EJ
513 bool has_log_file;
514
515 ovs_mutex_lock(&log_file_mutex);
516 has_log_file = log_file_name != NULL;
517 ovs_mutex_unlock(&log_file_mutex);
518
519 if (has_log_file) {
064af421
BP
520 int error = vlog_reopen_log_file();
521 if (error) {
10a89ef0 522 unixctl_command_reply_error(conn, ovs_strerror(errno));
064af421 523 } else {
bde9f75d 524 unixctl_command_reply(conn, NULL);
064af421
BP
525 }
526 } else {
bde9f75d 527 unixctl_command_reply_error(conn, "Logging to file not configured");
064af421
BP
528 }
529}
530
4958e3ee
BP
531static void
532set_all_rate_limits(bool enable)
533{
534 struct vlog_module **mp;
535
536 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
537 (*mp)->honor_rate_limits = enable;
538 }
539}
540
541static void
542set_rate_limits(struct unixctl_conn *conn, int argc,
543 const char *argv[], bool enable)
544{
545 if (argc > 1) {
546 int i;
547
548 for (i = 1; i < argc; i++) {
549 if (!strcasecmp(argv[i], "ANY")) {
550 set_all_rate_limits(enable);
551 } else {
552 struct vlog_module *module = vlog_module_from_name(argv[i]);
553 if (!module) {
554 unixctl_command_reply_error(conn, "unknown module");
555 return;
556 }
557 module->honor_rate_limits = enable;
558 }
559 }
560 } else {
561 set_all_rate_limits(enable);
562 }
563 unixctl_command_reply(conn, NULL);
564}
565
566static void
567vlog_enable_rate_limit(struct unixctl_conn *conn, int argc,
568 const char *argv[], void *aux OVS_UNUSED)
569{
570 set_rate_limits(conn, argc, argv, true);
571}
572
573static void
574vlog_disable_rate_limit(struct unixctl_conn *conn, int argc,
575 const char *argv[], void *aux OVS_UNUSED)
576{
577 set_rate_limits(conn, argc, argv, false);
578}
579
81d6495f
BP
580static void
581vlog_init__(void)
064af421 582{
487657b2 583 static char *program_name_copy;
2b31d8e7 584 long long int now;
064af421 585
487657b2
BP
586 /* openlog() is allowed to keep the pointer passed in, without making a
587 * copy. The daemonize code sometimes frees and replaces 'program_name',
588 * so make a private copy just for openlog(). (We keep a pointer to the
589 * private copy to suppress memory leak warnings in case openlog() does
590 * make its own copy.) */
591 program_name_copy = program_name ? xstrdup(program_name) : NULL;
592 openlog(program_name_copy, LOG_NDELAY, LOG_DAEMON);
064af421 593
2b31d8e7 594 now = time_wall_msec();
064af421 595 if (now < 0) {
2b31d8e7
PI
596 char *s = xastrftime_msec("%a, %d %b %Y %H:%M:%S", now, true);
597 VLOG_ERR("current time is negative: %s (%lld)", s, now);
3e78870d 598 free(s);
064af421
BP
599 }
600
0e15264f 601 unixctl_command_register(
2a3e30b2 602 "vlog/set", "{spec | PATTERN:facility:pattern}",
0e15264f
BP
603 1, INT_MAX, vlog_unixctl_set, NULL);
604 unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list, NULL);
4958e3ee
BP
605 unixctl_command_register("vlog/enable-rate-limit", "[module]...",
606 0, INT_MAX, vlog_enable_rate_limit, NULL);
607 unixctl_command_register("vlog/disable-rate-limit", "[module]...",
608 0, INT_MAX, vlog_disable_rate_limit, NULL);
0e15264f
BP
609 unixctl_command_register("vlog/reopen", "", 0, 0,
610 vlog_unixctl_reopen, NULL);
064af421
BP
611}
612
81d6495f
BP
613/* Initializes the logging subsystem and registers its unixctl server
614 * commands. */
615void
616vlog_init(void)
617{
618 static pthread_once_t once = PTHREAD_ONCE_INIT;
619 pthread_once(&once, vlog_init__);
620}
621
888e0cf4
BP
622/* Enables VLF_FILE log output to be written asynchronously to disk.
623 * Asynchronous file writes avoid blocking the process in the case of a busy
624 * disk, but on the other hand they are less robust: there is a chance that the
625 * write will not make it to the log file if the process crashes soon after the
626 * log call. */
627void
628vlog_enable_async(void)
629{
630 ovs_mutex_lock(&log_file_mutex);
631 log_async = true;
632 if (log_fd >= 0 && !log_writer) {
633 log_writer = async_append_create(log_fd);
634 }
635 ovs_mutex_unlock(&log_file_mutex);
636}
637
064af421
BP
638/* Print the current logging level for each module. */
639char *
640vlog_get_levels(void)
641{
642 struct ds s = DS_EMPTY_INITIALIZER;
480ce8ab 643 struct vlog_module **mp;
8628b0b7
JP
644 struct svec lines = SVEC_EMPTY_INITIALIZER;
645 char *line;
646 size_t i;
064af421
BP
647
648 ds_put_format(&s, " console syslog file\n");
649 ds_put_format(&s, " ------- ------ ------\n");
650
480ce8ab 651 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
4958e3ee
BP
652 struct ds line;
653
654 ds_init(&line);
655 ds_put_format(&line, "%-16s %4s %4s %4s",
656 vlog_get_module_name(*mp),
657 vlog_get_level_name(vlog_get_level(*mp, VLF_CONSOLE)),
658 vlog_get_level_name(vlog_get_level(*mp, VLF_SYSLOG)),
659 vlog_get_level_name(vlog_get_level(*mp, VLF_FILE)));
660 if (!(*mp)->honor_rate_limits) {
661 ds_put_cstr(&line, " (rate limiting disabled)");
662 }
663 ds_put_char(&line, '\n');
664
665 svec_add_nocopy(&lines, ds_steal_cstr(&line));
8628b0b7
JP
666 }
667
668 svec_sort(&lines);
669 SVEC_FOR_EACH (i, line, &lines) {
670 ds_put_cstr(&s, line);
064af421 671 }
8628b0b7 672 svec_destroy(&lines);
064af421
BP
673
674 return ds_cstr(&s);
675}
676
677/* Returns true if a log message emitted for the given 'module' and 'level'
678 * would cause some log output, false if that module and level are completely
679 * disabled. */
680bool
480ce8ab 681vlog_is_enabled(const struct vlog_module *module, enum vlog_level level)
064af421 682{
480ce8ab 683 return module->min_level >= level;
064af421
BP
684}
685
686static const char *
687fetch_braces(const char *p, const char *def, char *out, size_t out_size)
688{
689 if (*p == '{') {
690 size_t n = strcspn(p + 1, "}");
691 size_t n_copy = MIN(n, out_size - 1);
692 memcpy(out, p + 1, n_copy);
693 out[n_copy] = '\0';
694 p += n + 2;
695 } else {
696 ovs_strlcpy(out, def, out_size);
697 }
698 return p;
699}
700
701static void
480ce8ab 702format_log_message(const struct vlog_module *module, enum vlog_level level,
81d6495f 703 enum vlog_facility facility,
064af421
BP
704 const char *message, va_list args_, struct ds *s)
705{
706 char tmp[128];
707 va_list args;
708 const char *p;
709
710 ds_clear(s);
711 for (p = facilities[facility].pattern; *p != '\0'; ) {
bc9fb3a9 712 const char *subprogram_name;
064af421
BP
713 enum { LEFT, RIGHT } justify = RIGHT;
714 int pad = '0';
715 size_t length, field, used;
716
717 if (*p != '%') {
718 ds_put_char(s, *p++);
719 continue;
720 }
721
722 p++;
723 if (*p == '-') {
724 justify = LEFT;
725 p++;
726 }
727 if (*p == '0') {
728 pad = '0';
729 p++;
730 }
731 field = 0;
be2c418b 732 while (isdigit((unsigned char)*p)) {
064af421
BP
733 field = (field * 10) + (*p - '0');
734 p++;
735 }
736
737 length = s->length;
738 switch (*p++) {
739 case 'A':
740 ds_put_cstr(s, program_name);
741 break;
742 case 'c':
743 p = fetch_braces(p, "", tmp, sizeof tmp);
744 ds_put_cstr(s, vlog_get_module_name(module));
745 break;
746 case 'd':
2b31d8e7
PI
747 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
748 ds_put_strftime_msec(s, tmp, time_wall_msec(), false);
b5d29991
GS
749 break;
750 case 'D':
2b31d8e7
PI
751 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
752 ds_put_strftime_msec(s, tmp, time_wall_msec(), true);
064af421
BP
753 break;
754 case 'm':
755 /* Format user-supplied log message and trim trailing new-lines. */
756 length = s->length;
757 va_copy(args, args_);
758 ds_put_format_valist(s, message, args);
759 va_end(args);
760 while (s->length > length && s->string[s->length - 1] == '\n') {
761 s->length--;
762 }
763 break;
764 case 'N':
81d6495f 765 ds_put_format(s, "%u", *msg_num_get_unsafe());
064af421
BP
766 break;
767 case 'n':
768 ds_put_char(s, '\n');
769 break;
770 case 'p':
771 ds_put_cstr(s, vlog_get_level_name(level));
772 break;
773 case 'P':
774 ds_put_format(s, "%ld", (long int) getpid());
775 break;
776 case 'r':
4ae90ff9 777 ds_put_format(s, "%lld", time_msec() - time_boot_msec());
064af421 778 break;
781dee08 779 case 't':
bc9fb3a9 780 subprogram_name = get_subprogram_name();
781dee08
BP
781 ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main");
782 break;
783 case 'T':
bc9fb3a9 784 subprogram_name = get_subprogram_name();
781dee08
BP
785 if (subprogram_name[0]) {
786 ds_put_format(s, "(%s)", subprogram_name);
787 }
788 break;
064af421
BP
789 default:
790 ds_put_char(s, p[-1]);
791 break;
792 }
793 used = s->length - length;
794 if (used < field) {
795 size_t n_pad = field - used;
796 if (justify == RIGHT) {
797 ds_put_uninit(s, n_pad);
798 memmove(&s->string[length + n_pad], &s->string[length], used);
799 memset(&s->string[length], pad, n_pad);
800 } else {
801 ds_put_char_multiple(s, pad, n_pad);
802 }
803 }
804 }
805}
806
807/* Writes 'message' to the log at the given 'level' and as coming from the
808 * given 'module'.
809 *
810 * Guaranteed to preserve errno. */
811void
480ce8ab 812vlog_valist(const struct vlog_module *module, enum vlog_level level,
064af421
BP
813 const char *message, va_list args)
814{
480ce8ab
BP
815 bool log_to_console = module->levels[VLF_CONSOLE] >= level;
816 bool log_to_syslog = module->levels[VLF_SYSLOG] >= level;
97be1538
EJ
817 bool log_to_file;
818
819 ovs_mutex_lock(&log_file_mutex);
820 log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0;
821 ovs_mutex_unlock(&log_file_mutex);
064af421
BP
822 if (log_to_console || log_to_syslog || log_to_file) {
823 int save_errno = errno;
064af421
BP
824 struct ds s;
825
1e8cf0f7
BP
826 vlog_init();
827
064af421
BP
828 ds_init(&s);
829 ds_reserve(&s, 1024);
81d6495f 830 ++*msg_num_get();
064af421 831
97be1538 832 ovs_rwlock_rdlock(&pattern_rwlock);
064af421 833 if (log_to_console) {
81d6495f 834 format_log_message(module, level, VLF_CONSOLE, message, args, &s);
064af421
BP
835 ds_put_char(&s, '\n');
836 fputs(ds_cstr(&s), stderr);
837 }
838
839 if (log_to_syslog) {
840 int syslog_level = syslog_levels[level];
841 char *save_ptr = NULL;
842 char *line;
843
81d6495f 844 format_log_message(module, level, VLF_SYSLOG, message, args, &s);
064af421
BP
845 for (line = strtok_r(s.string, "\n", &save_ptr); line;
846 line = strtok_r(NULL, "\n", &save_ptr)) {
847 syslog(syslog_level, "%s", line);
848 }
849 }
850
851 if (log_to_file) {
81d6495f 852 format_log_message(module, level, VLF_FILE, message, args, &s);
064af421 853 ds_put_char(&s, '\n');
81d6495f 854
97be1538 855 ovs_mutex_lock(&log_file_mutex);
81d6495f 856 if (log_fd >= 0) {
888e0cf4
BP
857 if (log_writer) {
858 async_append_write(log_writer, s.string, s.length);
859 if (level == VLL_EMER) {
860 async_append_flush(log_writer);
861 }
862 } else {
863 ignore(write(log_fd, s.string, s.length));
81d6495f 864 }
a5fb0e29 865 }
97be1538 866 ovs_mutex_unlock(&log_file_mutex);
064af421 867 }
97be1538 868 ovs_rwlock_unlock(&pattern_rwlock);
064af421
BP
869
870 ds_destroy(&s);
871 errno = save_errno;
872 }
873}
874
875void
480ce8ab
BP
876vlog(const struct vlog_module *module, enum vlog_level level,
877 const char *message, ...)
064af421
BP
878{
879 va_list args;
880
881 va_start(args, message);
882 vlog_valist(module, level, message, args);
883 va_end(args);
884}
885
d41d4b71
BP
886/* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
887 * exit code. Always writes the message to stderr, even if the console
888 * facility is disabled.
889 *
890 * Choose this function instead of vlog_abort_valist() if the daemon monitoring
891 * facility shouldn't automatically restart the current daemon. */
279c9e03 892void
c1a543a8 893vlog_fatal_valist(const struct vlog_module *module_,
279c9e03
BP
894 const char *message, va_list args)
895{
ebc56baa 896 struct vlog_module *module = CONST_CAST(struct vlog_module *, module_);
279c9e03
BP
897
898 /* Don't log this message to the console to avoid redundancy with the
899 * message written by the later ovs_fatal_valist(). */
c1a543a8 900 module->levels[VLF_CONSOLE] = VLL_OFF;
279c9e03 901
c1a543a8 902 vlog_valist(module, VLL_EMER, message, args);
279c9e03
BP
903 ovs_fatal_valist(0, message, args);
904}
905
d41d4b71
BP
906/* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
907 * exit code. Always writes the message to stderr, even if the console
908 * facility is disabled.
909 *
910 * Choose this function instead of vlog_abort() if the daemon monitoring
911 * facility shouldn't automatically restart the current daemon. */
279c9e03 912void
c1a543a8 913vlog_fatal(const struct vlog_module *module, const char *message, ...)
279c9e03
BP
914{
915 va_list args;
916
917 va_start(args, message);
c1a543a8 918 vlog_fatal_valist(module, message, args);
279c9e03
BP
919 va_end(args);
920}
921
d41d4b71
BP
922/* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
923 * writes the message to stderr, even if the console facility is disabled.
924 *
925 * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
926 * facility should automatically restart the current daemon. */
927void
928vlog_abort_valist(const struct vlog_module *module_,
929 const char *message, va_list args)
930{
931 struct vlog_module *module = (struct vlog_module *) module_;
932
933 /* Don't log this message to the console to avoid redundancy with the
934 * message written by the later ovs_abort_valist(). */
935 module->levels[VLF_CONSOLE] = VLL_OFF;
936
937 vlog_valist(module, VLL_EMER, message, args);
938 ovs_abort_valist(0, message, args);
939}
940
941/* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
942 * writes the message to stderr, even if the console facility is disabled.
943 *
944 * Choose this function instead of vlog_fatal() if the daemon monitoring
945 * facility should automatically restart the current daemon. */
946void
947vlog_abort(const struct vlog_module *module, const char *message, ...)
948{
949 va_list args;
950
951 va_start(args, message);
952 vlog_abort_valist(module, message, args);
953 va_end(args);
954}
955
064af421 956bool
480ce8ab 957vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
064af421
BP
958 struct vlog_rate_limit *rl)
959{
4958e3ee
BP
960 if (!module->honor_rate_limits) {
961 return false;
962 }
963
064af421
BP
964 if (!vlog_is_enabled(module, level)) {
965 return true;
966 }
967
97be1538 968 ovs_mutex_lock(&rl->mutex);
648f4f1f 969 if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) {
064af421 970 time_t now = time_now();
648f4f1f
BP
971 if (!rl->n_dropped) {
972 rl->first_dropped = now;
064af421 973 }
648f4f1f
BP
974 rl->last_dropped = now;
975 rl->n_dropped++;
97be1538 976 ovs_mutex_unlock(&rl->mutex);
648f4f1f 977 return true;
064af421 978 }
064af421 979
fda28014 980 if (!rl->n_dropped) {
97be1538 981 ovs_mutex_unlock(&rl->mutex);
fda28014 982 } else {
e2eed6a7 983 time_t now = time_now();
fda28014 984 unsigned int n_dropped = rl->n_dropped;
e2eed6a7
BP
985 unsigned int first_dropped_elapsed = now - rl->first_dropped;
986 unsigned int last_dropped_elapsed = now - rl->last_dropped;
fda28014 987 rl->n_dropped = 0;
97be1538 988 ovs_mutex_unlock(&rl->mutex);
e2eed6a7 989
064af421 990 vlog(module, level,
e2eed6a7
BP
991 "Dropped %u log messages in last %u seconds (most recently, "
992 "%u seconds ago) due to excessive rate",
fda28014 993 n_dropped, first_dropped_elapsed, last_dropped_elapsed);
064af421 994 }
fda28014 995
064af421
BP
996 return false;
997}
998
999void
480ce8ab 1000vlog_rate_limit(const struct vlog_module *module, enum vlog_level level,
064af421
BP
1001 struct vlog_rate_limit *rl, const char *message, ...)
1002{
1003 if (!vlog_should_drop(module, level, rl)) {
1004 va_list args;
1005
1006 va_start(args, message);
1007 vlog_valist(module, level, message, args);
1008 va_end(args);
1009 }
1010}
1011
1012void
d295e8e9 1013vlog_usage(void)
064af421
BP
1014{
1015 printf("\nLogging options:\n"
2a3e30b2 1016 " -v, --verbose=[SPEC] set logging levels\n"
064af421
BP
1017 " -v, --verbose set maximum verbosity level\n"
1018 " --log-file[=FILE] enable logging to specified FILE\n"
1019 " (default: %s/%s.log)\n",
b43c6fe2 1020 ovs_logdir(), program_name);
064af421 1021}