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-null.h"
43 #include "syslog-provider.h"
48 VLOG_DEFINE_THIS_MODULE(vlog
);
50 /* ovs_assert() logs the assertion message, so using ovs_assert() in this
51 * source file could cause recursion. */
53 #define ovs_assert use_assert_instead_of_ovs_assert_in_this_module
55 /* Name for each logging level. */
56 static const char *const level_names
[VLL_N_LEVELS
] = {
57 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) #NAME,
62 /* Syslog value for each logging level. */
63 static const int syslog_levels
[VLL_N_LEVELS
] = {
64 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) SYSLOG_LEVEL,
69 /* RFC 5424 defines specific values for each syslog level. Normally LOG_* use
70 * the same values. Verify that in fact they're the same. If we get assertion
71 * failures here then we need to define a separate rfc5424_levels[] array. */
72 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) \
73 BUILD_ASSERT_DECL(SYSLOG_LEVEL == RFC5424);
77 /* Similarly, RFC 5424 defines the local0 facility with the value ordinarily
78 * used for LOG_LOCAL0. */
79 BUILD_ASSERT_DECL(LOG_LOCAL0
== (16 << 3));
81 /* Protects the 'pattern' in all "struct destination"s, so that a race between
82 * changing and reading the pattern does not cause an access to freed
84 static struct ovs_rwlock pattern_rwlock
= OVS_RWLOCK_INITIALIZER
;
86 /* Information about each destination. */
88 const char *name
; /* Name. */
89 char *pattern
OVS_GUARDED_BY(pattern_rwlock
); /* Current pattern. */
90 bool default_pattern
; /* Whether current pattern is the default. */
92 static struct destination destinations
[VLF_N_DESTINATIONS
] = {
93 #define VLOG_DESTINATION(NAME, PATTERN) {#NAME, PATTERN, true},
95 #undef VLOG_DESTINATION
98 /* Sequence number for the message currently being composed. */
99 DEFINE_STATIC_PER_THREAD_DATA(unsigned int, msg_num
, 0);
101 /* VLF_FILE configuration.
103 * All of the following is protected by 'log_file_mutex', which nests inside
105 static struct ovs_mutex log_file_mutex
OVS_ACQ_AFTER(pattern_rwlock
)
106 = OVS_MUTEX_INITIALIZER
;
107 static char *log_file_name
OVS_GUARDED_BY(log_file_mutex
) = NULL
;
108 static int log_fd
OVS_GUARDED_BY(log_file_mutex
) = -1;
109 static struct async_append
*log_writer
OVS_GUARDED_BY(log_file_mutex
);
110 static bool log_async
OVS_GUARDED_BY(log_file_mutex
);
111 static struct syslogger
*syslogger
= NULL
;
113 /* The log modules. */
114 static struct ovs_list vlog_modules
OVS_GUARDED_BY(log_file_mutex
)
115 = OVS_LIST_INITIALIZER(&vlog_modules
);
117 /* Syslog export configuration. */
118 static int syslog_fd
OVS_GUARDED_BY(pattern_rwlock
) = -1;
120 /* Log facility configuration. */
121 static atomic_int log_facility
= ATOMIC_VAR_INIT(0);
123 /* Facility name and its value. */
124 struct vlog_facility
{
125 char *name
; /* Name. */
126 unsigned int value
; /* Facility associated with 'name'. */
128 static struct vlog_facility vlog_facilities
[] = {
132 {"daemon", LOG_DAEMON
},
134 {"syslog", LOG_SYSLOG
},
144 {"local0", LOG_LOCAL0
},
145 {"local1", LOG_LOCAL1
},
146 {"local2", LOG_LOCAL2
},
147 {"local3", LOG_LOCAL3
},
148 {"local4", LOG_LOCAL4
},
149 {"local5", LOG_LOCAL5
},
150 {"local6", LOG_LOCAL6
},
151 {"local7", LOG_LOCAL7
}
153 static bool vlog_facility_exists(const char* facility
, int *value
);
155 static void format_log_message(const struct vlog_module
*, enum vlog_level
,
157 const char *message
, va_list, struct ds
*)
158 OVS_PRINTF_FORMAT(4, 0);
160 /* Searches the 'n_names' in 'names'. Returns the index of a match for
161 * 'target', or 'n_names' if no name matches. */
163 search_name_array(const char *target
, const char *const *names
, size_t n_names
)
167 for (i
= 0; i
< n_names
; i
++) {
169 if (!strcasecmp(names
[i
], target
)) {
176 /* Returns the name for logging level 'level'. */
178 vlog_get_level_name(enum vlog_level level
)
180 assert(level
< VLL_N_LEVELS
);
181 return level_names
[level
];
184 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
185 * is not the name of a logging level. */
187 vlog_get_level_val(const char *name
)
189 return search_name_array(name
, level_names
, ARRAY_SIZE(level_names
));
192 /* Returns the name for logging destination 'destination'. */
194 vlog_get_destination_name(enum vlog_destination destination
)
196 assert(destination
< VLF_N_DESTINATIONS
);
197 return destinations
[destination
].name
;
200 /* Returns the logging destination named 'name', or VLF_N_DESTINATIONS if
201 * 'name' is not the name of a logging destination. */
202 enum vlog_destination
203 vlog_get_destination_val(const char *name
)
207 for (i
= 0; i
< VLF_N_DESTINATIONS
; i
++) {
208 if (!strcasecmp(destinations
[i
].name
, name
)) {
216 vlog_insert_module(struct ovs_list
*vlog
)
218 ovs_mutex_lock(&log_file_mutex
);
219 ovs_list_insert(&vlog_modules
, vlog
);
220 ovs_mutex_unlock(&log_file_mutex
);
223 /* Returns the name for logging module 'module'. */
225 vlog_get_module_name(const struct vlog_module
*module
)
230 /* Returns the logging module named 'name', or NULL if 'name' is not the name
231 * of a logging module. */
233 vlog_module_from_name(const char *name
)
235 struct vlog_module
*mp
;
237 ovs_mutex_lock(&log_file_mutex
);
238 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
239 if (!strcasecmp(name
, mp
->name
)) {
240 ovs_mutex_unlock(&log_file_mutex
);
244 ovs_mutex_unlock(&log_file_mutex
);
249 /* Returns the current logging level for the given 'module' and
252 vlog_get_level(const struct vlog_module
*module
,
253 enum vlog_destination destination
)
255 assert(destination
< VLF_N_DESTINATIONS
);
256 return module
->levels
[destination
];
260 update_min_level(struct vlog_module
*module
) OVS_REQUIRES(log_file_mutex
)
262 enum vlog_destination destination
;
264 module
->min_level
= VLL_OFF
;
265 for (destination
= 0; destination
< VLF_N_DESTINATIONS
; destination
++) {
266 if (log_fd
>= 0 || destination
!= VLF_FILE
) {
267 enum vlog_level level
= module
->levels
[destination
];
268 if (level
> module
->min_level
) {
269 module
->min_level
= level
;
276 set_destination_level(enum vlog_destination destination
,
277 struct vlog_module
*module
, enum vlog_level level
)
279 assert(destination
>= 0 && destination
< VLF_N_DESTINATIONS
);
280 assert(level
< VLL_N_LEVELS
);
282 ovs_mutex_lock(&log_file_mutex
);
284 struct vlog_module
*mp
;
285 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
286 mp
->levels
[destination
] = level
;
287 update_min_level(mp
);
290 module
->levels
[destination
] = level
;
291 update_min_level(module
);
293 ovs_mutex_unlock(&log_file_mutex
);
296 /* Sets the logging level for the given 'module' and 'destination' to 'level'.
297 * A null 'module' or a 'destination' of VLF_ANY_DESTINATION is treated as a
298 * wildcard across all modules or destinations, respectively. */
300 vlog_set_levels(struct vlog_module
*module
, enum vlog_destination destination
,
301 enum vlog_level level
)
303 assert(destination
< VLF_N_DESTINATIONS
||
304 destination
== VLF_ANY_DESTINATION
);
305 if (destination
== VLF_ANY_DESTINATION
) {
306 for (destination
= 0; destination
< VLF_N_DESTINATIONS
;
308 set_destination_level(destination
, module
, level
);
311 set_destination_level(destination
, module
, level
);
316 do_set_pattern(enum vlog_destination destination
, const char *pattern
)
318 struct destination
*f
= &destinations
[destination
];
320 ovs_rwlock_wrlock(&pattern_rwlock
);
321 if (!f
->default_pattern
) {
324 f
->default_pattern
= false;
326 f
->pattern
= xstrdup(pattern
);
327 ovs_rwlock_unlock(&pattern_rwlock
);
330 /* Sets the pattern for the given 'destination' to 'pattern'. */
332 vlog_set_pattern(enum vlog_destination destination
, const char *pattern
)
334 assert(destination
< VLF_N_DESTINATIONS
||
335 destination
== VLF_ANY_DESTINATION
);
336 if (destination
== VLF_ANY_DESTINATION
) {
337 for (destination
= 0; destination
< VLF_N_DESTINATIONS
;
339 do_set_pattern(destination
, pattern
);
342 do_set_pattern(destination
, pattern
);
346 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
347 * default file name if 'file_name' is null. Returns 0 if successful,
348 * otherwise a positive errno value. */
350 vlog_set_log_file(const char *file_name
)
352 char *new_log_file_name
;
353 struct vlog_module
*mp
;
354 struct stat old_stat
;
355 struct stat new_stat
;
360 /* Open new log file. */
361 new_log_file_name
= (file_name
363 : xasprintf("%s/%s.log", ovs_logdir(), program_name
));
364 new_log_fd
= open(new_log_file_name
, O_WRONLY
| O_CREAT
| O_APPEND
, 0660);
365 if (new_log_fd
< 0) {
366 VLOG_WARN("failed to open %s for logging: %s",
367 new_log_file_name
, ovs_strerror(errno
));
368 free(new_log_file_name
);
372 /* If the new log file is the same one we already have open, bail out. */
373 ovs_mutex_lock(&log_file_mutex
);
374 same_file
= (log_fd
>= 0
376 && !fstat(log_fd
, &old_stat
)
377 && !fstat(new_log_fd
, &new_stat
)
378 && old_stat
.st_dev
== new_stat
.st_dev
379 && old_stat
.st_ino
== new_stat
.st_ino
);
380 ovs_mutex_unlock(&log_file_mutex
);
383 free(new_log_file_name
);
387 /* Log closing old log file (we can't log while holding log_file_mutex). */
388 ovs_mutex_lock(&log_file_mutex
);
389 log_close
= log_fd
>= 0;
390 ovs_mutex_unlock(&log_file_mutex
);
392 VLOG_INFO("closing log file");
395 /* Close old log file, if any, and install new one. */
396 ovs_mutex_lock(&log_file_mutex
);
399 async_append_destroy(log_writer
);
403 log_file_name
= xstrdup(new_log_file_name
);
406 log_writer
= async_append_create(new_log_fd
);
409 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
410 update_min_level(mp
);
412 ovs_mutex_unlock(&log_file_mutex
);
414 /* Log opening new log file (we can't log while holding log_file_mutex). */
415 VLOG_INFO("opened log file %s", new_log_file_name
);
416 free(new_log_file_name
);
421 /* Closes and then attempts to re-open the current log file. (This is useful
422 * just after log rotation, to ensure that the new log file starts being used.)
423 * Returns 0 if successful, otherwise a positive errno value. */
425 vlog_reopen_log_file(void)
429 ovs_mutex_lock(&log_file_mutex
);
430 fn
= nullable_xstrdup(log_file_name
);
431 ovs_mutex_unlock(&log_file_mutex
);
434 int error
= vlog_set_log_file(fn
);
443 /* In case a log file exists, change its owner to new 'user' and 'group'.
445 * This is useful for handling cases where the --log-file option is
446 * specified ahead of the --user option. */
448 vlog_change_owner_unix(uid_t user
, gid_t group
)
450 struct ds err
= DS_EMPTY_INITIALIZER
;
453 ovs_mutex_lock(&log_file_mutex
);
454 error
= log_file_name
? chown(log_file_name
, user
, group
) : 0;
456 /* Build the error message. We can not call VLOG_FATAL directly
457 * here because VLOG_FATAL() will try again to to acquire
458 * 'log_file_mutex' lock, causing deadlock.
460 ds_put_format(&err
, "Failed to change %s ownership: %s.",
461 log_file_name
, ovs_strerror(errno
));
463 ovs_mutex_unlock(&log_file_mutex
);
466 VLOG_FATAL("%s", ds_steal_cstr(&err
));
471 /* Set debugging levels. Returns null if successful, otherwise an error
472 * message that the caller must free(). */
474 vlog_set_levels_from_string(const char *s_
)
476 char *s
= xstrdup(s_
);
477 char *save_ptr
= NULL
;
481 word
= strtok_r(s
, " ,:\t", &save_ptr
);
482 if (word
&& !strcasecmp(word
, "PATTERN")) {
483 enum vlog_destination destination
;
485 word
= strtok_r(NULL
, " ,:\t", &save_ptr
);
487 msg
= xstrdup("missing destination");
491 destination
= (!strcasecmp(word
, "ANY")
492 ? VLF_ANY_DESTINATION
493 : vlog_get_destination_val(word
));
494 if (destination
== VLF_N_DESTINATIONS
) {
495 msg
= xasprintf("unknown destination \"%s\"", word
);
498 vlog_set_pattern(destination
, save_ptr
);
499 } else if (word
&& !strcasecmp(word
, "FACILITY")) {
502 if (!vlog_facility_exists(save_ptr
, &value
)) {
503 msg
= xstrdup("invalid facility");
506 atomic_store_explicit(&log_facility
, value
, memory_order_relaxed
);
508 struct vlog_module
*module
= NULL
;
509 enum vlog_level level
= VLL_N_LEVELS
;
510 enum vlog_destination destination
= VLF_N_DESTINATIONS
;
512 for (; word
!= NULL
; word
= strtok_r(NULL
, " ,:\t", &save_ptr
)) {
513 if (!strcasecmp(word
, "ANY")) {
515 } else if (vlog_get_destination_val(word
) != VLF_N_DESTINATIONS
) {
516 if (destination
!= VLF_N_DESTINATIONS
) {
517 msg
= xstrdup("cannot specify multiple destinations");
520 destination
= vlog_get_destination_val(word
);
521 } else if (vlog_get_level_val(word
) != VLL_N_LEVELS
) {
522 if (level
!= VLL_N_LEVELS
) {
523 msg
= xstrdup("cannot specify multiple levels");
526 level
= vlog_get_level_val(word
);
527 } else if (vlog_module_from_name(word
)) {
529 msg
= xstrdup("cannot specify multiple modules");
532 module
= vlog_module_from_name(word
);
534 msg
= xasprintf("no destination, level, or module \"%s\"",
540 if (destination
== VLF_N_DESTINATIONS
) {
541 destination
= VLF_ANY_DESTINATION
;
543 if (level
== VLL_N_LEVELS
) {
546 vlog_set_levels(module
, destination
, level
);
554 /* Set debugging levels. Abort with an error message if 's' is invalid. */
556 vlog_set_levels_from_string_assert(const char *s
)
558 char *error
= vlog_set_levels_from_string(s
);
560 ovs_fatal(0, "%s", error
);
564 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
565 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
567 vlog_set_verbosity(const char *arg
)
570 char *msg
= vlog_set_levels_from_string(arg
);
572 ovs_fatal(0, "processing \"%s\": %s", arg
, msg
);
575 vlog_set_levels(NULL
, VLF_ANY_DESTINATION
, VLL_DBG
);
580 vlog_set_syslog_method(const char *method
)
583 /* Set syslogger only, if one is not already set. This effectively
584 * means that only the first --syslog-method argument is honored. */
588 if (!strcmp(method
, "null")) {
589 syslogger
= syslog_null_create();
590 } else if (!strcmp(method
, "libc")) {
591 syslogger
= syslog_libc_create();
592 } else if (!strncmp(method
, "udp:", 4) || !strncmp(method
, "unix:", 5)) {
593 syslogger
= syslog_direct_create(method
);
595 ovs_fatal(0, "unsupported syslog method '%s'", method
);
599 /* Set the vlog udp syslog target. */
601 vlog_set_syslog_target(const char *target
)
605 inet_open_active(SOCK_DGRAM
, target
, -1, NULL
, &new_fd
, 0);
607 ovs_rwlock_wrlock(&pattern_rwlock
);
608 if (syslog_fd
>= 0) {
612 ovs_rwlock_unlock(&pattern_rwlock
);
616 * This function writes directly to log file without using async writer or
617 * taking a lock. Caller must hold 'log_file_mutex' or be sure that it's
618 * not necessary. Could be used in exceptional cases like dumping of backtrace
622 vlog_direct_write_to_log_file_unsafe(const char *s
)
623 OVS_NO_THREAD_SAFETY_ANALYSIS
626 ignore(write(log_fd
, s
, strlen(s
)));
630 /* Returns 'false' if 'facility' is not a valid string. If 'facility'
631 * is a valid string, sets 'value' with the integer value of 'facility'
632 * and returns 'true'. */
634 vlog_facility_exists(const char* facility
, int *value
)
637 for (i
= 0; i
< ARRAY_SIZE(vlog_facilities
); i
++) {
638 if (!strcasecmp(vlog_facilities
[i
].name
, facility
)) {
639 *value
= vlog_facilities
[i
].value
;
647 vlog_unixctl_set(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
648 void *aux OVS_UNUSED
)
652 /* With no argument, set all destinations and modules to "dbg". */
654 vlog_set_levels(NULL
, VLF_ANY_DESTINATION
, VLL_DBG
);
656 for (i
= 1; i
< argc
; i
++) {
657 char *msg
= vlog_set_levels_from_string(argv
[i
]);
659 unixctl_command_reply_error(conn
, msg
);
664 unixctl_command_reply(conn
, NULL
);
668 vlog_unixctl_list(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
669 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
671 char *msg
= vlog_get_levels();
672 unixctl_command_reply(conn
, msg
);
677 vlog_unixctl_list_pattern(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
678 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
682 msg
= vlog_get_patterns();
683 unixctl_command_reply(conn
, msg
);
688 vlog_unixctl_reopen(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
689 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
693 ovs_mutex_lock(&log_file_mutex
);
694 has_log_file
= log_file_name
!= NULL
;
695 ovs_mutex_unlock(&log_file_mutex
);
698 int error
= vlog_reopen_log_file();
700 unixctl_command_reply_error(conn
, ovs_strerror(errno
));
702 unixctl_command_reply(conn
, NULL
);
705 unixctl_command_reply_error(conn
, "Logging to file not configured");
710 vlog_unixctl_close(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
711 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
713 ovs_mutex_lock(&log_file_mutex
);
718 async_append_destroy(log_writer
);
721 struct vlog_module
*mp
;
722 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
723 update_min_level(mp
);
726 ovs_mutex_unlock(&log_file_mutex
);
728 unixctl_command_reply(conn
, NULL
);
732 set_all_rate_limits(bool enable
)
734 struct vlog_module
*mp
;
736 ovs_mutex_lock(&log_file_mutex
);
737 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
738 mp
->honor_rate_limits
= enable
;
740 ovs_mutex_unlock(&log_file_mutex
);
744 set_rate_limits(struct unixctl_conn
*conn
, int argc
,
745 const char *argv
[], bool enable
)
750 for (i
= 1; i
< argc
; i
++) {
751 if (!strcasecmp(argv
[i
], "ANY")) {
752 set_all_rate_limits(enable
);
754 struct vlog_module
*module
= vlog_module_from_name(argv
[i
]);
756 unixctl_command_reply_error(conn
, "unknown module");
759 module
->honor_rate_limits
= enable
;
763 set_all_rate_limits(enable
);
765 unixctl_command_reply(conn
, NULL
);
769 vlog_enable_rate_limit(struct unixctl_conn
*conn
, int argc
,
770 const char *argv
[], void *aux OVS_UNUSED
)
772 set_rate_limits(conn
, argc
, argv
, true);
776 vlog_disable_rate_limit(struct unixctl_conn
*conn
, int argc
,
777 const char *argv
[], void *aux OVS_UNUSED
)
779 set_rate_limits(conn
, argc
, argv
, false);
782 /* Initializes the logging subsystem and registers its unixctl server
787 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
789 if (ovsthread_once_start(&once
)) {
792 bool print_syslog_target_deprecation
;
794 /* Do initialization work that needs to be done before any logging
795 * occurs. We want to keep this really minimal because any attempt to
796 * log anything before calling ovsthread_once_done() will deadlock. */
797 atomic_read_explicit(&log_facility
, &facility
, memory_order_relaxed
);
799 char *env
= getenv("OVS_SYSLOG_METHOD");
801 vlog_set_syslog_method(env
);
803 syslogger
= syslog_libc_create();
806 syslogger
->class->openlog(syslogger
, facility
? facility
: LOG_DAEMON
);
807 ovsthread_once_done(&once
);
809 /* Now do anything that we want to happen only once but doesn't have to
810 * finish before we start logging. */
812 now
= time_wall_msec();
814 char *s
= xastrftime_msec("%a, %d %b %Y %H:%M:%S", now
, true);
815 VLOG_ERR("current time is negative: %s (%lld)", s
, now
);
819 unixctl_command_register(
820 "vlog/set", "{spec | PATTERN:destination:pattern}",
821 0, INT_MAX
, vlog_unixctl_set
, NULL
);
822 unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list
,
824 unixctl_command_register("vlog/list-pattern", "", 0, 0,
825 vlog_unixctl_list_pattern
, NULL
);
826 unixctl_command_register("vlog/enable-rate-limit", "[module]...",
827 0, INT_MAX
, vlog_enable_rate_limit
, NULL
);
828 unixctl_command_register("vlog/disable-rate-limit", "[module]...",
829 0, INT_MAX
, vlog_disable_rate_limit
, NULL
);
830 unixctl_command_register("vlog/reopen", "", 0, 0,
831 vlog_unixctl_reopen
, NULL
);
832 unixctl_command_register("vlog/close", "", 0, 0,
833 vlog_unixctl_close
, NULL
);
835 ovs_rwlock_rdlock(&pattern_rwlock
);
836 print_syslog_target_deprecation
= syslog_fd
>= 0;
837 ovs_rwlock_unlock(&pattern_rwlock
);
839 if (print_syslog_target_deprecation
) {
840 VLOG_WARN("--syslog-target flag is deprecated, use "
841 "--syslog-method instead");
846 /* Enables VLF_FILE log output to be written asynchronously to disk.
847 * Asynchronous file writes avoid blocking the process in the case of a busy
848 * disk, but on the other hand they are less robust: there is a chance that the
849 * write will not make it to the log file if the process crashes soon after the
852 vlog_enable_async(void)
854 ovs_mutex_lock(&log_file_mutex
);
856 if (log_fd
>= 0 && !log_writer
) {
857 log_writer
= async_append_create(log_fd
);
859 ovs_mutex_unlock(&log_file_mutex
);
863 vlog_disable_async(void)
865 ovs_mutex_lock(&log_file_mutex
);
867 async_append_destroy(log_writer
);
869 ovs_mutex_unlock(&log_file_mutex
);
872 /* Print the current logging level for each module. */
874 vlog_get_levels(void)
876 struct ds s
= DS_EMPTY_INITIALIZER
;
877 struct vlog_module
*mp
;
878 struct svec lines
= SVEC_EMPTY_INITIALIZER
;
881 ds_put_format(&s
, " console syslog file\n");
882 ds_put_format(&s
, " ------- ------ ------\n");
884 ovs_mutex_lock(&log_file_mutex
);
885 LIST_FOR_EACH (mp
, list
, &vlog_modules
) {
889 ds_put_format(&line
, "%-16s %4s %4s %4s",
890 vlog_get_module_name(mp
),
891 vlog_get_level_name(vlog_get_level(mp
, VLF_CONSOLE
)),
892 vlog_get_level_name(vlog_get_level(mp
, VLF_SYSLOG
)),
893 vlog_get_level_name(vlog_get_level(mp
, VLF_FILE
)));
894 if (!mp
->honor_rate_limits
) {
895 ds_put_cstr(&line
, " (rate limiting disabled)");
897 ds_put_char(&line
, '\n');
899 svec_add_nocopy(&lines
, ds_steal_cstr(&line
));
901 ovs_mutex_unlock(&log_file_mutex
);
906 SVEC_FOR_EACH (i
, line
, &lines
) {
907 ds_put_cstr(&s
, line
);
909 svec_destroy(&lines
);
914 /* Returns as a string current logging patterns for each destination.
915 * This string must be released by caller. */
917 vlog_get_patterns(void)
919 struct ds ds
= DS_EMPTY_INITIALIZER
;
920 enum vlog_destination destination
;
922 ovs_rwlock_rdlock(&pattern_rwlock
);
923 ds_put_format(&ds
, " prefix format\n");
924 ds_put_format(&ds
, " ------ ------\n");
926 for (destination
= 0; destination
< VLF_N_DESTINATIONS
; destination
++) {
927 struct destination
*f
= &destinations
[destination
];
928 const char *prefix
= "none";
930 if (destination
== VLF_SYSLOG
&& syslogger
) {
931 prefix
= syslog_get_prefix(syslogger
);
933 ds_put_format(&ds
, "%-7s %-32s %s\n", f
->name
, prefix
, f
->pattern
);
935 ovs_rwlock_unlock(&pattern_rwlock
);
940 /* Returns true if a log message emitted for the given 'module' and 'level'
941 * would cause some log output, false if that module and level are completely
944 vlog_is_enabled(const struct vlog_module
*module
, enum vlog_level level
)
946 return module
->min_level
>= level
;
950 fetch_braces(const char *p
, const char *def
, char *out
, size_t out_size
)
953 size_t n
= strcspn(p
+ 1, "}");
954 size_t n_copy
= MIN(n
, out_size
- 1);
955 memcpy(out
, p
+ 1, n_copy
);
959 ovs_strlcpy(out
, def
, out_size
);
965 format_log_message(const struct vlog_module
*module
, enum vlog_level level
,
966 const char *pattern
, const char *message
,
967 va_list args_
, struct ds
*s
)
975 for (p
= pattern
; *p
!= '\0'; ) {
976 const char *subprogram_name
;
977 enum { LEFT
, RIGHT
} justify
= RIGHT
;
979 size_t length
, field
, used
;
982 ds_put_char(s
, *p
++);
996 while (isdigit((unsigned char)*p
)) {
997 field
= (field
* 10) + (*p
- '0');
1004 ds_put_cstr(s
, program_name
);
1007 atomic_read_explicit(&log_facility
, &facility
,
1008 memory_order_relaxed
);
1009 facility
= facility
? facility
: LOG_LOCAL0
;
1010 ds_put_format(s
, "%d", facility
+ syslog_levels
[level
]);
1013 p
= fetch_braces(p
, "", tmp
, sizeof tmp
);
1014 ds_put_cstr(s
, vlog_get_module_name(module
));
1017 p
= fetch_braces(p
, "%Y-%m-%d %H:%M:%S.###", tmp
, sizeof tmp
);
1018 ds_put_strftime_msec(s
, tmp
, time_wall_msec(), false);
1021 p
= fetch_braces(p
, "%Y-%m-%d %H:%M:%S.###", tmp
, sizeof tmp
);
1022 ds_put_strftime_msec(s
, tmp
, time_wall_msec(), true);
1025 gethostname(tmp
, sizeof tmp
);
1026 tmp
[sizeof tmp
- 1] = '\0';
1027 ds_put_cstr(s
, tmp
);
1030 /* Format user-supplied log message and trim trailing new-lines. */
1032 va_copy(args
, args_
);
1033 ds_put_format_valist(s
, message
, args
);
1035 while (s
->length
> length
&& s
->string
[s
->length
- 1] == '\n') {
1040 ds_put_format(s
, "%u", *msg_num_get_unsafe());
1043 ds_put_char(s
, '\n');
1046 ds_put_cstr(s
, vlog_get_level_name(level
));
1049 ds_put_format(s
, "%ld", (long int) getpid());
1052 ds_put_format(s
, "%lld", time_msec() - time_boot_msec());
1055 subprogram_name
= get_subprogram_name();
1056 ds_put_cstr(s
, subprogram_name
[0] ? subprogram_name
: "main");
1059 subprogram_name
= get_subprogram_name();
1060 if (subprogram_name
[0]) {
1061 ds_put_format(s
, "(%s)", subprogram_name
);
1065 ds_put_char(s
, p
[-1]);
1068 used
= s
->length
- length
;
1070 size_t n_pad
= field
- used
;
1071 if (justify
== RIGHT
) {
1072 ds_put_uninit(s
, n_pad
);
1073 memmove(&s
->string
[length
+ n_pad
], &s
->string
[length
], used
);
1074 memset(&s
->string
[length
], pad
, n_pad
);
1076 ds_put_char_multiple(s
, pad
, n_pad
);
1082 /* Exports the given 'syslog_message' to the configured udp syslog sink. */
1084 send_to_syslog_fd(const char *s
, size_t length
)
1085 OVS_REQ_RDLOCK(pattern_rwlock
)
1087 static size_t max_length
= SIZE_MAX
;
1088 size_t send_len
= MIN(length
, max_length
);
1090 while (write(syslog_fd
, s
, send_len
) < 0 && errno
== EMSGSIZE
) {
1091 send_len
-= send_len
/ 20;
1092 max_length
= send_len
;
1096 /* Writes 'message' to the log at the given 'level' and as coming from the
1099 * Guaranteed to preserve errno. */
1101 vlog_valist(const struct vlog_module
*module
, enum vlog_level level
,
1102 const char *message
, va_list args
)
1104 bool log_to_console
= module
->levels
[VLF_CONSOLE
] >= level
;
1105 bool log_to_syslog
= module
->levels
[VLF_SYSLOG
] >= level
;
1106 bool log_to_file
= module
->levels
[VLF_FILE
] >= level
;
1108 if (!(log_to_console
|| log_to_syslog
|| log_to_file
)) {
1109 /* fast path - all logging levels specify no logging, no
1110 * need to hog the log mutex
1115 ovs_mutex_lock(&log_file_mutex
);
1116 log_to_file
&= (log_fd
>= 0);
1117 ovs_mutex_unlock(&log_file_mutex
);
1118 if (log_to_console
|| log_to_syslog
|| log_to_file
) {
1119 int save_errno
= errno
;
1125 ds_reserve(&s
, 1024);
1128 ovs_rwlock_rdlock(&pattern_rwlock
);
1129 if (log_to_console
) {
1130 format_log_message(module
, level
,
1131 destinations
[VLF_CONSOLE
].pattern
, message
,
1133 ds_put_char(&s
, '\n');
1134 fputs(ds_cstr(&s
), stderr
);
1137 if (log_to_syslog
) {
1138 int syslog_level
= syslog_levels
[level
];
1139 char *save_ptr
= NULL
;
1143 format_log_message(module
, level
, destinations
[VLF_SYSLOG
].pattern
,
1145 for (line
= strtok_r(s
.string
, "\n", &save_ptr
); line
;
1146 line
= strtok_r(NULL
, "\n", &save_ptr
)) {
1147 atomic_read_explicit(&log_facility
, &facility
,
1148 memory_order_relaxed
);
1149 syslogger
->class->syslog(syslogger
, syslog_level
|facility
, line
);
1152 if (syslog_fd
>= 0) {
1153 format_log_message(module
, level
,
1154 "<%B>1 %D{%Y-%m-%dT%H:%M:%S.###Z} "
1155 "%E %A %P %c - \xef\xbb\xbf%m",
1157 send_to_syslog_fd(ds_cstr(&s
), s
.length
);
1162 format_log_message(module
, level
, destinations
[VLF_FILE
].pattern
,
1164 ds_put_char(&s
, '\n');
1166 ovs_mutex_lock(&log_file_mutex
);
1169 async_append_write(log_writer
, s
.string
, s
.length
);
1170 if (level
== VLL_EMER
) {
1171 async_append_flush(log_writer
);
1174 ignore(write(log_fd
, s
.string
, s
.length
));
1177 ovs_mutex_unlock(&log_file_mutex
);
1179 ovs_rwlock_unlock(&pattern_rwlock
);
1187 vlog(const struct vlog_module
*module
, enum vlog_level level
,
1188 const char *message
, ...)
1192 va_start(args
, message
);
1193 vlog_valist(module
, level
, message
, args
);
1197 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
1198 * exit code. Always writes the message to stderr, even if the console
1199 * destination is disabled.
1201 * Choose this function instead of vlog_abort_valist() if the daemon monitoring
1202 * facility shouldn't automatically restart the current daemon. */
1204 vlog_fatal_valist(const struct vlog_module
*module_
,
1205 const char *message
, va_list args
)
1207 struct vlog_module
*module
= CONST_CAST(struct vlog_module
*, module_
);
1209 /* Don't log this message to the console to avoid redundancy with the
1210 * message written by the later ovs_fatal_valist(). */
1211 module
->levels
[VLF_CONSOLE
] = VLL_OFF
;
1213 vlog_valist(module
, VLL_EMER
, message
, args
);
1214 ovs_fatal_valist(0, message
, args
);
1217 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
1218 * exit code. Always writes the message to stderr, even if the console
1219 * destination is disabled.
1221 * Choose this function instead of vlog_abort() if the daemon monitoring
1222 * facility shouldn't automatically restart the current daemon. */
1224 vlog_fatal(const struct vlog_module
*module
, const char *message
, ...)
1228 va_start(args
, message
);
1229 vlog_fatal_valist(module
, message
, args
);
1233 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1234 * writes the message to stderr, even if the console destination is disabled.
1236 * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
1237 * facility should automatically restart the current daemon. */
1239 vlog_abort_valist(const struct vlog_module
*module_
,
1240 const char *message
, va_list args
)
1242 struct vlog_module
*module
= (struct vlog_module
*) module_
;
1244 /* Don't log this message to the console to avoid redundancy with the
1245 * message written by the later ovs_abort_valist(). */
1246 module
->levels
[VLF_CONSOLE
] = VLL_OFF
;
1248 vlog_valist(module
, VLL_EMER
, message
, args
);
1249 ovs_abort_valist(0, message
, args
);
1252 /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always
1253 * writes the message to stderr, even if the console destination is disabled.
1255 * Choose this function instead of vlog_fatal() if the daemon monitoring
1256 * facility should automatically restart the current daemon. */
1258 vlog_abort(const struct vlog_module
*module
, const char *message
, ...)
1262 va_start(args
, message
);
1263 vlog_abort_valist(module
, message
, args
);
1268 vlog_should_drop(const struct vlog_module
*module
, enum vlog_level level
,
1269 struct vlog_rate_limit
*rl
)
1271 if (!module
->honor_rate_limits
) {
1275 if (!vlog_is_enabled(module
, level
)) {
1279 ovs_mutex_lock(&rl
->mutex
);
1280 if (!token_bucket_withdraw(&rl
->token_bucket
, VLOG_MSG_TOKENS
)) {
1281 time_t now
= time_now();
1282 if (!rl
->n_dropped
) {
1283 rl
->first_dropped
= now
;
1285 rl
->last_dropped
= now
;
1287 ovs_mutex_unlock(&rl
->mutex
);
1291 if (!rl
->n_dropped
) {
1292 ovs_mutex_unlock(&rl
->mutex
);
1294 time_t now
= time_now();
1295 unsigned int n_dropped
= rl
->n_dropped
;
1296 unsigned int first_dropped_elapsed
= now
- rl
->first_dropped
;
1297 unsigned int last_dropped_elapsed
= now
- rl
->last_dropped
;
1299 ovs_mutex_unlock(&rl
->mutex
);
1302 "Dropped %u log messages in last %u seconds (most recently, "
1303 "%u seconds ago) due to excessive rate",
1304 n_dropped
, first_dropped_elapsed
, last_dropped_elapsed
);
1311 vlog_rate_limit(const struct vlog_module
*module
, enum vlog_level level
,
1312 struct vlog_rate_limit
*rl
, const char *message
, ...)
1314 if (!vlog_should_drop(module
, level
, rl
)) {
1317 va_start(args
, message
);
1318 vlog_valist(module
, level
, message
, args
);
1328 -vSPEC, --verbose=SPEC set logging levels\n\
1329 -v, --verbose set maximum verbosity level\n\
1330 --log-file[=FILE] enable logging to specified FILE\n\
1331 (default: %s/%s.log)\n\
1332 --syslog-method=(libc|unix:file|udp:ip:port)\n\
1333 specify how to send messages to syslog daemon\n\
1334 --syslog-target=HOST:PORT also send syslog msgs to HOST:PORT via UDP\n",
1335 ovs_logdir(), program_name
);