2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 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 "openvswitch/dynamic-string.h"
35 #include "openvswitch/ofpbuf.h"
36 #include "ovs-thread.h"
38 #include "socket-util.h"
40 #include "syslog-direct.h"
41 #include "syslog-libc.h"
42 #include "syslog-provider.h"
47 VLOG_DEFINE_THIS_MODULE(vlog
);
49 /* ovs_assert() logs the assertion message, so using ovs_assert() in this
50 * source file could cause recursion. */
52 #define ovs_assert use_assert_instead_of_ovs_assert_in_this_module
54 /* Name for each logging level. */
55 static const char *const level_names
[VLL_N_LEVELS
] = {
56 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) #NAME,
61 /* Syslog value for each logging level. */
62 static const int syslog_levels
[VLL_N_LEVELS
] = {
63 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) SYSLOG_LEVEL,
68 /* RFC 5424 defines specific values for each syslog level. Normally LOG_* use
69 * the same values. Verify that in fact they're the same. If we get assertion
70 * failures here then we need to define a separate rfc5424_levels[] array. */
71 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) \
72 BUILD_ASSERT_DECL(SYSLOG_LEVEL == RFC5424);
76 /* Similarly, RFC 5424 defines the local0 facility with the value ordinarily
77 * used for LOG_LOCAL0. */
78 BUILD_ASSERT_DECL(LOG_LOCAL0
== (16 << 3));
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_ACQ_AFTER(pattern_rwlock
)
105 = OVS_MUTEX_INITIALIZER
;
106 static char *log_file_name
OVS_GUARDED_BY(log_file_mutex
) = NULL
;
107 static int log_fd
OVS_GUARDED_BY(log_file_mutex
) = -1;
108 static struct async_append
*log_writer
OVS_GUARDED_BY(log_file_mutex
);
109 static bool log_async
OVS_GUARDED_BY(log_file_mutex
);
110 static struct syslogger
*syslogger
= NULL
;
112 /* The log modules. */
113 static struct ovs_list vlog_modules
OVS_GUARDED_BY(log_file_mutex
)
114 = OVS_LIST_INITIALIZER(&vlog_modules
);
116 /* Syslog export configuration. */
117 static int syslog_fd
OVS_GUARDED_BY(pattern_rwlock
) = -1;
119 /* Log facility configuration. */
120 static atomic_int log_facility
= ATOMIC_VAR_INIT(0);
122 /* Facility name and its value. */
123 struct vlog_facility
{
124 char *name
; /* Name. */
125 unsigned int value
; /* Facility associated with 'name'. */
127 static struct vlog_facility vlog_facilities
[] = {
131 {"daemon", LOG_DAEMON
},
133 {"syslog", LOG_SYSLOG
},
143 {"local0", LOG_LOCAL0
},
144 {"local1", LOG_LOCAL1
},
145 {"local2", LOG_LOCAL2
},
146 {"local3", LOG_LOCAL3
},
147 {"local4", LOG_LOCAL4
},
148 {"local5", LOG_LOCAL5
},
149 {"local6", LOG_LOCAL6
},
150 {"local7", LOG_LOCAL7
}
152 static bool vlog_facility_exists(const char* facility
, int *value
);
154 static void format_log_message(const struct vlog_module
*, enum vlog_level
,
156 const char *message
, va_list, struct ds
*)
157 OVS_PRINTF_FORMAT(4, 0);
159 /* Searches the 'n_names' in 'names'. Returns the index of a match for
160 * 'target', or 'n_names' if no name matches. */
162 search_name_array(const char *target
, const char *const *names
, size_t n_names
)
166 for (i
= 0; i
< n_names
; i
++) {
168 if (!strcasecmp(names
[i
], target
)) {
175 /* Returns the name for logging level 'level'. */
177 vlog_get_level_name(enum vlog_level level
)
179 assert(level
< VLL_N_LEVELS
);
180 return level_names
[level
];
183 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
184 * is not the name of a logging level. */
186 vlog_get_level_val(const char *name
)
188 return search_name_array(name
, level_names
, ARRAY_SIZE(level_names
));
191 /* Returns the name for logging destination 'destination'. */
193 vlog_get_destination_name(enum vlog_destination destination
)
195 assert(destination
< VLF_N_DESTINATIONS
);
196 return destinations
[destination
].name
;
199 /* Returns the logging destination named 'name', or VLF_N_DESTINATIONS if
200 * 'name' is not the name of a logging destination. */
201 enum vlog_destination
202 vlog_get_destination_val(const char *name
)
206 for (i
= 0; i
< VLF_N_DESTINATIONS
; i
++) {
207 if (!strcasecmp(destinations
[i
].name
, name
)) {
215 vlog_insert_module(struct ovs_list
*vlog
)
217 ovs_mutex_lock(&log_file_mutex
);
218 ovs_list_insert(&vlog_modules
, vlog
);
219 ovs_mutex_unlock(&log_file_mutex
);
222 /* Returns the name for logging module 'module'. */
224 vlog_get_module_name(const struct vlog_module
*module
)
229 /* Returns the logging module named 'name', or NULL if 'name' is not the name
230 * of a logging module. */
232 vlog_module_from_name(const char *name
)
234 struct vlog_module
*mp
;
236 ovs_mutex_lock(&log_file_mutex
);
237 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
238 if (!strcasecmp(name
, mp
->name
)) {
239 ovs_mutex_unlock(&log_file_mutex
);
243 ovs_mutex_unlock(&log_file_mutex
);
248 /* Returns the current logging level for the given 'module' and
251 vlog_get_level(const struct vlog_module
*module
,
252 enum vlog_destination destination
)
254 assert(destination
< VLF_N_DESTINATIONS
);
255 return module
->levels
[destination
];
259 update_min_level(struct vlog_module
*module
) OVS_REQUIRES(&log_file_mutex
)
261 enum vlog_destination destination
;
263 module
->min_level
= VLL_OFF
;
264 for (destination
= 0; destination
< VLF_N_DESTINATIONS
; destination
++) {
265 if (log_fd
>= 0 || destination
!= VLF_FILE
) {
266 enum vlog_level level
= module
->levels
[destination
];
267 if (level
> module
->min_level
) {
268 module
->min_level
= level
;
275 set_destination_level(enum vlog_destination destination
,
276 struct vlog_module
*module
, enum vlog_level level
)
278 assert(destination
>= 0 && destination
< VLF_N_DESTINATIONS
);
279 assert(level
< VLL_N_LEVELS
);
281 ovs_mutex_lock(&log_file_mutex
);
283 struct vlog_module
*mp
;
284 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
285 mp
->levels
[destination
] = level
;
286 update_min_level(mp
);
289 module
->levels
[destination
] = level
;
290 update_min_level(module
);
292 ovs_mutex_unlock(&log_file_mutex
);
295 /* Sets the logging level for the given 'module' and 'destination' to 'level'.
296 * A null 'module' or a 'destination' of VLF_ANY_DESTINATION is treated as a
297 * wildcard across all modules or destinations, respectively. */
299 vlog_set_levels(struct vlog_module
*module
, enum vlog_destination destination
,
300 enum vlog_level level
)
302 assert(destination
< VLF_N_DESTINATIONS
||
303 destination
== VLF_ANY_DESTINATION
);
304 if (destination
== VLF_ANY_DESTINATION
) {
305 for (destination
= 0; destination
< VLF_N_DESTINATIONS
;
307 set_destination_level(destination
, module
, level
);
310 set_destination_level(destination
, module
, level
);
315 do_set_pattern(enum vlog_destination destination
, const char *pattern
)
317 struct destination
*f
= &destinations
[destination
];
319 ovs_rwlock_wrlock(&pattern_rwlock
);
320 if (!f
->default_pattern
) {
323 f
->default_pattern
= false;
325 f
->pattern
= xstrdup(pattern
);
326 ovs_rwlock_unlock(&pattern_rwlock
);
329 /* Sets the pattern for the given 'destination' to 'pattern'. */
331 vlog_set_pattern(enum vlog_destination destination
, const char *pattern
)
333 assert(destination
< VLF_N_DESTINATIONS
||
334 destination
== VLF_ANY_DESTINATION
);
335 if (destination
== VLF_ANY_DESTINATION
) {
336 for (destination
= 0; destination
< VLF_N_DESTINATIONS
;
338 do_set_pattern(destination
, pattern
);
341 do_set_pattern(destination
, pattern
);
345 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
346 * default file name if 'file_name' is null. Returns 0 if successful,
347 * otherwise a positive errno value. */
349 vlog_set_log_file(const char *file_name
)
351 char *new_log_file_name
;
352 struct vlog_module
*mp
;
353 struct stat old_stat
;
354 struct stat new_stat
;
359 /* Open new log file. */
360 new_log_file_name
= (file_name
362 : xasprintf("%s/%s.log", ovs_logdir(), program_name
));
363 new_log_fd
= open(new_log_file_name
, O_WRONLY
| O_CREAT
| O_APPEND
, 0666);
364 if (new_log_fd
< 0) {
365 VLOG_WARN("failed to open %s for logging: %s",
366 new_log_file_name
, ovs_strerror(errno
));
367 free(new_log_file_name
);
371 /* If the new log file is the same one we already have open, bail out. */
372 ovs_mutex_lock(&log_file_mutex
);
373 same_file
= (log_fd
>= 0
375 && !fstat(log_fd
, &old_stat
)
376 && !fstat(new_log_fd
, &new_stat
)
377 && old_stat
.st_dev
== new_stat
.st_dev
378 && old_stat
.st_ino
== new_stat
.st_ino
);
379 ovs_mutex_unlock(&log_file_mutex
);
382 free(new_log_file_name
);
386 /* Log closing old log file (we can't log while holding log_file_mutex). */
387 ovs_mutex_lock(&log_file_mutex
);
388 log_close
= log_fd
>= 0;
389 ovs_mutex_unlock(&log_file_mutex
);
391 VLOG_INFO("closing log file");
394 /* Close old log file, if any, and install new one. */
395 ovs_mutex_lock(&log_file_mutex
);
399 async_append_destroy(log_writer
);
402 log_file_name
= xstrdup(new_log_file_name
);
405 log_writer
= async_append_create(new_log_fd
);
408 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
409 update_min_level(mp
);
411 ovs_mutex_unlock(&log_file_mutex
);
413 /* Log opening new log file (we can't log while holding log_file_mutex). */
414 VLOG_INFO("opened log file %s", new_log_file_name
);
415 free(new_log_file_name
);
420 /* Closes and then attempts to re-open the current log file. (This is useful
421 * just after log rotation, to ensure that the new log file starts being used.)
422 * Returns 0 if successful, otherwise a positive errno value. */
424 vlog_reopen_log_file(void)
428 ovs_mutex_lock(&log_file_mutex
);
429 fn
= nullable_xstrdup(log_file_name
);
430 ovs_mutex_unlock(&log_file_mutex
);
433 int error
= vlog_set_log_file(fn
);
442 /* In case a log file exists, change its owner to new 'user' and 'group'.
444 * This is useful for handling cases where the --log-file option is
445 * specified ahead of the --user option. */
447 vlog_change_owner_unix(uid_t user
, gid_t group
)
449 struct ds err
= DS_EMPTY_INITIALIZER
;
452 ovs_mutex_lock(&log_file_mutex
);
453 error
= log_file_name
? chown(log_file_name
, user
, group
) : 0;
455 /* Build the error message. We can not call VLOG_FATAL directly
456 * here because VLOG_FATAL() will try again to to acquire
457 * 'log_file_mutex' lock, causing deadlock.
459 ds_put_format(&err
, "Failed to change %s ownership: %s.",
460 log_file_name
, ovs_strerror(errno
));
462 ovs_mutex_unlock(&log_file_mutex
);
465 VLOG_FATAL("%s", ds_steal_cstr(&err
));
470 /* Set debugging levels. Returns null if successful, otherwise an error
471 * message that the caller must free(). */
473 vlog_set_levels_from_string(const char *s_
)
475 char *s
= xstrdup(s_
);
476 char *save_ptr
= NULL
;
480 word
= strtok_r(s
, " ,:\t", &save_ptr
);
481 if (word
&& !strcasecmp(word
, "PATTERN")) {
482 enum vlog_destination destination
;
484 word
= strtok_r(NULL
, " ,:\t", &save_ptr
);
486 msg
= xstrdup("missing destination");
490 destination
= (!strcasecmp(word
, "ANY")
491 ? VLF_ANY_DESTINATION
492 : vlog_get_destination_val(word
));
493 if (destination
== VLF_N_DESTINATIONS
) {
494 msg
= xasprintf("unknown destination \"%s\"", word
);
497 vlog_set_pattern(destination
, save_ptr
);
498 } else if (word
&& !strcasecmp(word
, "FACILITY")) {
501 if (!vlog_facility_exists(save_ptr
, &value
)) {
502 msg
= xstrdup("invalid facility");
505 atomic_store_explicit(&log_facility
, value
, memory_order_relaxed
);
507 struct vlog_module
*module
= NULL
;
508 enum vlog_level level
= VLL_N_LEVELS
;
509 enum vlog_destination destination
= VLF_N_DESTINATIONS
;
511 for (; word
!= NULL
; word
= strtok_r(NULL
, " ,:\t", &save_ptr
)) {
512 if (!strcasecmp(word
, "ANY")) {
514 } else if (vlog_get_destination_val(word
) != VLF_N_DESTINATIONS
) {
515 if (destination
!= VLF_N_DESTINATIONS
) {
516 msg
= xstrdup("cannot specify multiple destinations");
519 destination
= vlog_get_destination_val(word
);
520 } else if (vlog_get_level_val(word
) != VLL_N_LEVELS
) {
521 if (level
!= VLL_N_LEVELS
) {
522 msg
= xstrdup("cannot specify multiple levels");
525 level
= vlog_get_level_val(word
);
526 } else if (vlog_module_from_name(word
)) {
528 msg
= xstrdup("cannot specify multiple modules");
531 module
= vlog_module_from_name(word
);
533 msg
= xasprintf("no destination, level, or module \"%s\"",
539 if (destination
== VLF_N_DESTINATIONS
) {
540 destination
= VLF_ANY_DESTINATION
;
542 if (level
== VLL_N_LEVELS
) {
545 vlog_set_levels(module
, destination
, level
);
553 /* Set debugging levels. Abort with an error message if 's' is invalid. */
555 vlog_set_levels_from_string_assert(const char *s
)
557 char *error
= vlog_set_levels_from_string(s
);
559 ovs_fatal(0, "%s", error
);
563 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
564 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
566 vlog_set_verbosity(const char *arg
)
569 char *msg
= vlog_set_levels_from_string(arg
);
571 ovs_fatal(0, "processing \"%s\": %s", arg
, msg
);
574 vlog_set_levels(NULL
, VLF_ANY_DESTINATION
, VLL_DBG
);
579 vlog_set_syslog_method(const char *method
)
582 /* Set syslogger only, if one is not already set. This effectively
583 * means that only the first --syslog-method argument is honored. */
587 if (!strcmp(method
, "libc")) {
588 syslogger
= syslog_libc_create();
589 } else if (!strncmp(method
, "udp:", 4) || !strncmp(method
, "unix:", 5)) {
590 syslogger
= syslog_direct_create(method
);
592 ovs_fatal(0, "unsupported syslog method '%s'", method
);
596 /* Set the vlog udp syslog target. */
598 vlog_set_syslog_target(const char *target
)
602 inet_open_active(SOCK_DGRAM
, target
, 0, NULL
, &new_fd
, 0);
604 ovs_rwlock_wrlock(&pattern_rwlock
);
605 if (syslog_fd
>= 0) {
609 ovs_rwlock_unlock(&pattern_rwlock
);
612 /* Returns 'false' if 'facility' is not a valid string. If 'facility'
613 * is a valid string, sets 'value' with the integer value of 'facility'
614 * and returns 'true'. */
616 vlog_facility_exists(const char* facility
, int *value
)
619 for (i
= 0; i
< ARRAY_SIZE(vlog_facilities
); i
++) {
620 if (!strcasecmp(vlog_facilities
[i
].name
, facility
)) {
621 *value
= vlog_facilities
[i
].value
;
629 vlog_unixctl_set(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
630 void *aux OVS_UNUSED
)
634 for (i
= 1; i
< argc
; i
++) {
635 char *msg
= vlog_set_levels_from_string(argv
[i
]);
637 unixctl_command_reply_error(conn
, msg
);
642 unixctl_command_reply(conn
, NULL
);
646 vlog_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
647 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
649 char *msg
= vlog_get_levels();
650 unixctl_command_reply(conn
, msg
);
655 vlog_unixctl_list_pattern(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
656 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
660 msg
= vlog_get_patterns();
661 unixctl_command_reply(conn
, msg
);
666 vlog_unixctl_reopen(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
667 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
671 ovs_mutex_lock(&log_file_mutex
);
672 has_log_file
= log_file_name
!= NULL
;
673 ovs_mutex_unlock(&log_file_mutex
);
676 int error
= vlog_reopen_log_file();
678 unixctl_command_reply_error(conn
, ovs_strerror(errno
));
680 unixctl_command_reply(conn
, NULL
);
683 unixctl_command_reply_error(conn
, "Logging to file not configured");
688 vlog_unixctl_close(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
689 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
691 ovs_mutex_lock(&log_file_mutex
);
696 async_append_destroy(log_writer
);
699 struct vlog_module
*mp
;
700 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
701 update_min_level(mp
);
704 ovs_mutex_unlock(&log_file_mutex
);
706 unixctl_command_reply(conn
, NULL
);
710 set_all_rate_limits(bool enable
)
712 struct vlog_module
*mp
;
714 ovs_mutex_lock(&log_file_mutex
);
715 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
716 mp
->honor_rate_limits
= enable
;
718 ovs_mutex_unlock(&log_file_mutex
);
722 set_rate_limits(struct unixctl_conn
*conn
, int argc
,
723 const char *argv
[], bool enable
)
728 for (i
= 1; i
< argc
; i
++) {
729 if (!strcasecmp(argv
[i
], "ANY")) {
730 set_all_rate_limits(enable
);
732 struct vlog_module
*module
= vlog_module_from_name(argv
[i
]);
734 unixctl_command_reply_error(conn
, "unknown module");
737 module
->honor_rate_limits
= enable
;
741 set_all_rate_limits(enable
);
743 unixctl_command_reply(conn
, NULL
);
747 vlog_enable_rate_limit(struct unixctl_conn
*conn
, int argc
,
748 const char *argv
[], void *aux OVS_UNUSED
)
750 set_rate_limits(conn
, argc
, argv
, true);
754 vlog_disable_rate_limit(struct unixctl_conn
*conn
, int argc
,
755 const char *argv
[], void *aux OVS_UNUSED
)
757 set_rate_limits(conn
, argc
, argv
, false);
760 /* Initializes the logging subsystem and registers its unixctl server
765 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
767 if (ovsthread_once_start(&once
)) {
770 bool print_syslog_target_deprecation
;
772 /* Do initialization work that needs to be done before any logging
773 * occurs. We want to keep this really minimal because any attempt to
774 * log anything before calling ovsthread_once_done() will deadlock. */
775 atomic_read_explicit(&log_facility
, &facility
, memory_order_relaxed
);
777 syslogger
= syslog_libc_create();
779 syslogger
->class->openlog(syslogger
, facility
? facility
: LOG_DAEMON
);
780 ovsthread_once_done(&once
);
782 /* Now do anything that we want to happen only once but doesn't have to
783 * finish before we start logging. */
785 now
= time_wall_msec();
787 char *s
= xastrftime_msec("%a, %d %b %Y %H:%M:%S", now
, true);
788 VLOG_ERR("current time is negative: %s (%lld)", s
, now
);
792 unixctl_command_register(
793 "vlog/set", "{spec | PATTERN:destination:pattern}",
794 1, INT_MAX
, vlog_unixctl_set
, NULL
);
795 unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list
,
797 unixctl_command_register("vlog/list-pattern", "", 0, 0,
798 vlog_unixctl_list_pattern
, NULL
);
799 unixctl_command_register("vlog/enable-rate-limit", "[module]...",
800 0, INT_MAX
, vlog_enable_rate_limit
, NULL
);
801 unixctl_command_register("vlog/disable-rate-limit", "[module]...",
802 0, INT_MAX
, vlog_disable_rate_limit
, NULL
);
803 unixctl_command_register("vlog/reopen", "", 0, 0,
804 vlog_unixctl_reopen
, NULL
);
805 unixctl_command_register("vlog/close", "", 0, 0,
806 vlog_unixctl_close
, NULL
);
808 ovs_rwlock_rdlock(&pattern_rwlock
);
809 print_syslog_target_deprecation
= syslog_fd
>= 0;
810 ovs_rwlock_unlock(&pattern_rwlock
);
812 if (print_syslog_target_deprecation
) {
813 VLOG_WARN("--syslog-target flag is deprecated, use "
814 "--syslog-method instead");
819 /* Enables VLF_FILE log output to be written asynchronously to disk.
820 * Asynchronous file writes avoid blocking the process in the case of a busy
821 * disk, but on the other hand they are less robust: there is a chance that the
822 * write will not make it to the log file if the process crashes soon after the
825 vlog_enable_async(void)
827 ovs_mutex_lock(&log_file_mutex
);
829 if (log_fd
>= 0 && !log_writer
) {
830 log_writer
= async_append_create(log_fd
);
832 ovs_mutex_unlock(&log_file_mutex
);
835 /* Print the current logging level for each module. */
837 vlog_get_levels(void)
839 struct ds s
= DS_EMPTY_INITIALIZER
;
840 struct vlog_module
*mp
;
841 struct svec lines
= SVEC_EMPTY_INITIALIZER
;
845 ds_put_format(&s
, " console syslog file\n");
846 ds_put_format(&s
, " ------- ------ ------\n");
848 ovs_mutex_lock(&log_file_mutex
);
849 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
853 ds_put_format(&line
, "%-16s %4s %4s %4s",
854 vlog_get_module_name(mp
),
855 vlog_get_level_name(vlog_get_level(mp
, VLF_CONSOLE
)),
856 vlog_get_level_name(vlog_get_level(mp
, VLF_SYSLOG
)),
857 vlog_get_level_name(vlog_get_level(mp
, VLF_FILE
)));
858 if (!mp
->honor_rate_limits
) {
859 ds_put_cstr(&line
, " (rate limiting disabled)");
861 ds_put_char(&line
, '\n');
863 svec_add_nocopy(&lines
, ds_steal_cstr(&line
));
865 ovs_mutex_unlock(&log_file_mutex
);
868 SVEC_FOR_EACH (i
, line
, &lines
) {
869 ds_put_cstr(&s
, line
);
871 svec_destroy(&lines
);
876 /* Returns as a string current logging patterns for each destination.
877 * This string must be released by caller. */
879 vlog_get_patterns(void)
881 struct ds ds
= DS_EMPTY_INITIALIZER
;
882 enum vlog_destination destination
;
884 ovs_rwlock_rdlock(&pattern_rwlock
);
885 ds_put_format(&ds
, " prefix format\n");
886 ds_put_format(&ds
, " ------ ------\n");
888 for (destination
= 0; destination
< VLF_N_DESTINATIONS
; destination
++) {
889 struct destination
*f
= &destinations
[destination
];
890 const char *prefix
= "none";
892 if (destination
== VLF_SYSLOG
&& syslogger
) {
893 prefix
= syslog_get_prefix(syslogger
);
895 ds_put_format(&ds
, "%-7s %-32s %s\n", f
->name
, prefix
, f
->pattern
);
897 ovs_rwlock_unlock(&pattern_rwlock
);
902 /* Returns true if a log message emitted for the given 'module' and 'level'
903 * would cause some log output, false if that module and level are completely
906 vlog_is_enabled(const struct vlog_module
*module
, enum vlog_level level
)
908 return module
->min_level
>= level
;
912 fetch_braces(const char *p
, const char *def
, char *out
, size_t out_size
)
915 size_t n
= strcspn(p
+ 1, "}");
916 size_t n_copy
= MIN(n
, out_size
- 1);
917 memcpy(out
, p
+ 1, n_copy
);
921 ovs_strlcpy(out
, def
, out_size
);
927 format_log_message(const struct vlog_module
*module
, enum vlog_level level
,
928 const char *pattern
, const char *message
,
929 va_list args_
, struct ds
*s
)
937 for (p
= pattern
; *p
!= '\0'; ) {
938 const char *subprogram_name
;
939 enum { LEFT
, RIGHT
} justify
= RIGHT
;
941 size_t length
, field
, used
;
944 ds_put_char(s
, *p
++);
958 while (isdigit((unsigned char)*p
)) {
959 field
= (field
* 10) + (*p
- '0');
966 ds_put_cstr(s
, program_name
);
969 atomic_read_explicit(&log_facility
, &facility
,
970 memory_order_relaxed
);
971 facility
= facility
? facility
: LOG_LOCAL0
;
972 ds_put_format(s
, "%d", facility
+ syslog_levels
[level
]);
975 p
= fetch_braces(p
, "", tmp
, sizeof tmp
);
976 ds_put_cstr(s
, vlog_get_module_name(module
));
979 p
= fetch_braces(p
, "%Y-%m-%d %H:%M:%S.###", tmp
, sizeof tmp
);
980 ds_put_strftime_msec(s
, tmp
, time_wall_msec(), false);
983 p
= fetch_braces(p
, "%Y-%m-%d %H:%M:%S.###", tmp
, sizeof tmp
);
984 ds_put_strftime_msec(s
, tmp
, time_wall_msec(), true);
987 gethostname(tmp
, sizeof tmp
);
988 tmp
[sizeof tmp
- 1] = '\0';
992 /* Format user-supplied log message and trim trailing new-lines. */
994 va_copy(args
, args_
);
995 ds_put_format_valist(s
, message
, args
);
997 while (s
->length
> length
&& s
->string
[s
->length
- 1] == '\n') {
1002 ds_put_format(s
, "%u", *msg_num_get_unsafe());
1005 ds_put_char(s
, '\n');
1008 ds_put_cstr(s
, vlog_get_level_name(level
));
1011 ds_put_format(s
, "%ld", (long int) getpid());
1014 ds_put_format(s
, "%lld", time_msec() - time_boot_msec());
1017 subprogram_name
= get_subprogram_name();
1018 ds_put_cstr(s
, subprogram_name
[0] ? subprogram_name
: "main");
1021 subprogram_name
= get_subprogram_name();
1022 if (subprogram_name
[0]) {
1023 ds_put_format(s
, "(%s)", subprogram_name
);
1027 ds_put_char(s
, p
[-1]);
1030 used
= s
->length
- length
;
1032 size_t n_pad
= field
- used
;
1033 if (justify
== RIGHT
) {
1034 ds_put_uninit(s
, n_pad
);
1035 memmove(&s
->string
[length
+ n_pad
], &s
->string
[length
], used
);
1036 memset(&s
->string
[length
], pad
, n_pad
);
1038 ds_put_char_multiple(s
, pad
, n_pad
);
1044 /* Exports the given 'syslog_message' to the configured udp syslog sink. */
1046 send_to_syslog_fd(const char *s
, size_t length
)
1047 OVS_REQ_RDLOCK(pattern_rwlock
)
1049 static size_t max_length
= SIZE_MAX
;
1050 size_t send_len
= MIN(length
, max_length
);
1052 while (write(syslog_fd
, s
, send_len
) < 0 && errno
== EMSGSIZE
) {
1053 send_len
-= send_len
/ 20;
1054 max_length
= send_len
;
1058 /* Writes 'message' to the log at the given 'level' and as coming from the
1061 * Guaranteed to preserve errno. */
1063 vlog_valist(const struct vlog_module
*module
, enum vlog_level level
,
1064 const char *message
, va_list args
)
1066 bool log_to_console
= module
->levels
[VLF_CONSOLE
] >= level
;
1067 bool log_to_syslog
= module
->levels
[VLF_SYSLOG
] >= level
;
1070 ovs_mutex_lock(&log_file_mutex
);
1071 log_to_file
= module
->levels
[VLF_FILE
] >= level
&& log_fd
>= 0;
1072 ovs_mutex_unlock(&log_file_mutex
);
1073 if (log_to_console
|| log_to_syslog
|| log_to_file
) {
1074 int save_errno
= errno
;
1080 ds_reserve(&s
, 1024);
1083 ovs_rwlock_rdlock(&pattern_rwlock
);
1084 if (log_to_console
) {
1085 format_log_message(module
, level
,
1086 destinations
[VLF_CONSOLE
].pattern
, message
,
1088 ds_put_char(&s
, '\n');
1089 fputs(ds_cstr(&s
), stderr
);
1092 if (log_to_syslog
) {
1093 int syslog_level
= syslog_levels
[level
];
1094 char *save_ptr
= NULL
;
1098 format_log_message(module
, level
, destinations
[VLF_SYSLOG
].pattern
,
1100 for (line
= strtok_r(s
.string
, "\n", &save_ptr
); line
;
1101 line
= strtok_r(NULL
, "\n", &save_ptr
)) {
1102 atomic_read_explicit(&log_facility
, &facility
,
1103 memory_order_relaxed
);
1104 syslogger
->class->syslog(syslogger
, syslog_level
|facility
, line
);
1107 if (syslog_fd
>= 0) {
1108 format_log_message(module
, level
,
1109 "<%B>1 %D{%Y-%m-%dT%H:%M:%S.###Z} "
1110 "%E %A %P %c - \xef\xbb\xbf%m",
1112 send_to_syslog_fd(ds_cstr(&s
), s
.length
);
1117 format_log_message(module
, level
, destinations
[VLF_FILE
].pattern
,
1119 ds_put_char(&s
, '\n');
1121 ovs_mutex_lock(&log_file_mutex
);
1124 async_append_write(log_writer
, s
.string
, s
.length
);
1125 if (level
== VLL_EMER
) {
1126 async_append_flush(log_writer
);
1129 ignore(write(log_fd
, s
.string
, s
.length
));
1132 ovs_mutex_unlock(&log_file_mutex
);
1134 ovs_rwlock_unlock(&pattern_rwlock
);
1142 vlog(const struct vlog_module
*module
, enum vlog_level level
,
1143 const char *message
, ...)
1147 va_start(args
, message
);
1148 vlog_valist(module
, level
, message
, args
);
1152 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
1153 * exit code. Always writes the message to stderr, even if the console
1154 * destination is disabled.
1156 * Choose this function instead of vlog_abort_valist() if the daemon monitoring
1157 * facility shouldn't automatically restart the current daemon. */
1159 vlog_fatal_valist(const struct vlog_module
*module_
,
1160 const char *message
, va_list args
)
1162 struct vlog_module
*module
= CONST_CAST(struct vlog_module
*, module_
);
1164 /* Don't log this message to the console to avoid redundancy with the
1165 * message written by the later ovs_fatal_valist(). */
1166 module
->levels
[VLF_CONSOLE
] = VLL_OFF
;
1168 vlog_valist(module
, VLL_EMER
, message
, args
);
1169 ovs_fatal_valist(0, message
, args
);
1172 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
1173 * exit code. Always writes the message to stderr, even if the console
1174 * destination is disabled.
1176 * Choose this function instead of vlog_abort() if the daemon monitoring
1177 * facility shouldn't automatically restart the current daemon. */
1179 vlog_fatal(const struct vlog_module
*module
, const char *message
, ...)
1183 va_start(args
, message
);
1184 vlog_fatal_valist(module
, message
, args
);
1188 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1189 * writes the message to stderr, even if the console destination is disabled.
1191 * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
1192 * facility should automatically restart the current daemon. */
1194 vlog_abort_valist(const struct vlog_module
*module_
,
1195 const char *message
, va_list args
)
1197 struct vlog_module
*module
= (struct vlog_module
*) module_
;
1199 /* Don't log this message to the console to avoid redundancy with the
1200 * message written by the later ovs_abort_valist(). */
1201 module
->levels
[VLF_CONSOLE
] = VLL_OFF
;
1203 vlog_valist(module
, VLL_EMER
, message
, args
);
1204 ovs_abort_valist(0, message
, args
);
1207 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1208 * writes the message to stderr, even if the console destination is disabled.
1210 * Choose this function instead of vlog_fatal() if the daemon monitoring
1211 * facility should automatically restart the current daemon. */
1213 vlog_abort(const struct vlog_module
*module
, const char *message
, ...)
1217 va_start(args
, message
);
1218 vlog_abort_valist(module
, message
, args
);
1223 vlog_should_drop(const struct vlog_module
*module
, enum vlog_level level
,
1224 struct vlog_rate_limit
*rl
)
1226 if (!module
->honor_rate_limits
) {
1230 if (!vlog_is_enabled(module
, level
)) {
1234 ovs_mutex_lock(&rl
->mutex
);
1235 if (!token_bucket_withdraw(&rl
->token_bucket
, VLOG_MSG_TOKENS
)) {
1236 time_t now
= time_now();
1237 if (!rl
->n_dropped
) {
1238 rl
->first_dropped
= now
;
1240 rl
->last_dropped
= now
;
1242 ovs_mutex_unlock(&rl
->mutex
);
1246 if (!rl
->n_dropped
) {
1247 ovs_mutex_unlock(&rl
->mutex
);
1249 time_t now
= time_now();
1250 unsigned int n_dropped
= rl
->n_dropped
;
1251 unsigned int first_dropped_elapsed
= now
- rl
->first_dropped
;
1252 unsigned int last_dropped_elapsed
= now
- rl
->last_dropped
;
1254 ovs_mutex_unlock(&rl
->mutex
);
1257 "Dropped %u log messages in last %u seconds (most recently, "
1258 "%u seconds ago) due to excessive rate",
1259 n_dropped
, first_dropped_elapsed
, last_dropped_elapsed
);
1266 vlog_rate_limit(const struct vlog_module
*module
, enum vlog_level level
,
1267 struct vlog_rate_limit
*rl
, const char *message
, ...)
1269 if (!vlog_should_drop(module
, level
, rl
)) {
1272 va_start(args
, message
);
1273 vlog_valist(module
, level
, message
, args
);
1283 -vSPEC, --verbose=SPEC set logging levels\n\
1284 -v, --verbose set maximum verbosity level\n\
1285 --log-file[=FILE] enable logging to specified FILE\n\
1286 (default: %s/%s.log)\n\
1287 --syslog-method=(libc|unix:file|udp:ip:port)\n\
1288 specify how to send messages to syslog daemon\n\
1289 --syslog-target=HOST:PORT also send syslog msgs to HOST:PORT via UDP\n",
1290 ovs_logdir(), program_name
);