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.
18 #include "openvswitch/vlog.h"
27 #include <sys/types.h>
31 #include "async-append.h"
34 #include "dynamic-string.h"
36 #include "ovs-thread.h"
38 #include "socket-util.h"
44 VLOG_DEFINE_THIS_MODULE(vlog);
46 /* ovs_assert() logs the assertion message, so using ovs_assert() in this
47 * source file could cause recursion. */
49 #define ovs_assert use_assert_instead_of_ovs_assert_in_this_module
51 /* Name for each logging level. */
52 static const char *const level_names[VLL_N_LEVELS] = {
53 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) #NAME,
58 /* Syslog value for each logging level. */
59 static const int syslog_levels[VLL_N_LEVELS] = {
60 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) SYSLOG_LEVEL,
65 /* RFC 5424 defines specific values for each syslog level. Normally LOG_* use
66 * the same values. Verify that in fact they're the same. If we get assertion
67 * failures here then we need to define a separate rfc5424_levels[] array. */
68 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) \
69 BUILD_ASSERT_DECL(SYSLOG_LEVEL == RFC5424);
73 /* Similarly, RFC 5424 defines the local0 facility with the value ordinarily
74 * used for LOG_LOCAL0. */
75 BUILD_ASSERT_DECL(LOG_LOCAL0 == (16 << 3));
77 /* The log modules. */
78 struct ovs_list vlog_modules = OVS_LIST_INITIALIZER(&vlog_modules);
80 /* Protects the 'pattern' in all "struct facility"s, so that a race between
81 * changing and reading the pattern does not cause an access to freed
83 static struct ovs_rwlock pattern_rwlock = OVS_RWLOCK_INITIALIZER;
85 /* Information about each facility. */
87 const char *name; /* Name. */
88 char *pattern OVS_GUARDED_BY(pattern_rwlock); /* Current pattern. */
89 bool default_pattern; /* Whether current pattern is the default. */
91 static struct facility facilities[VLF_N_FACILITIES] = {
92 #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
97 /* Sequence number for the message currently being composed. */
98 DEFINE_STATIC_PER_THREAD_DATA(unsigned int, msg_num, 0);
100 /* VLF_FILE configuration.
102 * All of the following is protected by 'log_file_mutex', which nests inside
104 static struct ovs_mutex log_file_mutex = OVS_MUTEX_INITIALIZER;
105 static char *log_file_name OVS_GUARDED_BY(log_file_mutex);
106 static int log_fd OVS_GUARDED_BY(log_file_mutex) = -1;
107 static struct async_append *log_writer OVS_GUARDED_BY(log_file_mutex);
108 static bool log_async OVS_GUARDED_BY(log_file_mutex);
110 /* Syslog export configuration. */
111 static int syslog_fd OVS_GUARDED_BY(pattern_rwlock) = -1;
113 static void format_log_message(const struct vlog_module *, enum vlog_level,
115 const char *message, va_list, struct ds *)
116 OVS_PRINTF_FORMAT(4, 0);
118 /* Searches the 'n_names' in 'names'. Returns the index of a match for
119 * 'target', or 'n_names' if no name matches. */
121 search_name_array(const char *target, const char *const *names, size_t n_names)
125 for (i = 0; i < n_names; i++) {
127 if (!strcasecmp(names[i], target)) {
134 /* Returns the name for logging level 'level'. */
136 vlog_get_level_name(enum vlog_level level)
138 assert(level < VLL_N_LEVELS);
139 return level_names[level];
142 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
143 * is not the name of a logging level. */
145 vlog_get_level_val(const char *name)
147 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
150 /* Returns the name for logging facility 'facility'. */
152 vlog_get_facility_name(enum vlog_facility facility)
154 assert(facility < VLF_N_FACILITIES);
155 return facilities[facility].name;
158 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
159 * not the name of a logging facility. */
161 vlog_get_facility_val(const char *name)
165 for (i = 0; i < VLF_N_FACILITIES; i++) {
166 if (!strcasecmp(facilities[i].name, name)) {
173 void vlog_insert_module(struct ovs_list *vlog)
175 list_insert(&vlog_modules, vlog);
178 /* Returns the name for logging module 'module'. */
180 vlog_get_module_name(const struct vlog_module *module)
185 /* Returns the logging module named 'name', or NULL if 'name' is not the name
186 * of a logging module. */
188 vlog_module_from_name(const char *name)
190 struct vlog_module *mp;
192 LIST_FOR_EACH (mp, list, &vlog_modules) {
193 if (!strcasecmp(name, mp->name)) {
201 /* Returns the current logging level for the given 'module' and 'facility'. */
203 vlog_get_level(const struct vlog_module *module, enum vlog_facility facility)
205 assert(facility < VLF_N_FACILITIES);
206 return module->levels[facility];
210 update_min_level(struct vlog_module *module) OVS_REQUIRES(&log_file_mutex)
212 enum vlog_facility facility;
214 module->min_level = VLL_OFF;
215 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
216 if (log_fd >= 0 || facility != VLF_FILE) {
217 enum vlog_level level = module->levels[facility];
218 if (level > module->min_level) {
219 module->min_level = level;
226 set_facility_level(enum vlog_facility facility, struct vlog_module *module,
227 enum vlog_level level)
229 assert(facility >= 0 && facility < VLF_N_FACILITIES);
230 assert(level < VLL_N_LEVELS);
232 ovs_mutex_lock(&log_file_mutex);
234 struct vlog_module *mp;
235 LIST_FOR_EACH (mp, list, &vlog_modules) {
236 mp->levels[facility] = level;
237 update_min_level(mp);
240 module->levels[facility] = level;
241 update_min_level(module);
243 ovs_mutex_unlock(&log_file_mutex);
246 /* Sets the logging level for the given 'module' and 'facility' to 'level'. A
247 * null 'module' or a 'facility' of VLF_ANY_FACILITY is treated as a wildcard
248 * across all modules or facilities, respectively. */
250 vlog_set_levels(struct vlog_module *module, enum vlog_facility facility,
251 enum vlog_level level)
253 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
254 if (facility == VLF_ANY_FACILITY) {
255 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
256 set_facility_level(facility, module, level);
259 set_facility_level(facility, module, level);
264 do_set_pattern(enum vlog_facility facility, const char *pattern)
266 struct facility *f = &facilities[facility];
268 ovs_rwlock_wrlock(&pattern_rwlock);
269 if (!f->default_pattern) {
272 f->default_pattern = false;
274 f->pattern = xstrdup(pattern);
275 ovs_rwlock_unlock(&pattern_rwlock);
278 /* Sets the pattern for the given 'facility' to 'pattern'. */
280 vlog_set_pattern(enum vlog_facility facility, const char *pattern)
282 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
283 if (facility == VLF_ANY_FACILITY) {
284 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
285 do_set_pattern(facility, pattern);
288 do_set_pattern(facility, pattern);
292 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
293 * default file name if 'file_name' is null. Returns 0 if successful,
294 * otherwise a positive errno value. */
296 vlog_set_log_file(const char *file_name)
298 char *new_log_file_name;
299 struct vlog_module *mp;
300 struct stat old_stat;
301 struct stat new_stat;
306 /* Open new log file. */
307 new_log_file_name = (file_name
309 : xasprintf("%s/%s.log", ovs_logdir(), program_name));
310 new_log_fd = open(new_log_file_name, O_WRONLY | O_CREAT | O_APPEND, 0666);
311 if (new_log_fd < 0) {
312 VLOG_WARN("failed to open %s for logging: %s",
313 new_log_file_name, ovs_strerror(errno));
314 free(new_log_file_name);
318 /* If the new log file is the same one we already have open, bail out. */
319 ovs_mutex_lock(&log_file_mutex);
320 same_file = (log_fd >= 0
322 && !fstat(log_fd, &old_stat)
323 && !fstat(new_log_fd, &new_stat)
324 && old_stat.st_dev == new_stat.st_dev
325 && old_stat.st_ino == new_stat.st_ino);
326 ovs_mutex_unlock(&log_file_mutex);
329 free(new_log_file_name);
333 /* Log closing old log file (we can't log while holding log_file_mutex). */
334 ovs_mutex_lock(&log_file_mutex);
335 log_close = log_fd >= 0;
336 ovs_mutex_unlock(&log_file_mutex);
338 VLOG_INFO("closing log file");
341 /* Close old log file, if any, and install new one. */
342 ovs_mutex_lock(&log_file_mutex);
346 async_append_destroy(log_writer);
349 log_file_name = xstrdup(new_log_file_name);
352 log_writer = async_append_create(new_log_fd);
355 LIST_FOR_EACH (mp, list, &vlog_modules) {
356 update_min_level(mp);
358 ovs_mutex_unlock(&log_file_mutex);
360 /* Log opening new log file (we can't log while holding log_file_mutex). */
361 VLOG_INFO("opened log file %s", new_log_file_name);
362 free(new_log_file_name);
367 /* Closes and then attempts to re-open the current log file. (This is useful
368 * just after log rotation, to ensure that the new log file starts being used.)
369 * Returns 0 if successful, otherwise a positive errno value. */
371 vlog_reopen_log_file(void)
375 ovs_mutex_lock(&log_file_mutex);
376 fn = log_file_name ? xstrdup(log_file_name) : NULL;
377 ovs_mutex_unlock(&log_file_mutex);
380 int error = vlog_set_log_file(fn);
388 /* Set debugging levels. Returns null if successful, otherwise an error
389 * message that the caller must free(). */
391 vlog_set_levels_from_string(const char *s_)
393 char *s = xstrdup(s_);
394 char *save_ptr = NULL;
398 word = strtok_r(s, " ,:\t", &save_ptr);
399 if (word && !strcasecmp(word, "PATTERN")) {
400 enum vlog_facility facility;
402 word = strtok_r(NULL, " ,:\t", &save_ptr);
404 msg = xstrdup("missing facility");
408 facility = (!strcasecmp(word, "ANY")
410 : vlog_get_facility_val(word));
411 if (facility == VLF_N_FACILITIES) {
412 msg = xasprintf("unknown facility \"%s\"", word);
415 vlog_set_pattern(facility, save_ptr);
417 struct vlog_module *module = NULL;
418 enum vlog_level level = VLL_N_LEVELS;
419 enum vlog_facility facility = VLF_N_FACILITIES;
421 for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) {
422 if (!strcasecmp(word, "ANY")) {
424 } else if (vlog_get_facility_val(word) != VLF_N_FACILITIES) {
425 if (facility != VLF_N_FACILITIES) {
426 msg = xstrdup("cannot specify multiple facilities");
429 facility = vlog_get_facility_val(word);
430 } else if (vlog_get_level_val(word) != VLL_N_LEVELS) {
431 if (level != VLL_N_LEVELS) {
432 msg = xstrdup("cannot specify multiple levels");
435 level = vlog_get_level_val(word);
436 } else if (vlog_module_from_name(word)) {
438 msg = xstrdup("cannot specify multiple modules");
441 module = vlog_module_from_name(word);
443 msg = xasprintf("no facility, level, or module \"%s\"", word);
448 if (facility == VLF_N_FACILITIES) {
449 facility = VLF_ANY_FACILITY;
451 if (level == VLL_N_LEVELS) {
454 vlog_set_levels(module, facility, level);
462 /* Set debugging levels. Abort with an error message if 's' is invalid. */
464 vlog_set_levels_from_string_assert(const char *s)
466 char *error = vlog_set_levels_from_string(s);
468 ovs_fatal(0, "%s", error);
472 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
473 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
475 vlog_set_verbosity(const char *arg)
478 char *msg = vlog_set_levels_from_string(arg);
480 ovs_fatal(0, "processing \"%s\": %s", arg, msg);
483 vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_DBG);
487 /* Set the vlog udp syslog target. */
489 vlog_set_syslog_target(const char *target)
493 inet_open_active(SOCK_DGRAM, target, 0, NULL, &new_fd, 0);
495 ovs_rwlock_wrlock(&pattern_rwlock);
496 if (syslog_fd >= 0) {
500 ovs_rwlock_unlock(&pattern_rwlock);
504 vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[],
505 void *aux OVS_UNUSED)
509 for (i = 1; i < argc; i++) {
510 char *msg = vlog_set_levels_from_string(argv[i]);
512 unixctl_command_reply_error(conn, msg);
517 unixctl_command_reply(conn, NULL);
521 vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
522 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
524 char *msg = vlog_get_levels();
525 unixctl_command_reply(conn, msg);
530 vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED,
531 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
535 ovs_mutex_lock(&log_file_mutex);
536 has_log_file = log_file_name != NULL;
537 ovs_mutex_unlock(&log_file_mutex);
540 int error = vlog_reopen_log_file();
542 unixctl_command_reply_error(conn, ovs_strerror(errno));
544 unixctl_command_reply(conn, NULL);
547 unixctl_command_reply_error(conn, "Logging to file not configured");
552 set_all_rate_limits(bool enable)
554 struct vlog_module *mp;
556 LIST_FOR_EACH (mp, list, &vlog_modules) {
557 mp->honor_rate_limits = enable;
562 set_rate_limits(struct unixctl_conn *conn, int argc,
563 const char *argv[], bool enable)
568 for (i = 1; i < argc; i++) {
569 if (!strcasecmp(argv[i], "ANY")) {
570 set_all_rate_limits(enable);
572 struct vlog_module *module = vlog_module_from_name(argv[i]);
574 unixctl_command_reply_error(conn, "unknown module");
577 module->honor_rate_limits = enable;
581 set_all_rate_limits(enable);
583 unixctl_command_reply(conn, NULL);
587 vlog_enable_rate_limit(struct unixctl_conn *conn, int argc,
588 const char *argv[], void *aux OVS_UNUSED)
590 set_rate_limits(conn, argc, argv, true);
594 vlog_disable_rate_limit(struct unixctl_conn *conn, int argc,
595 const char *argv[], void *aux OVS_UNUSED)
597 set_rate_limits(conn, argc, argv, false);
600 /* Initializes the logging subsystem and registers its unixctl server
605 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
607 if (ovsthread_once_start(&once)) {
608 static char *program_name_copy;
611 /* Do initialization work that needs to be done before any logging
612 * occurs. We want to keep this really minimal because any attempt to
613 * log anything before calling ovsthread_once_done() will deadlock. */
615 /* openlog() is allowed to keep the pointer passed in, without making a
616 * copy. The daemonize code sometimes frees and replaces
617 * 'program_name', so make a private copy just for openlog(). (We keep
618 * a pointer to the private copy to suppress memory leak warnings in
619 * case openlog() does make its own copy.) */
620 program_name_copy = program_name ? xstrdup(program_name) : NULL;
621 openlog(program_name_copy, LOG_NDELAY, LOG_DAEMON);
622 ovsthread_once_done(&once);
624 /* Now do anything that we want to happen only once but doesn't have to
625 * finish before we start logging. */
627 now = time_wall_msec();
629 char *s = xastrftime_msec("%a, %d %b %Y %H:%M:%S", now, true);
630 VLOG_ERR("current time is negative: %s (%lld)", s, now);
634 unixctl_command_register(
635 "vlog/set", "{spec | PATTERN:facility:pattern}",
636 1, INT_MAX, vlog_unixctl_set, NULL);
637 unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list,
639 unixctl_command_register("vlog/enable-rate-limit", "[module]...",
640 0, INT_MAX, vlog_enable_rate_limit, NULL);
641 unixctl_command_register("vlog/disable-rate-limit", "[module]...",
642 0, INT_MAX, vlog_disable_rate_limit, NULL);
643 unixctl_command_register("vlog/reopen", "", 0, 0,
644 vlog_unixctl_reopen, NULL);
648 /* Enables VLF_FILE log output to be written asynchronously to disk.
649 * Asynchronous file writes avoid blocking the process in the case of a busy
650 * disk, but on the other hand they are less robust: there is a chance that the
651 * write will not make it to the log file if the process crashes soon after the
654 vlog_enable_async(void)
656 ovs_mutex_lock(&log_file_mutex);
658 if (log_fd >= 0 && !log_writer) {
659 log_writer = async_append_create(log_fd);
661 ovs_mutex_unlock(&log_file_mutex);
664 /* Print the current logging level for each module. */
666 vlog_get_levels(void)
668 struct ds s = DS_EMPTY_INITIALIZER;
669 struct vlog_module *mp;
670 struct svec lines = SVEC_EMPTY_INITIALIZER;
674 ds_put_format(&s, " console syslog file\n");
675 ds_put_format(&s, " ------- ------ ------\n");
677 LIST_FOR_EACH (mp, list, &vlog_modules) {
681 ds_put_format(&line, "%-16s %4s %4s %4s",
682 vlog_get_module_name(mp),
683 vlog_get_level_name(vlog_get_level(mp, VLF_CONSOLE)),
684 vlog_get_level_name(vlog_get_level(mp, VLF_SYSLOG)),
685 vlog_get_level_name(vlog_get_level(mp, VLF_FILE)));
686 if (!mp->honor_rate_limits) {
687 ds_put_cstr(&line, " (rate limiting disabled)");
689 ds_put_char(&line, '\n');
691 svec_add_nocopy(&lines, ds_steal_cstr(&line));
695 SVEC_FOR_EACH (i, line, &lines) {
696 ds_put_cstr(&s, line);
698 svec_destroy(&lines);
703 /* Returns true if a log message emitted for the given 'module' and 'level'
704 * would cause some log output, false if that module and level are completely
707 vlog_is_enabled(const struct vlog_module *module, enum vlog_level level)
709 return module->min_level >= level;
713 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
716 size_t n = strcspn(p + 1, "}");
717 size_t n_copy = MIN(n, out_size - 1);
718 memcpy(out, p + 1, n_copy);
722 ovs_strlcpy(out, def, out_size);
728 format_log_message(const struct vlog_module *module, enum vlog_level level,
729 const char *pattern, const char *message,
730 va_list args_, struct ds *s)
737 for (p = pattern; *p != '\0'; ) {
738 const char *subprogram_name;
739 enum { LEFT, RIGHT } justify = RIGHT;
741 size_t length, field, used;
744 ds_put_char(s, *p++);
758 while (isdigit((unsigned char)*p)) {
759 field = (field * 10) + (*p - '0');
766 ds_put_cstr(s, program_name);
769 ds_put_format(s, "%d", LOG_LOCAL0 + syslog_levels[level]);
772 p = fetch_braces(p, "", tmp, sizeof tmp);
773 ds_put_cstr(s, vlog_get_module_name(module));
776 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
777 ds_put_strftime_msec(s, tmp, time_wall_msec(), false);
780 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
781 ds_put_strftime_msec(s, tmp, time_wall_msec(), true);
784 gethostname(tmp, sizeof tmp);
785 tmp[sizeof tmp - 1] = '\0';
789 /* Format user-supplied log message and trim trailing new-lines. */
791 va_copy(args, args_);
792 ds_put_format_valist(s, message, args);
794 while (s->length > length && s->string[s->length - 1] == '\n') {
799 ds_put_format(s, "%u", *msg_num_get_unsafe());
802 ds_put_char(s, '\n');
805 ds_put_cstr(s, vlog_get_level_name(level));
808 ds_put_format(s, "%ld", (long int) getpid());
811 ds_put_format(s, "%lld", time_msec() - time_boot_msec());
814 subprogram_name = get_subprogram_name();
815 ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main");
818 subprogram_name = get_subprogram_name();
819 if (subprogram_name[0]) {
820 ds_put_format(s, "(%s)", subprogram_name);
824 ds_put_char(s, p[-1]);
827 used = s->length - length;
829 size_t n_pad = field - used;
830 if (justify == RIGHT) {
831 ds_put_uninit(s, n_pad);
832 memmove(&s->string[length + n_pad], &s->string[length], used);
833 memset(&s->string[length], pad, n_pad);
835 ds_put_char_multiple(s, pad, n_pad);
841 /* Exports the given 'syslog_message' to the configured udp syslog sink. */
843 send_to_syslog_fd(const char *s, size_t length)
844 OVS_REQ_RDLOCK(pattern_rwlock)
846 static size_t max_length = SIZE_MAX;
847 size_t send_len = MIN(length, max_length);
849 while (write(syslog_fd, s, send_len) < 0 && errno == EMSGSIZE) {
850 send_len -= send_len / 20;
851 max_length = send_len;
855 /* Writes 'message' to the log at the given 'level' and as coming from the
858 * Guaranteed to preserve errno. */
860 vlog_valist(const struct vlog_module *module, enum vlog_level level,
861 const char *message, va_list args)
863 bool log_to_console = module->levels[VLF_CONSOLE] >= level;
864 bool log_to_syslog = module->levels[VLF_SYSLOG] >= level;
867 ovs_mutex_lock(&log_file_mutex);
868 log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0;
869 ovs_mutex_unlock(&log_file_mutex);
870 if (log_to_console || log_to_syslog || log_to_file) {
871 int save_errno = errno;
877 ds_reserve(&s, 1024);
880 ovs_rwlock_rdlock(&pattern_rwlock);
881 if (log_to_console) {
882 format_log_message(module, level, facilities[VLF_CONSOLE].pattern,
884 ds_put_char(&s, '\n');
885 fputs(ds_cstr(&s), stderr);
889 int syslog_level = syslog_levels[level];
890 char *save_ptr = NULL;
893 format_log_message(module, level, facilities[VLF_SYSLOG].pattern,
895 for (line = strtok_r(s.string, "\n", &save_ptr); line;
896 line = strtok_r(NULL, "\n", &save_ptr)) {
897 syslog(syslog_level, "%s", line);
900 if (syslog_fd >= 0) {
901 format_log_message(module, level,
902 "<%B>1 %D{%Y-%m-%dT%H:%M:%S.###Z} "
903 "%E %A %P %c - \xef\xbb\xbf%m",
905 send_to_syslog_fd(ds_cstr(&s), s.length);
910 format_log_message(module, level, facilities[VLF_FILE].pattern,
912 ds_put_char(&s, '\n');
914 ovs_mutex_lock(&log_file_mutex);
917 async_append_write(log_writer, s.string, s.length);
918 if (level == VLL_EMER) {
919 async_append_flush(log_writer);
922 ignore(write(log_fd, s.string, s.length));
925 ovs_mutex_unlock(&log_file_mutex);
927 ovs_rwlock_unlock(&pattern_rwlock);
935 vlog(const struct vlog_module *module, enum vlog_level level,
936 const char *message, ...)
940 va_start(args, message);
941 vlog_valist(module, level, message, args);
945 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
946 * exit code. Always writes the message to stderr, even if the console
947 * facility is disabled.
949 * Choose this function instead of vlog_abort_valist() if the daemon monitoring
950 * facility shouldn't automatically restart the current daemon. */
952 vlog_fatal_valist(const struct vlog_module *module_,
953 const char *message, va_list args)
955 struct vlog_module *module = CONST_CAST(struct vlog_module *, module_);
957 /* Don't log this message to the console to avoid redundancy with the
958 * message written by the later ovs_fatal_valist(). */
959 module->levels[VLF_CONSOLE] = VLL_OFF;
961 vlog_valist(module, VLL_EMER, message, args);
962 ovs_fatal_valist(0, message, args);
965 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
966 * exit code. Always writes the message to stderr, even if the console
967 * facility is disabled.
969 * Choose this function instead of vlog_abort() if the daemon monitoring
970 * facility shouldn't automatically restart the current daemon. */
972 vlog_fatal(const struct vlog_module *module, const char *message, ...)
976 va_start(args, message);
977 vlog_fatal_valist(module, message, args);
981 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
982 * writes the message to stderr, even if the console facility is disabled.
984 * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
985 * facility should automatically restart the current daemon. */
987 vlog_abort_valist(const struct vlog_module *module_,
988 const char *message, va_list args)
990 struct vlog_module *module = (struct vlog_module *) module_;
992 /* Don't log this message to the console to avoid redundancy with the
993 * message written by the later ovs_abort_valist(). */
994 module->levels[VLF_CONSOLE] = VLL_OFF;
996 vlog_valist(module, VLL_EMER, message, args);
997 ovs_abort_valist(0, message, args);
1000 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1001 * writes the message to stderr, even if the console facility is disabled.
1003 * Choose this function instead of vlog_fatal() if the daemon monitoring
1004 * facility should automatically restart the current daemon. */
1006 vlog_abort(const struct vlog_module *module, const char *message, ...)
1010 va_start(args, message);
1011 vlog_abort_valist(module, message, args);
1016 vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
1017 struct vlog_rate_limit *rl)
1019 if (!module->honor_rate_limits) {
1023 if (!vlog_is_enabled(module, level)) {
1027 ovs_mutex_lock(&rl->mutex);
1028 if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) {
1029 time_t now = time_now();
1030 if (!rl->n_dropped) {
1031 rl->first_dropped = now;
1033 rl->last_dropped = now;
1035 ovs_mutex_unlock(&rl->mutex);
1039 if (!rl->n_dropped) {
1040 ovs_mutex_unlock(&rl->mutex);
1042 time_t now = time_now();
1043 unsigned int n_dropped = rl->n_dropped;
1044 unsigned int first_dropped_elapsed = now - rl->first_dropped;
1045 unsigned int last_dropped_elapsed = now - rl->last_dropped;
1047 ovs_mutex_unlock(&rl->mutex);
1050 "Dropped %u log messages in last %u seconds (most recently, "
1051 "%u seconds ago) due to excessive rate",
1052 n_dropped, first_dropped_elapsed, last_dropped_elapsed);
1059 vlog_rate_limit(const struct vlog_module *module, enum vlog_level level,
1060 struct vlog_rate_limit *rl, const char *message, ...)
1062 if (!vlog_should_drop(module, level, rl)) {
1065 va_start(args, message);
1066 vlog_valist(module, level, message, args);
1076 -vSPEC, --verbose=SPEC set logging levels\n\
1077 -v, --verbose set maximum verbosity level\n\
1078 --log-file[=FILE] enable logging to specified FILE\n\
1079 (default: %s/%s.log)\n\
1080 --syslog-target=HOST:PORT also send syslog msgs to HOST:PORT via UDP\n",
1081 ovs_logdir(), program_name);