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 destination"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 destination. */
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 destination destinations[VLF_N_DESTINATIONS] = {
92 #define VLOG_DESTINATION(NAME, PATTERN) {#NAME, PATTERN, true},
94 #undef VLOG_DESTINATION
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 /* Log facility configuration. */
114 static atomic_int log_facility = ATOMIC_VAR_INIT(0);
116 /* Facility name and its value. */
117 struct vlog_facility {
118 char *name; /* Name. */
119 unsigned int value; /* Facility associated with 'name'. */
121 static struct vlog_facility vlog_facilities[] = {
125 {"daemon", LOG_DAEMON},
127 {"syslog", LOG_SYSLOG},
137 {"local0", LOG_LOCAL0},
138 {"local1", LOG_LOCAL1},
139 {"local2", LOG_LOCAL2},
140 {"local3", LOG_LOCAL3},
141 {"local4", LOG_LOCAL4},
142 {"local5", LOG_LOCAL5},
143 {"local6", LOG_LOCAL6},
144 {"local7", LOG_LOCAL7}
146 static bool vlog_facility_exists(const char* facility, int *value);
148 static void format_log_message(const struct vlog_module *, enum vlog_level,
150 const char *message, va_list, struct ds *)
151 OVS_PRINTF_FORMAT(4, 0);
153 /* Searches the 'n_names' in 'names'. Returns the index of a match for
154 * 'target', or 'n_names' if no name matches. */
156 search_name_array(const char *target, const char *const *names, size_t n_names)
160 for (i = 0; i < n_names; i++) {
162 if (!strcasecmp(names[i], target)) {
169 /* Returns the name for logging level 'level'. */
171 vlog_get_level_name(enum vlog_level level)
173 assert(level < VLL_N_LEVELS);
174 return level_names[level];
177 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
178 * is not the name of a logging level. */
180 vlog_get_level_val(const char *name)
182 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
185 /* Returns the name for logging destination 'destination'. */
187 vlog_get_destination_name(enum vlog_destination destination)
189 assert(destination < VLF_N_DESTINATIONS);
190 return destinations[destination].name;
193 /* Returns the logging destination named 'name', or VLF_N_DESTINATIONS if
194 * 'name' is not the name of a logging destination. */
195 enum vlog_destination
196 vlog_get_destination_val(const char *name)
200 for (i = 0; i < VLF_N_DESTINATIONS; i++) {
201 if (!strcasecmp(destinations[i].name, name)) {
208 void vlog_insert_module(struct ovs_list *vlog)
210 list_insert(&vlog_modules, vlog);
213 /* Returns the name for logging module 'module'. */
215 vlog_get_module_name(const struct vlog_module *module)
220 /* Returns the logging module named 'name', or NULL if 'name' is not the name
221 * of a logging module. */
223 vlog_module_from_name(const char *name)
225 struct vlog_module *mp;
227 LIST_FOR_EACH (mp, list, &vlog_modules) {
228 if (!strcasecmp(name, mp->name)) {
236 /* Returns the current logging level for the given 'module' and
239 vlog_get_level(const struct vlog_module *module,
240 enum vlog_destination destination)
242 assert(destination < VLF_N_DESTINATIONS);
243 return module->levels[destination];
247 update_min_level(struct vlog_module *module) OVS_REQUIRES(&log_file_mutex)
249 enum vlog_destination destination;
251 module->min_level = VLL_OFF;
252 for (destination = 0; destination < VLF_N_DESTINATIONS; destination++) {
253 if (log_fd >= 0 || destination != VLF_FILE) {
254 enum vlog_level level = module->levels[destination];
255 if (level > module->min_level) {
256 module->min_level = level;
263 set_destination_level(enum vlog_destination destination,
264 struct vlog_module *module, enum vlog_level level)
266 assert(destination >= 0 && destination < VLF_N_DESTINATIONS);
267 assert(level < VLL_N_LEVELS);
269 ovs_mutex_lock(&log_file_mutex);
271 struct vlog_module *mp;
272 LIST_FOR_EACH (mp, list, &vlog_modules) {
273 mp->levels[destination] = level;
274 update_min_level(mp);
277 module->levels[destination] = level;
278 update_min_level(module);
280 ovs_mutex_unlock(&log_file_mutex);
283 /* Sets the logging level for the given 'module' and 'destination' to 'level'.
284 * A null 'module' or a 'destination' of VLF_ANY_DESTINATION is treated as a
285 * wildcard across all modules or destinations, respectively. */
287 vlog_set_levels(struct vlog_module *module, enum vlog_destination destination,
288 enum vlog_level level)
290 assert(destination < VLF_N_DESTINATIONS ||
291 destination == VLF_ANY_DESTINATION);
292 if (destination == VLF_ANY_DESTINATION) {
293 for (destination = 0; destination < VLF_N_DESTINATIONS;
295 set_destination_level(destination, module, level);
298 set_destination_level(destination, module, level);
303 do_set_pattern(enum vlog_destination destination, const char *pattern)
305 struct destination *f = &destinations[destination];
307 ovs_rwlock_wrlock(&pattern_rwlock);
308 if (!f->default_pattern) {
311 f->default_pattern = false;
313 f->pattern = xstrdup(pattern);
314 ovs_rwlock_unlock(&pattern_rwlock);
317 /* Sets the pattern for the given 'destination' to 'pattern'. */
319 vlog_set_pattern(enum vlog_destination destination, const char *pattern)
321 assert(destination < VLF_N_DESTINATIONS ||
322 destination == VLF_ANY_DESTINATION);
323 if (destination == VLF_ANY_DESTINATION) {
324 for (destination = 0; destination < VLF_N_DESTINATIONS;
326 do_set_pattern(destination, pattern);
329 do_set_pattern(destination, pattern);
333 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
334 * default file name if 'file_name' is null. Returns 0 if successful,
335 * otherwise a positive errno value. */
337 vlog_set_log_file(const char *file_name)
339 char *new_log_file_name;
340 struct vlog_module *mp;
341 struct stat old_stat;
342 struct stat new_stat;
347 /* Open new log file. */
348 new_log_file_name = (file_name
350 : xasprintf("%s/%s.log", ovs_logdir(), program_name));
351 new_log_fd = open(new_log_file_name, O_WRONLY | O_CREAT | O_APPEND, 0666);
352 if (new_log_fd < 0) {
353 VLOG_WARN("failed to open %s for logging: %s",
354 new_log_file_name, ovs_strerror(errno));
355 free(new_log_file_name);
359 /* If the new log file is the same one we already have open, bail out. */
360 ovs_mutex_lock(&log_file_mutex);
361 same_file = (log_fd >= 0
363 && !fstat(log_fd, &old_stat)
364 && !fstat(new_log_fd, &new_stat)
365 && old_stat.st_dev == new_stat.st_dev
366 && old_stat.st_ino == new_stat.st_ino);
367 ovs_mutex_unlock(&log_file_mutex);
370 free(new_log_file_name);
374 /* Log closing old log file (we can't log while holding log_file_mutex). */
375 ovs_mutex_lock(&log_file_mutex);
376 log_close = log_fd >= 0;
377 ovs_mutex_unlock(&log_file_mutex);
379 VLOG_INFO("closing log file");
382 /* Close old log file, if any, and install new one. */
383 ovs_mutex_lock(&log_file_mutex);
387 async_append_destroy(log_writer);
390 log_file_name = xstrdup(new_log_file_name);
393 log_writer = async_append_create(new_log_fd);
396 LIST_FOR_EACH (mp, list, &vlog_modules) {
397 update_min_level(mp);
399 ovs_mutex_unlock(&log_file_mutex);
401 /* Log opening new log file (we can't log while holding log_file_mutex). */
402 VLOG_INFO("opened log file %s", new_log_file_name);
403 free(new_log_file_name);
408 /* Closes and then attempts to re-open the current log file. (This is useful
409 * just after log rotation, to ensure that the new log file starts being used.)
410 * Returns 0 if successful, otherwise a positive errno value. */
412 vlog_reopen_log_file(void)
416 ovs_mutex_lock(&log_file_mutex);
417 fn = log_file_name ? xstrdup(log_file_name) : NULL;
418 ovs_mutex_unlock(&log_file_mutex);
421 int error = vlog_set_log_file(fn);
429 /* Set debugging levels. Returns null if successful, otherwise an error
430 * message that the caller must free(). */
432 vlog_set_levels_from_string(const char *s_)
434 char *s = xstrdup(s_);
435 char *save_ptr = NULL;
439 word = strtok_r(s, " ,:\t", &save_ptr);
440 if (word && !strcasecmp(word, "PATTERN")) {
441 enum vlog_destination destination;
443 word = strtok_r(NULL, " ,:\t", &save_ptr);
445 msg = xstrdup("missing destination");
449 destination = (!strcasecmp(word, "ANY")
450 ? VLF_ANY_DESTINATION
451 : vlog_get_destination_val(word));
452 if (destination == VLF_N_DESTINATIONS) {
453 msg = xasprintf("unknown destination \"%s\"", word);
456 vlog_set_pattern(destination, save_ptr);
457 } else if (word && !strcasecmp(word, "FACILITY")) {
460 if (!vlog_facility_exists(save_ptr, &value)) {
461 msg = xstrdup("invalid facility");
464 atomic_store_explicit(&log_facility, value, memory_order_relaxed);
466 struct vlog_module *module = NULL;
467 enum vlog_level level = VLL_N_LEVELS;
468 enum vlog_destination destination = VLF_N_DESTINATIONS;
470 for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) {
471 if (!strcasecmp(word, "ANY")) {
473 } else if (vlog_get_destination_val(word) != VLF_N_DESTINATIONS) {
474 if (destination != VLF_N_DESTINATIONS) {
475 msg = xstrdup("cannot specify multiple destinations");
478 destination = vlog_get_destination_val(word);
479 } else if (vlog_get_level_val(word) != VLL_N_LEVELS) {
480 if (level != VLL_N_LEVELS) {
481 msg = xstrdup("cannot specify multiple levels");
484 level = vlog_get_level_val(word);
485 } else if (vlog_module_from_name(word)) {
487 msg = xstrdup("cannot specify multiple modules");
490 module = vlog_module_from_name(word);
492 msg = xasprintf("no destination, level, or module \"%s\"",
498 if (destination == VLF_N_DESTINATIONS) {
499 destination = VLF_ANY_DESTINATION;
501 if (level == VLL_N_LEVELS) {
504 vlog_set_levels(module, destination, level);
512 /* Set debugging levels. Abort with an error message if 's' is invalid. */
514 vlog_set_levels_from_string_assert(const char *s)
516 char *error = vlog_set_levels_from_string(s);
518 ovs_fatal(0, "%s", error);
522 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
523 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
525 vlog_set_verbosity(const char *arg)
528 char *msg = vlog_set_levels_from_string(arg);
530 ovs_fatal(0, "processing \"%s\": %s", arg, msg);
533 vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_DBG);
537 /* Set the vlog udp syslog target. */
539 vlog_set_syslog_target(const char *target)
543 inet_open_active(SOCK_DGRAM, target, 0, NULL, &new_fd, 0);
545 ovs_rwlock_wrlock(&pattern_rwlock);
546 if (syslog_fd >= 0) {
550 ovs_rwlock_unlock(&pattern_rwlock);
553 /* Returns 'false' if 'facility' is not a valid string. If 'facility'
554 * is a valid string, sets 'value' with the integer value of 'facility'
555 * and returns 'true'. */
557 vlog_facility_exists(const char* facility, int *value)
560 for (i = 0; i < ARRAY_SIZE(vlog_facilities); i++) {
561 if (!strcasecmp(vlog_facilities[i].name, facility)) {
562 *value = vlog_facilities[i].value;
570 vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[],
571 void *aux OVS_UNUSED)
575 for (i = 1; i < argc; i++) {
576 char *msg = vlog_set_levels_from_string(argv[i]);
578 unixctl_command_reply_error(conn, msg);
583 unixctl_command_reply(conn, NULL);
587 vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
588 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
590 char *msg = vlog_get_levels();
591 unixctl_command_reply(conn, msg);
596 vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED,
597 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
601 ovs_mutex_lock(&log_file_mutex);
602 has_log_file = log_file_name != NULL;
603 ovs_mutex_unlock(&log_file_mutex);
606 int error = vlog_reopen_log_file();
608 unixctl_command_reply_error(conn, ovs_strerror(errno));
610 unixctl_command_reply(conn, NULL);
613 unixctl_command_reply_error(conn, "Logging to file not configured");
618 set_all_rate_limits(bool enable)
620 struct vlog_module *mp;
622 LIST_FOR_EACH (mp, list, &vlog_modules) {
623 mp->honor_rate_limits = enable;
628 set_rate_limits(struct unixctl_conn *conn, int argc,
629 const char *argv[], bool enable)
634 for (i = 1; i < argc; i++) {
635 if (!strcasecmp(argv[i], "ANY")) {
636 set_all_rate_limits(enable);
638 struct vlog_module *module = vlog_module_from_name(argv[i]);
640 unixctl_command_reply_error(conn, "unknown module");
643 module->honor_rate_limits = enable;
647 set_all_rate_limits(enable);
649 unixctl_command_reply(conn, NULL);
653 vlog_enable_rate_limit(struct unixctl_conn *conn, int argc,
654 const char *argv[], void *aux OVS_UNUSED)
656 set_rate_limits(conn, argc, argv, true);
660 vlog_disable_rate_limit(struct unixctl_conn *conn, int argc,
661 const char *argv[], void *aux OVS_UNUSED)
663 set_rate_limits(conn, argc, argv, false);
666 /* Initializes the logging subsystem and registers its unixctl server
671 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
673 if (ovsthread_once_start(&once)) {
674 static char *program_name_copy;
678 /* Do initialization work that needs to be done before any logging
679 * occurs. We want to keep this really minimal because any attempt to
680 * log anything before calling ovsthread_once_done() will deadlock. */
682 /* openlog() is allowed to keep the pointer passed in, without making a
683 * copy. The daemonize code sometimes frees and replaces
684 * 'program_name', so make a private copy just for openlog(). (We keep
685 * a pointer to the private copy to suppress memory leak warnings in
686 * case openlog() does make its own copy.) */
687 program_name_copy = program_name ? xstrdup(program_name) : NULL;
688 atomic_read_explicit(&log_facility, &facility, memory_order_relaxed);
689 openlog(program_name_copy, LOG_NDELAY,
690 facility ? facility : LOG_DAEMON);
691 ovsthread_once_done(&once);
693 /* Now do anything that we want to happen only once but doesn't have to
694 * finish before we start logging. */
696 now = time_wall_msec();
698 char *s = xastrftime_msec("%a, %d %b %Y %H:%M:%S", now, true);
699 VLOG_ERR("current time is negative: %s (%lld)", s, now);
703 unixctl_command_register(
704 "vlog/set", "{spec | PATTERN:destination:pattern}",
705 1, INT_MAX, vlog_unixctl_set, NULL);
706 unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list,
708 unixctl_command_register("vlog/enable-rate-limit", "[module]...",
709 0, INT_MAX, vlog_enable_rate_limit, NULL);
710 unixctl_command_register("vlog/disable-rate-limit", "[module]...",
711 0, INT_MAX, vlog_disable_rate_limit, NULL);
712 unixctl_command_register("vlog/reopen", "", 0, 0,
713 vlog_unixctl_reopen, NULL);
717 /* Enables VLF_FILE log output to be written asynchronously to disk.
718 * Asynchronous file writes avoid blocking the process in the case of a busy
719 * disk, but on the other hand they are less robust: there is a chance that the
720 * write will not make it to the log file if the process crashes soon after the
723 vlog_enable_async(void)
725 ovs_mutex_lock(&log_file_mutex);
727 if (log_fd >= 0 && !log_writer) {
728 log_writer = async_append_create(log_fd);
730 ovs_mutex_unlock(&log_file_mutex);
733 /* Print the current logging level for each module. */
735 vlog_get_levels(void)
737 struct ds s = DS_EMPTY_INITIALIZER;
738 struct vlog_module *mp;
739 struct svec lines = SVEC_EMPTY_INITIALIZER;
743 ds_put_format(&s, " console syslog file\n");
744 ds_put_format(&s, " ------- ------ ------\n");
746 LIST_FOR_EACH (mp, list, &vlog_modules) {
750 ds_put_format(&line, "%-16s %4s %4s %4s",
751 vlog_get_module_name(mp),
752 vlog_get_level_name(vlog_get_level(mp, VLF_CONSOLE)),
753 vlog_get_level_name(vlog_get_level(mp, VLF_SYSLOG)),
754 vlog_get_level_name(vlog_get_level(mp, VLF_FILE)));
755 if (!mp->honor_rate_limits) {
756 ds_put_cstr(&line, " (rate limiting disabled)");
758 ds_put_char(&line, '\n');
760 svec_add_nocopy(&lines, ds_steal_cstr(&line));
764 SVEC_FOR_EACH (i, line, &lines) {
765 ds_put_cstr(&s, line);
767 svec_destroy(&lines);
772 /* Returns true if a log message emitted for the given 'module' and 'level'
773 * would cause some log output, false if that module and level are completely
776 vlog_is_enabled(const struct vlog_module *module, enum vlog_level level)
778 return module->min_level >= level;
782 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
785 size_t n = strcspn(p + 1, "}");
786 size_t n_copy = MIN(n, out_size - 1);
787 memcpy(out, p + 1, n_copy);
791 ovs_strlcpy(out, def, out_size);
797 format_log_message(const struct vlog_module *module, enum vlog_level level,
798 const char *pattern, const char *message,
799 va_list args_, struct ds *s)
807 for (p = pattern; *p != '\0'; ) {
808 const char *subprogram_name;
809 enum { LEFT, RIGHT } justify = RIGHT;
811 size_t length, field, used;
814 ds_put_char(s, *p++);
828 while (isdigit((unsigned char)*p)) {
829 field = (field * 10) + (*p - '0');
836 ds_put_cstr(s, program_name);
839 atomic_read_explicit(&log_facility, &facility,
840 memory_order_relaxed);
841 facility = facility ? facility : LOG_LOCAL0;
842 ds_put_format(s, "%d", facility + syslog_levels[level]);
845 p = fetch_braces(p, "", tmp, sizeof tmp);
846 ds_put_cstr(s, vlog_get_module_name(module));
849 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
850 ds_put_strftime_msec(s, tmp, time_wall_msec(), false);
853 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
854 ds_put_strftime_msec(s, tmp, time_wall_msec(), true);
857 gethostname(tmp, sizeof tmp);
858 tmp[sizeof tmp - 1] = '\0';
862 /* Format user-supplied log message and trim trailing new-lines. */
864 va_copy(args, args_);
865 ds_put_format_valist(s, message, args);
867 while (s->length > length && s->string[s->length - 1] == '\n') {
872 ds_put_format(s, "%u", *msg_num_get_unsafe());
875 ds_put_char(s, '\n');
878 ds_put_cstr(s, vlog_get_level_name(level));
881 ds_put_format(s, "%ld", (long int) getpid());
884 ds_put_format(s, "%lld", time_msec() - time_boot_msec());
887 subprogram_name = get_subprogram_name();
888 ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main");
891 subprogram_name = get_subprogram_name();
892 if (subprogram_name[0]) {
893 ds_put_format(s, "(%s)", subprogram_name);
897 ds_put_char(s, p[-1]);
900 used = s->length - length;
902 size_t n_pad = field - used;
903 if (justify == RIGHT) {
904 ds_put_uninit(s, n_pad);
905 memmove(&s->string[length + n_pad], &s->string[length], used);
906 memset(&s->string[length], pad, n_pad);
908 ds_put_char_multiple(s, pad, n_pad);
914 /* Exports the given 'syslog_message' to the configured udp syslog sink. */
916 send_to_syslog_fd(const char *s, size_t length)
917 OVS_REQ_RDLOCK(pattern_rwlock)
919 static size_t max_length = SIZE_MAX;
920 size_t send_len = MIN(length, max_length);
922 while (write(syslog_fd, s, send_len) < 0 && errno == EMSGSIZE) {
923 send_len -= send_len / 20;
924 max_length = send_len;
928 /* Writes 'message' to the log at the given 'level' and as coming from the
931 * Guaranteed to preserve errno. */
933 vlog_valist(const struct vlog_module *module, enum vlog_level level,
934 const char *message, va_list args)
936 bool log_to_console = module->levels[VLF_CONSOLE] >= level;
937 bool log_to_syslog = module->levels[VLF_SYSLOG] >= level;
940 ovs_mutex_lock(&log_file_mutex);
941 log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0;
942 ovs_mutex_unlock(&log_file_mutex);
943 if (log_to_console || log_to_syslog || log_to_file) {
944 int save_errno = errno;
950 ds_reserve(&s, 1024);
953 ovs_rwlock_rdlock(&pattern_rwlock);
954 if (log_to_console) {
955 format_log_message(module, level,
956 destinations[VLF_CONSOLE].pattern, message,
958 ds_put_char(&s, '\n');
959 fputs(ds_cstr(&s), stderr);
963 int syslog_level = syslog_levels[level];
964 char *save_ptr = NULL;
968 format_log_message(module, level, destinations[VLF_SYSLOG].pattern,
970 for (line = strtok_r(s.string, "\n", &save_ptr); line;
971 line = strtok_r(NULL, "\n", &save_ptr)) {
972 atomic_read_explicit(&log_facility, &facility,
973 memory_order_relaxed);
974 syslog(syslog_level|facility, "%s", line);
977 if (syslog_fd >= 0) {
978 format_log_message(module, level,
979 "<%B>1 %D{%Y-%m-%dT%H:%M:%S.###Z} "
980 "%E %A %P %c - \xef\xbb\xbf%m",
982 send_to_syslog_fd(ds_cstr(&s), s.length);
987 format_log_message(module, level, destinations[VLF_FILE].pattern,
989 ds_put_char(&s, '\n');
991 ovs_mutex_lock(&log_file_mutex);
994 async_append_write(log_writer, s.string, s.length);
995 if (level == VLL_EMER) {
996 async_append_flush(log_writer);
999 ignore(write(log_fd, s.string, s.length));
1002 ovs_mutex_unlock(&log_file_mutex);
1004 ovs_rwlock_unlock(&pattern_rwlock);
1012 vlog(const struct vlog_module *module, enum vlog_level level,
1013 const char *message, ...)
1017 va_start(args, message);
1018 vlog_valist(module, level, message, args);
1022 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
1023 * exit code. Always writes the message to stderr, even if the console
1024 * destination is disabled.
1026 * Choose this function instead of vlog_abort_valist() if the daemon monitoring
1027 * facility shouldn't automatically restart the current daemon. */
1029 vlog_fatal_valist(const struct vlog_module *module_,
1030 const char *message, va_list args)
1032 struct vlog_module *module = CONST_CAST(struct vlog_module *, module_);
1034 /* Don't log this message to the console to avoid redundancy with the
1035 * message written by the later ovs_fatal_valist(). */
1036 module->levels[VLF_CONSOLE] = VLL_OFF;
1038 vlog_valist(module, VLL_EMER, message, args);
1039 ovs_fatal_valist(0, message, args);
1042 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
1043 * exit code. Always writes the message to stderr, even if the console
1044 * destination is disabled.
1046 * Choose this function instead of vlog_abort() if the daemon monitoring
1047 * facility shouldn't automatically restart the current daemon. */
1049 vlog_fatal(const struct vlog_module *module, const char *message, ...)
1053 va_start(args, message);
1054 vlog_fatal_valist(module, message, args);
1058 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1059 * writes the message to stderr, even if the console destination is disabled.
1061 * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
1062 * facility should automatically restart the current daemon. */
1064 vlog_abort_valist(const struct vlog_module *module_,
1065 const char *message, va_list args)
1067 struct vlog_module *module = (struct vlog_module *) module_;
1069 /* Don't log this message to the console to avoid redundancy with the
1070 * message written by the later ovs_abort_valist(). */
1071 module->levels[VLF_CONSOLE] = VLL_OFF;
1073 vlog_valist(module, VLL_EMER, message, args);
1074 ovs_abort_valist(0, message, args);
1077 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1078 * writes the message to stderr, even if the console destination is disabled.
1080 * Choose this function instead of vlog_fatal() if the daemon monitoring
1081 * facility should automatically restart the current daemon. */
1083 vlog_abort(const struct vlog_module *module, const char *message, ...)
1087 va_start(args, message);
1088 vlog_abort_valist(module, message, args);
1093 vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
1094 struct vlog_rate_limit *rl)
1096 if (!module->honor_rate_limits) {
1100 if (!vlog_is_enabled(module, level)) {
1104 ovs_mutex_lock(&rl->mutex);
1105 if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) {
1106 time_t now = time_now();
1107 if (!rl->n_dropped) {
1108 rl->first_dropped = now;
1110 rl->last_dropped = now;
1112 ovs_mutex_unlock(&rl->mutex);
1116 if (!rl->n_dropped) {
1117 ovs_mutex_unlock(&rl->mutex);
1119 time_t now = time_now();
1120 unsigned int n_dropped = rl->n_dropped;
1121 unsigned int first_dropped_elapsed = now - rl->first_dropped;
1122 unsigned int last_dropped_elapsed = now - rl->last_dropped;
1124 ovs_mutex_unlock(&rl->mutex);
1127 "Dropped %u log messages in last %u seconds (most recently, "
1128 "%u seconds ago) due to excessive rate",
1129 n_dropped, first_dropped_elapsed, last_dropped_elapsed);
1136 vlog_rate_limit(const struct vlog_module *module, enum vlog_level level,
1137 struct vlog_rate_limit *rl, const char *message, ...)
1139 if (!vlog_should_drop(module, level, rl)) {
1142 va_start(args, message);
1143 vlog_valist(module, level, message, args);
1153 -vSPEC, --verbose=SPEC set logging levels\n\
1154 -v, --verbose set maximum verbosity level\n\
1155 --log-file[=FILE] enable logging to specified FILE\n\
1156 (default: %s/%s.log)\n\
1157 --syslog-target=HOST:PORT also send syslog msgs to HOST:PORT via UDP\n",
1158 ovs_logdir(), program_name);