2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
31 #include "async-append.h"
34 #include "dynamic-string.h"
36 #include "ovs-thread.h"
43 VLOG_DEFINE_THIS_MODULE(vlog);
45 COVERAGE_DEFINE(vlog_recursive);
47 /* ovs_assert() logs the assertion message, so using ovs_assert() in this
48 * source file could cause recursion. */
50 #define ovs_assert use_assert_instead_of_ovs_assert_in_this_module
52 /* Name for each logging level. */
53 static const char *const level_names[VLL_N_LEVELS] = {
54 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) #NAME,
59 /* Syslog value for each logging level. */
60 static const int syslog_levels[VLL_N_LEVELS] = {
61 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) SYSLOG_LEVEL,
66 /* The log modules. */
67 #if USE_LINKER_SECTIONS
68 extern struct vlog_module *__start_vlog_modules[];
69 extern struct vlog_module *__stop_vlog_modules[];
70 #define vlog_modules __start_vlog_modules
71 #define n_vlog_modules (__stop_vlog_modules - __start_vlog_modules)
73 #define VLOG_MODULE VLOG_DEFINE_MODULE__
74 #include "vlog-modules.def"
77 struct vlog_module *vlog_modules[] = {
78 #define VLOG_MODULE(NAME) &VLM_##NAME,
79 #include "vlog-modules.def"
82 #define n_vlog_modules ARRAY_SIZE(vlog_modules)
85 /* Protects the 'pattern' in all "struct facility"s, so that a race between
86 * changing and reading the pattern does not cause an access to freed
88 static struct ovs_rwlock pattern_rwlock = OVS_RWLOCK_INITIALIZER;
90 /* Information about each facility. */
92 const char *name; /* Name. */
93 char *pattern OVS_GUARDED_BY(pattern_rwlock); /* Current pattern. */
94 bool default_pattern; /* Whether current pattern is the default. */
96 static struct facility facilities[VLF_N_FACILITIES] = {
97 #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
102 /* Sequence number for the message currently being composed. */
103 DEFINE_PER_THREAD_DATA(unsigned int, msg_num, 0);
105 /* VLF_FILE configuration.
107 * All of the following is protected by 'log_file_mutex', which nests inside
109 static struct ovs_mutex log_file_mutex = OVS_ADAPTIVE_MUTEX_INITIALIZER;
110 static char *log_file_name OVS_GUARDED_BY(log_file_mutex);
111 static int log_fd OVS_GUARDED_BY(log_file_mutex) = -1;
112 static struct async_append *log_writer OVS_GUARDED_BY(log_file_mutex);
113 static bool log_async OVS_GUARDED_BY(log_file_mutex);
115 static void format_log_message(const struct vlog_module *, enum vlog_level,
117 const char *message, va_list, struct ds *)
118 PRINTF_FORMAT(4, 0) OVS_REQ_RDLOCK(&pattern_rwlock);
120 /* Searches the 'n_names' in 'names'. Returns the index of a match for
121 * 'target', or 'n_names' if no name matches. */
123 search_name_array(const char *target, const char *const *names, size_t n_names)
127 for (i = 0; i < n_names; i++) {
129 if (!strcasecmp(names[i], target)) {
136 /* Returns the name for logging level 'level'. */
138 vlog_get_level_name(enum vlog_level level)
140 assert(level < VLL_N_LEVELS);
141 return level_names[level];
144 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
145 * is not the name of a logging level. */
147 vlog_get_level_val(const char *name)
149 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
152 /* Returns the name for logging facility 'facility'. */
154 vlog_get_facility_name(enum vlog_facility facility)
156 assert(facility < VLF_N_FACILITIES);
157 return facilities[facility].name;
160 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
161 * not the name of a logging facility. */
163 vlog_get_facility_val(const char *name)
167 for (i = 0; i < VLF_N_FACILITIES; i++) {
168 if (!strcasecmp(facilities[i].name, name)) {
175 /* Returns the name for logging module 'module'. */
177 vlog_get_module_name(const struct vlog_module *module)
182 /* Returns the logging module named 'name', or NULL if 'name' is not the name
183 * of a logging module. */
185 vlog_module_from_name(const char *name)
187 struct vlog_module **mp;
189 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
190 if (!strcasecmp(name, (*mp)->name)) {
197 /* Returns the current logging level for the given 'module' and 'facility'. */
199 vlog_get_level(const struct vlog_module *module, enum vlog_facility facility)
201 assert(facility < VLF_N_FACILITIES);
202 return module->levels[facility];
206 update_min_level(struct vlog_module *module) OVS_REQUIRES(&log_file_mutex)
208 enum vlog_facility facility;
210 module->min_level = VLL_OFF;
211 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
212 if (log_fd >= 0 || facility != VLF_FILE) {
213 enum vlog_level level = module->levels[facility];
214 if (level > module->min_level) {
215 module->min_level = level;
222 set_facility_level(enum vlog_facility facility, struct vlog_module *module,
223 enum vlog_level level)
225 assert(facility >= 0 && facility < VLF_N_FACILITIES);
226 assert(level < VLL_N_LEVELS);
228 ovs_mutex_lock(&log_file_mutex);
230 struct vlog_module **mp;
232 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
233 (*mp)->levels[facility] = level;
234 update_min_level(*mp);
237 module->levels[facility] = level;
238 update_min_level(module);
240 ovs_mutex_unlock(&log_file_mutex);
243 /* Sets the logging level for the given 'module' and 'facility' to 'level'. A
244 * null 'module' or a 'facility' of VLF_ANY_FACILITY is treated as a wildcard
245 * across all modules or facilities, respectively. */
247 vlog_set_levels(struct vlog_module *module, enum vlog_facility facility,
248 enum vlog_level level)
250 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
251 if (facility == VLF_ANY_FACILITY) {
252 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
253 set_facility_level(facility, module, level);
256 set_facility_level(facility, module, level);
261 do_set_pattern(enum vlog_facility facility, const char *pattern)
263 struct facility *f = &facilities[facility];
265 ovs_rwlock_wrlock(&pattern_rwlock);
266 if (!f->default_pattern) {
269 f->default_pattern = false;
271 f->pattern = xstrdup(pattern);
272 ovs_rwlock_unlock(&pattern_rwlock);
275 /* Sets the pattern for the given 'facility' to 'pattern'. */
277 vlog_set_pattern(enum vlog_facility facility, const char *pattern)
279 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
280 if (facility == VLF_ANY_FACILITY) {
281 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
282 do_set_pattern(facility, pattern);
285 do_set_pattern(facility, pattern);
289 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
290 * default file name if 'file_name' is null. Returns 0 if successful,
291 * otherwise a positive errno value. */
293 vlog_set_log_file(const char *file_name)
295 char *new_log_file_name;
296 struct vlog_module **mp;
297 struct stat old_stat;
298 struct stat new_stat;
303 /* Open new log file. */
304 new_log_file_name = (file_name
306 : xasprintf("%s/%s.log", ovs_logdir(), program_name));
307 new_log_fd = open(new_log_file_name, O_WRONLY | O_CREAT | O_APPEND, 0666);
308 if (new_log_fd < 0) {
309 VLOG_WARN("failed to open %s for logging: %s",
310 new_log_file_name, ovs_strerror(errno));
311 free(new_log_file_name);
315 /* If the new log file is the same one we already have open, bail out. */
316 ovs_mutex_lock(&log_file_mutex);
317 same_file = (log_fd >= 0
319 && !fstat(log_fd, &old_stat)
320 && !fstat(new_log_fd, &new_stat)
321 && old_stat.st_dev == new_stat.st_dev
322 && old_stat.st_ino == new_stat.st_ino);
323 ovs_mutex_unlock(&log_file_mutex);
326 free(new_log_file_name);
330 /* Log closing old log file (we can't log while holding log_file_mutex). */
331 ovs_mutex_lock(&log_file_mutex);
332 log_close = log_fd >= 0;
333 ovs_mutex_unlock(&log_file_mutex);
335 VLOG_INFO("closing log file");
338 /* Close old log file, if any, and install new one. */
339 ovs_mutex_lock(&log_file_mutex);
343 async_append_destroy(log_writer);
346 log_file_name = xstrdup(new_log_file_name);
349 log_writer = async_append_create(new_log_fd);
352 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
353 update_min_level(*mp);
355 ovs_mutex_unlock(&log_file_mutex);
357 /* Log opening new log file (we can't log while holding log_file_mutex). */
358 VLOG_INFO("opened log file %s", new_log_file_name);
359 free(new_log_file_name);
364 /* Closes and then attempts to re-open the current log file. (This is useful
365 * just after log rotation, to ensure that the new log file starts being used.)
366 * Returns 0 if successful, otherwise a positive errno value. */
368 vlog_reopen_log_file(void)
372 ovs_mutex_lock(&log_file_mutex);
373 fn = log_file_name ? xstrdup(log_file_name) : NULL;
374 ovs_mutex_unlock(&log_file_mutex);
377 int error = vlog_set_log_file(fn);
385 /* Set debugging levels. Returns null if successful, otherwise an error
386 * message that the caller must free(). */
388 vlog_set_levels_from_string(const char *s_)
390 char *s = xstrdup(s_);
391 char *save_ptr = NULL;
395 word = strtok_r(s, " ,:\t", &save_ptr);
396 if (word && !strcasecmp(word, "PATTERN")) {
397 enum vlog_facility facility;
399 word = strtok_r(NULL, " ,:\t", &save_ptr);
401 msg = xstrdup("missing facility");
405 facility = (!strcasecmp(word, "ANY")
407 : vlog_get_facility_val(word));
408 if (facility == VLF_N_FACILITIES) {
409 msg = xasprintf("unknown facility \"%s\"", word);
412 vlog_set_pattern(facility, save_ptr);
414 struct vlog_module *module = NULL;
415 enum vlog_level level = VLL_N_LEVELS;
416 enum vlog_facility facility = VLF_N_FACILITIES;
418 for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) {
419 if (!strcasecmp(word, "ANY")) {
421 } else if (vlog_get_facility_val(word) != VLF_N_FACILITIES) {
422 if (facility != VLF_N_FACILITIES) {
423 msg = xstrdup("cannot specify multiple facilities");
426 facility = vlog_get_facility_val(word);
427 } else if (vlog_get_level_val(word) != VLL_N_LEVELS) {
428 if (level != VLL_N_LEVELS) {
429 msg = xstrdup("cannot specify multiple levels");
432 level = vlog_get_level_val(word);
433 } else if (vlog_module_from_name(word)) {
435 msg = xstrdup("cannot specify multiple modules");
438 module = vlog_module_from_name(word);
440 msg = xasprintf("no facility, level, or module \"%s\"", word);
445 if (facility == VLF_N_FACILITIES) {
446 facility = VLF_ANY_FACILITY;
448 if (level == VLL_N_LEVELS) {
451 vlog_set_levels(module, facility, level);
459 /* Set debugging levels. Abort with an error message if 's' is invalid. */
461 vlog_set_levels_from_string_assert(const char *s)
463 char *error = vlog_set_levels_from_string(s);
465 ovs_fatal(0, "%s", error);
469 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
470 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
472 vlog_set_verbosity(const char *arg)
475 char *msg = vlog_set_levels_from_string(arg);
477 ovs_fatal(0, "processing \"%s\": %s", arg, msg);
480 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_DBG);
485 vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[],
486 void *aux OVS_UNUSED)
490 for (i = 1; i < argc; i++) {
491 char *msg = vlog_set_levels_from_string(argv[i]);
493 unixctl_command_reply_error(conn, msg);
498 unixctl_command_reply(conn, NULL);
502 vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
503 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
505 char *msg = vlog_get_levels();
506 unixctl_command_reply(conn, msg);
511 vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED,
512 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
516 ovs_mutex_lock(&log_file_mutex);
517 has_log_file = log_file_name != NULL;
518 ovs_mutex_unlock(&log_file_mutex);
521 int error = vlog_reopen_log_file();
523 unixctl_command_reply_error(conn, ovs_strerror(errno));
525 unixctl_command_reply(conn, NULL);
528 unixctl_command_reply_error(conn, "Logging to file not configured");
533 set_all_rate_limits(bool enable)
535 struct vlog_module **mp;
537 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
538 (*mp)->honor_rate_limits = enable;
543 set_rate_limits(struct unixctl_conn *conn, int argc,
544 const char *argv[], bool enable)
549 for (i = 1; i < argc; i++) {
550 if (!strcasecmp(argv[i], "ANY")) {
551 set_all_rate_limits(enable);
553 struct vlog_module *module = vlog_module_from_name(argv[i]);
555 unixctl_command_reply_error(conn, "unknown module");
558 module->honor_rate_limits = enable;
562 set_all_rate_limits(enable);
564 unixctl_command_reply(conn, NULL);
568 vlog_enable_rate_limit(struct unixctl_conn *conn, int argc,
569 const char *argv[], void *aux OVS_UNUSED)
571 set_rate_limits(conn, argc, argv, true);
575 vlog_disable_rate_limit(struct unixctl_conn *conn, int argc,
576 const char *argv[], void *aux OVS_UNUSED)
578 set_rate_limits(conn, argc, argv, false);
584 static char *program_name_copy;
587 /* openlog() is allowed to keep the pointer passed in, without making a
588 * copy. The daemonize code sometimes frees and replaces 'program_name',
589 * so make a private copy just for openlog(). (We keep a pointer to the
590 * private copy to suppress memory leak warnings in case openlog() does
591 * make its own copy.) */
592 program_name_copy = program_name ? xstrdup(program_name) : NULL;
593 openlog(program_name_copy, LOG_NDELAY, LOG_DAEMON);
597 char *s = xastrftime("%a, %d %b %Y %H:%M:%S", now, true);
598 VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
602 unixctl_command_register(
603 "vlog/set", "{spec | PATTERN:facility:pattern}",
604 1, INT_MAX, vlog_unixctl_set, NULL);
605 unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list, NULL);
606 unixctl_command_register("vlog/enable-rate-limit", "[module]...",
607 0, INT_MAX, vlog_enable_rate_limit, NULL);
608 unixctl_command_register("vlog/disable-rate-limit", "[module]...",
609 0, INT_MAX, vlog_disable_rate_limit, NULL);
610 unixctl_command_register("vlog/reopen", "", 0, 0,
611 vlog_unixctl_reopen, NULL);
614 /* Initializes the logging subsystem and registers its unixctl server
619 static pthread_once_t once = PTHREAD_ONCE_INIT;
620 pthread_once(&once, vlog_init__);
623 /* Enables VLF_FILE log output to be written asynchronously to disk.
624 * Asynchronous file writes avoid blocking the process in the case of a busy
625 * disk, but on the other hand they are less robust: there is a chance that the
626 * write will not make it to the log file if the process crashes soon after the
629 vlog_enable_async(void)
631 ovs_mutex_lock(&log_file_mutex);
633 if (log_fd >= 0 && !log_writer) {
634 log_writer = async_append_create(log_fd);
636 ovs_mutex_unlock(&log_file_mutex);
639 /* Print the current logging level for each module. */
641 vlog_get_levels(void)
643 struct ds s = DS_EMPTY_INITIALIZER;
644 struct vlog_module **mp;
645 struct svec lines = SVEC_EMPTY_INITIALIZER;
649 ds_put_format(&s, " console syslog file\n");
650 ds_put_format(&s, " ------- ------ ------\n");
652 for (mp = vlog_modules; mp < &vlog_modules[n_vlog_modules]; mp++) {
656 ds_put_format(&line, "%-16s %4s %4s %4s",
657 vlog_get_module_name(*mp),
658 vlog_get_level_name(vlog_get_level(*mp, VLF_CONSOLE)),
659 vlog_get_level_name(vlog_get_level(*mp, VLF_SYSLOG)),
660 vlog_get_level_name(vlog_get_level(*mp, VLF_FILE)));
661 if (!(*mp)->honor_rate_limits) {
662 ds_put_cstr(&line, " (rate limiting disabled)");
664 ds_put_char(&line, '\n');
666 svec_add_nocopy(&lines, ds_steal_cstr(&line));
670 SVEC_FOR_EACH (i, line, &lines) {
671 ds_put_cstr(&s, line);
673 svec_destroy(&lines);
678 /* Returns true if a log message emitted for the given 'module' and 'level'
679 * would cause some log output, false if that module and level are completely
682 vlog_is_enabled(const struct vlog_module *module, enum vlog_level level)
684 return module->min_level >= level;
688 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
691 size_t n = strcspn(p + 1, "}");
692 size_t n_copy = MIN(n, out_size - 1);
693 memcpy(out, p + 1, n_copy);
697 ovs_strlcpy(out, def, out_size);
703 format_log_message(const struct vlog_module *module, enum vlog_level level,
704 enum vlog_facility facility,
705 const char *message, va_list args_, struct ds *s)
712 for (p = facilities[facility].pattern; *p != '\0'; ) {
713 const char *subprogram_name;
714 enum { LEFT, RIGHT } justify = RIGHT;
716 size_t length, field, used;
719 ds_put_char(s, *p++);
733 while (isdigit((unsigned char)*p)) {
734 field = (field * 10) + (*p - '0');
741 ds_put_cstr(s, program_name);
744 p = fetch_braces(p, "", tmp, sizeof tmp);
745 ds_put_cstr(s, vlog_get_module_name(module));
748 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
749 ds_put_strftime(s, tmp, time_wall(), false);
752 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
753 ds_put_strftime(s, tmp, time_wall(), true);
756 /* Format user-supplied log message and trim trailing new-lines. */
758 va_copy(args, args_);
759 ds_put_format_valist(s, message, args);
761 while (s->length > length && s->string[s->length - 1] == '\n') {
766 ds_put_format(s, "%u", *msg_num_get_unsafe());
769 ds_put_char(s, '\n');
772 ds_put_cstr(s, vlog_get_level_name(level));
775 ds_put_format(s, "%ld", (long int) getpid());
778 ds_put_format(s, "%lld", time_msec() - time_boot_msec());
781 subprogram_name = get_subprogram_name();
782 ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main");
785 subprogram_name = get_subprogram_name();
786 if (subprogram_name[0]) {
787 ds_put_format(s, "(%s)", subprogram_name);
791 ds_put_char(s, p[-1]);
794 used = s->length - length;
796 size_t n_pad = field - used;
797 if (justify == RIGHT) {
798 ds_put_uninit(s, n_pad);
799 memmove(&s->string[length + n_pad], &s->string[length], used);
800 memset(&s->string[length], pad, n_pad);
802 ds_put_char_multiple(s, pad, n_pad);
808 /* Writes 'message' to the log at the given 'level' and as coming from the
811 * Guaranteed to preserve errno. */
813 vlog_valist(const struct vlog_module *module, enum vlog_level level,
814 const char *message, va_list args)
816 bool log_to_console = module->levels[VLF_CONSOLE] >= level;
817 bool log_to_syslog = module->levels[VLF_SYSLOG] >= level;
820 ovs_mutex_lock(&log_file_mutex);
821 log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0;
822 ovs_mutex_unlock(&log_file_mutex);
823 if (log_to_console || log_to_syslog || log_to_file) {
824 int save_errno = errno;
830 ds_reserve(&s, 1024);
833 ovs_rwlock_rdlock(&pattern_rwlock);
834 if (log_to_console) {
835 format_log_message(module, level, VLF_CONSOLE, message, args, &s);
836 ds_put_char(&s, '\n');
837 fputs(ds_cstr(&s), stderr);
841 int syslog_level = syslog_levels[level];
842 char *save_ptr = NULL;
845 format_log_message(module, level, VLF_SYSLOG, message, args, &s);
846 for (line = strtok_r(s.string, "\n", &save_ptr); line;
847 line = strtok_r(NULL, "\n", &save_ptr)) {
848 syslog(syslog_level, "%s", line);
853 format_log_message(module, level, VLF_FILE, message, args, &s);
854 ds_put_char(&s, '\n');
856 ovs_mutex_lock(&log_file_mutex);
859 async_append_write(log_writer, s.string, s.length);
860 if (level == VLL_EMER) {
861 async_append_flush(log_writer);
864 ignore(write(log_fd, s.string, s.length));
867 ovs_mutex_unlock(&log_file_mutex);
869 ovs_rwlock_unlock(&pattern_rwlock);
877 vlog(const struct vlog_module *module, enum vlog_level level,
878 const char *message, ...)
882 va_start(args, message);
883 vlog_valist(module, level, message, args);
887 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
888 * exit code. Always writes the message to stderr, even if the console
889 * facility is disabled.
891 * Choose this function instead of vlog_abort_valist() if the daemon monitoring
892 * facility shouldn't automatically restart the current daemon. */
894 vlog_fatal_valist(const struct vlog_module *module_,
895 const char *message, va_list args)
897 struct vlog_module *module = CONST_CAST(struct vlog_module *, module_);
899 /* Don't log this message to the console to avoid redundancy with the
900 * message written by the later ovs_fatal_valist(). */
901 module->levels[VLF_CONSOLE] = VLL_OFF;
903 vlog_valist(module, VLL_EMER, message, args);
904 ovs_fatal_valist(0, message, args);
907 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
908 * exit code. Always writes the message to stderr, even if the console
909 * facility is disabled.
911 * Choose this function instead of vlog_abort() if the daemon monitoring
912 * facility shouldn't automatically restart the current daemon. */
914 vlog_fatal(const struct vlog_module *module, const char *message, ...)
918 va_start(args, message);
919 vlog_fatal_valist(module, message, args);
923 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
924 * writes the message to stderr, even if the console facility is disabled.
926 * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
927 * facility should automatically restart the current daemon. */
929 vlog_abort_valist(const struct vlog_module *module_,
930 const char *message, va_list args)
932 struct vlog_module *module = (struct vlog_module *) module_;
934 /* Don't log this message to the console to avoid redundancy with the
935 * message written by the later ovs_abort_valist(). */
936 module->levels[VLF_CONSOLE] = VLL_OFF;
938 vlog_valist(module, VLL_EMER, message, args);
939 ovs_abort_valist(0, message, args);
942 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
943 * writes the message to stderr, even if the console facility is disabled.
945 * Choose this function instead of vlog_fatal() if the daemon monitoring
946 * facility should automatically restart the current daemon. */
948 vlog_abort(const struct vlog_module *module, const char *message, ...)
952 va_start(args, message);
953 vlog_abort_valist(module, message, args);
958 vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
959 struct vlog_rate_limit *rl)
961 if (!module->honor_rate_limits) {
965 if (!vlog_is_enabled(module, level)) {
969 ovs_mutex_lock(&rl->mutex);
970 if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) {
971 time_t now = time_now();
972 if (!rl->n_dropped) {
973 rl->first_dropped = now;
975 rl->last_dropped = now;
977 ovs_mutex_unlock(&rl->mutex);
981 if (!rl->n_dropped) {
982 ovs_mutex_unlock(&rl->mutex);
984 time_t now = time_now();
985 unsigned int n_dropped = rl->n_dropped;
986 unsigned int first_dropped_elapsed = now - rl->first_dropped;
987 unsigned int last_dropped_elapsed = now - rl->last_dropped;
989 ovs_mutex_unlock(&rl->mutex);
992 "Dropped %u log messages in last %u seconds (most recently, "
993 "%u seconds ago) due to excessive rate",
994 n_dropped, first_dropped_elapsed, last_dropped_elapsed);
1001 vlog_rate_limit(const struct vlog_module *module, enum vlog_level level,
1002 struct vlog_rate_limit *rl, const char *message, ...)
1004 if (!vlog_should_drop(module, level, rl)) {
1007 va_start(args, message);
1008 vlog_valist(module, level, message, args);
1016 printf("\nLogging options:\n"
1017 " -v, --verbose=[SPEC] set logging levels\n"
1018 " -v, --verbose set maximum verbosity level\n"
1019 " --log-file[=FILE] enable logging to specified FILE\n"
1020 " (default: %s/%s.log)\n",
1021 ovs_logdir(), program_name);