2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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.
25 #include <sys/types.h>
30 #include "dynamic-string.h"
36 VLOG_DEFINE_THIS_MODULE(vlog)
38 /* Name for each logging level. */
39 static const char *level_names[VLL_N_LEVELS] = {
40 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) #NAME,
45 /* Syslog value for each logging level. */
46 static int syslog_levels[VLL_N_LEVELS] = {
47 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) SYSLOG_LEVEL,
52 /* Name for each logging module */
53 static const char *module_names[VLM_N_MODULES] = {
54 #define VLOG_MODULE(NAME) #NAME,
55 #include "vlog-modules.def"
59 /* Information about each facility. */
61 const char *name; /* Name. */
62 char *pattern; /* Current pattern. */
63 bool default_pattern; /* Whether current pattern is the default. */
65 static struct facility facilities[VLF_N_FACILITIES] = {
66 #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
71 /* Current log levels. */
72 static int levels[VLM_N_MODULES][VLF_N_FACILITIES] = {
73 #define VLOG_MODULE(NAME) { VLL_INFO, VLL_INFO, VLL_INFO },
74 #include "vlog-modules.def"
78 /* For fast checking whether we're logging anything for a given module and
80 enum vlog_level min_vlog_levels[VLM_N_MODULES] = {
81 #define VLOG_MODULE(NAME) VLL_INFO,
82 #include "vlog-modules.def"
86 /* Time at which vlog was initialized, in milliseconds. */
87 static long long int boot_time;
89 /* VLF_FILE configuration. */
90 static char *log_file_name;
91 static FILE *log_file;
93 /* vlog initialized? */
94 static bool vlog_inited;
96 static void format_log_message(enum vlog_module, enum vlog_level,
97 enum vlog_facility, unsigned int msg_num,
98 const char *message, va_list, struct ds *)
101 /* Searches the 'n_names' in 'names'. Returns the index of a match for
102 * 'target', or 'n_names' if no name matches. */
104 search_name_array(const char *target, const char **names, size_t n_names)
108 for (i = 0; i < n_names; i++) {
110 if (!strcasecmp(names[i], target)) {
117 /* Returns the name for logging level 'level'. */
119 vlog_get_level_name(enum vlog_level level)
121 assert(level < VLL_N_LEVELS);
122 return level_names[level];
125 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
126 * is not the name of a logging level. */
128 vlog_get_level_val(const char *name)
130 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
133 /* Returns the name for logging facility 'facility'. */
135 vlog_get_facility_name(enum vlog_facility facility)
137 assert(facility < VLF_N_FACILITIES);
138 return facilities[facility].name;
141 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
142 * not the name of a logging facility. */
144 vlog_get_facility_val(const char *name)
148 for (i = 0; i < VLF_N_FACILITIES; i++) {
149 if (!strcasecmp(facilities[i].name, name)) {
156 /* Returns the name for logging module 'module'. */
157 const char *vlog_get_module_name(enum vlog_module module)
159 assert(module < VLM_N_MODULES);
160 return module_names[module];
163 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
164 * the name of a logging module. */
166 vlog_get_module_val(const char *name)
168 return search_name_array(name, module_names, ARRAY_SIZE(module_names));
171 /* Returns the current logging level for the given 'module' and 'facility'. */
173 vlog_get_level(enum vlog_module module, enum vlog_facility facility)
175 assert(module < VLM_N_MODULES);
176 assert(facility < VLF_N_FACILITIES);
177 return levels[module][facility];
181 update_min_level(enum vlog_module module)
183 enum vlog_level min_level = VLL_EMER;
184 enum vlog_facility facility;
186 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
187 if (log_file || facility != VLF_FILE) {
188 min_level = MAX(min_level, levels[module][facility]);
191 min_vlog_levels[module] = min_level;
195 set_facility_level(enum vlog_facility facility, enum vlog_module module,
196 enum vlog_level level)
198 assert(facility >= 0 && facility < VLF_N_FACILITIES);
199 assert(level < VLL_N_LEVELS);
201 if (module == VLM_ANY_MODULE) {
202 for (module = 0; module < VLM_N_MODULES; module++) {
203 levels[module][facility] = level;
204 update_min_level(module);
207 levels[module][facility] = level;
208 update_min_level(module);
212 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
214 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
215 enum vlog_level level)
217 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
218 if (facility == VLF_ANY_FACILITY) {
219 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
220 set_facility_level(facility, module, level);
223 set_facility_level(facility, module, level);
228 do_set_pattern(enum vlog_facility facility, const char *pattern)
230 struct facility *f = &facilities[facility];
231 if (!f->default_pattern) {
234 f->default_pattern = false;
236 f->pattern = xstrdup(pattern);
239 /* Sets the pattern for the given 'facility' to 'pattern'. */
241 vlog_set_pattern(enum vlog_facility facility, const char *pattern)
243 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
244 if (facility == VLF_ANY_FACILITY) {
245 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
246 do_set_pattern(facility, pattern);
249 do_set_pattern(facility, pattern);
253 /* Returns the name of the log file used by VLF_FILE, or a null pointer if no
254 * log file has been set. (A non-null return value does not assert that the
255 * named log file is in use: if vlog_set_log_file() or vlog_reopen_log_file()
256 * fails, it still sets the log file name.) */
258 vlog_get_log_file(void)
260 return log_file_name;
263 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
264 * default file name if 'file_name' is null. Returns 0 if successful,
265 * otherwise a positive errno value. */
267 vlog_set_log_file(const char *file_name)
269 char *old_log_file_name;
270 enum vlog_module module;
273 /* Close old log file. */
275 VLOG_INFO("closing log file");
280 /* Update log file name and free old name. The ordering is important
281 * because 'file_name' might be 'log_file_name' or some suffix of it. */
282 old_log_file_name = log_file_name;
283 log_file_name = (file_name
285 : xasprintf("%s/%s.log", ovs_logdir, program_name));
286 free(old_log_file_name);
287 file_name = NULL; /* Might have been freed. */
289 /* Open new log file and update min_levels[] to reflect whether we actually
290 * have a log_file. */
291 log_file = fopen(log_file_name, "a");
292 for (module = 0; module < VLM_N_MODULES; module++) {
293 update_min_level(module);
296 /* Log success or failure. */
298 VLOG_WARN("failed to open %s for logging: %s",
299 log_file_name, strerror(errno));
302 VLOG_INFO("opened log file %s", log_file_name);
309 /* Closes and then attempts to re-open the current log file. (This is useful
310 * just after log rotation, to ensure that the new log file starts being used.)
311 * Returns 0 if successful, otherwise a positive errno value. */
313 vlog_reopen_log_file(void)
315 return log_file_name ? vlog_set_log_file(log_file_name) : 0;
318 /* Set debugging levels:
320 * mod[:facility[:level]] mod2[:facility[:level]] ...
322 * Return null if successful, otherwise an error message that the caller must
326 vlog_set_levels_from_string(const char *s_)
328 char *save_ptr = NULL;
329 char *s = xstrdup(s_);
330 char *module, *facility;
332 for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
333 module = strtok_r(NULL, ": \t", &save_ptr)) {
334 enum vlog_module e_module;
335 enum vlog_facility e_facility;
337 facility = strtok_r(NULL, ":", &save_ptr);
339 if (!facility || !strcmp(facility, "ANY")) {
340 e_facility = VLF_ANY_FACILITY;
342 e_facility = vlog_get_facility_val(facility);
343 if (e_facility >= VLF_N_FACILITIES) {
344 char *msg = xasprintf("unknown facility \"%s\"", facility);
350 if (!strcmp(module, "PATTERN")) {
351 vlog_set_pattern(e_facility, save_ptr);
355 enum vlog_level e_level;
357 if (!strcmp(module, "ANY")) {
358 e_module = VLM_ANY_MODULE;
360 e_module = vlog_get_module_val(module);
361 if (e_module >= VLM_N_MODULES) {
362 char *msg = xasprintf("unknown module \"%s\"", module);
368 level = strtok_r(NULL, ":", &save_ptr);
369 e_level = level ? vlog_get_level_val(level) : VLL_DBG;
370 if (e_level >= VLL_N_LEVELS) {
371 char *msg = xasprintf("unknown level \"%s\"", level);
376 vlog_set_levels(e_module, e_facility, e_level);
383 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
384 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
386 vlog_set_verbosity(const char *arg)
389 char *msg = vlog_set_levels_from_string(arg);
391 ovs_fatal(0, "processing \"%s\": %s", arg, msg);
394 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_DBG);
399 vlog_unixctl_set(struct unixctl_conn *conn,
400 const char *args, void *aux OVS_UNUSED)
402 char *msg = vlog_set_levels_from_string(args);
403 unixctl_command_reply(conn, msg ? 501 : 202, msg);
408 vlog_unixctl_list(struct unixctl_conn *conn,
409 const char *args OVS_UNUSED, void *aux OVS_UNUSED)
411 char *msg = vlog_get_levels();
412 unixctl_command_reply(conn, 200, msg);
417 vlog_unixctl_reopen(struct unixctl_conn *conn,
418 const char *args OVS_UNUSED, void *aux OVS_UNUSED)
421 int error = vlog_reopen_log_file();
423 unixctl_command_reply(conn, 503, strerror(errno));
425 unixctl_command_reply(conn, 202, NULL);
428 unixctl_command_reply(conn, 403, "Logging to file not configured");
432 /* Initializes the logging subsystem. */
443 openlog(program_name, LOG_NDELAY, LOG_DAEMON);
445 boot_time = time_msec();
451 localtime_r(&now, &tm);
452 strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm);
453 VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
456 unixctl_command_register("vlog/set", vlog_unixctl_set, NULL);
457 unixctl_command_register("vlog/list", vlog_unixctl_list, NULL);
458 unixctl_command_register("vlog/reopen", vlog_unixctl_reopen, NULL);
461 /* Closes the logging subsystem. */
471 /* Print the current logging level for each module. */
473 vlog_get_levels(void)
475 struct ds s = DS_EMPTY_INITIALIZER;
476 enum vlog_module module;
478 ds_put_format(&s, " console syslog file\n");
479 ds_put_format(&s, " ------- ------ ------\n");
481 for (module = 0; module < VLM_N_MODULES; module++) {
482 ds_put_format(&s, "%-16s %4s %4s %4s\n",
483 vlog_get_module_name(module),
484 vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
485 vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)),
486 vlog_get_level_name(vlog_get_level(module, VLF_FILE)));
492 /* Returns true if a log message emitted for the given 'module' and 'level'
493 * would cause some log output, false if that module and level are completely
496 vlog_is_enabled(enum vlog_module module, enum vlog_level level)
498 return min_vlog_levels[module] >= level;
502 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
505 size_t n = strcspn(p + 1, "}");
506 size_t n_copy = MIN(n, out_size - 1);
507 memcpy(out, p + 1, n_copy);
511 ovs_strlcpy(out, def, out_size);
517 format_log_message(enum vlog_module module, enum vlog_level level,
518 enum vlog_facility facility, unsigned int msg_num,
519 const char *message, va_list args_, struct ds *s)
526 for (p = facilities[facility].pattern; *p != '\0'; ) {
527 enum { LEFT, RIGHT } justify = RIGHT;
529 size_t length, field, used;
532 ds_put_char(s, *p++);
546 while (isdigit((unsigned char)*p)) {
547 field = (field * 10) + (*p - '0');
554 ds_put_cstr(s, program_name);
557 p = fetch_braces(p, "", tmp, sizeof tmp);
558 ds_put_cstr(s, vlog_get_module_name(module));
561 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
562 ds_put_strftime(s, tmp, NULL);
565 /* Format user-supplied log message and trim trailing new-lines. */
567 va_copy(args, args_);
568 ds_put_format_valist(s, message, args);
570 while (s->length > length && s->string[s->length - 1] == '\n') {
575 ds_put_format(s, "%u", msg_num);
578 ds_put_char(s, '\n');
581 ds_put_cstr(s, vlog_get_level_name(level));
584 ds_put_format(s, "%ld", (long int) getpid());
587 ds_put_format(s, "%lld", time_msec() - boot_time);
590 ds_put_char(s, p[-1]);
593 used = s->length - length;
595 size_t n_pad = field - used;
596 if (justify == RIGHT) {
597 ds_put_uninit(s, n_pad);
598 memmove(&s->string[length + n_pad], &s->string[length], used);
599 memset(&s->string[length], pad, n_pad);
601 ds_put_char_multiple(s, pad, n_pad);
607 /* Writes 'message' to the log at the given 'level' and as coming from the
610 * Guaranteed to preserve errno. */
612 vlog_valist(enum vlog_module module, enum vlog_level level,
613 const char *message, va_list args)
615 bool log_to_console = levels[module][VLF_CONSOLE] >= level;
616 bool log_to_syslog = levels[module][VLF_SYSLOG] >= level;
617 bool log_to_file = levels[module][VLF_FILE] >= level && log_file;
618 if (log_to_console || log_to_syslog || log_to_file) {
619 int save_errno = errno;
620 static unsigned int msg_num;
626 ds_reserve(&s, 1024);
629 if (log_to_console) {
630 format_log_message(module, level, VLF_CONSOLE, msg_num,
632 ds_put_char(&s, '\n');
633 fputs(ds_cstr(&s), stderr);
637 int syslog_level = syslog_levels[level];
638 char *save_ptr = NULL;
641 format_log_message(module, level, VLF_SYSLOG, msg_num,
643 for (line = strtok_r(s.string, "\n", &save_ptr); line;
644 line = strtok_r(NULL, "\n", &save_ptr)) {
645 syslog(syslog_level, "%s", line);
650 format_log_message(module, level, VLF_FILE, msg_num,
652 ds_put_char(&s, '\n');
653 fputs(ds_cstr(&s), log_file);
663 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
667 va_start(args, message);
668 vlog_valist(module, level, message, args);
673 vlog_should_drop(enum vlog_module module, enum vlog_level level,
674 struct vlog_rate_limit *rl)
676 if (!vlog_is_enabled(module, level)) {
680 if (rl->tokens < VLOG_MSG_TOKENS) {
681 time_t now = time_now();
682 if (rl->last_fill > now) {
683 /* Last filled in the future? Time must have gone backward, or
684 * 'rl' has not been used before. */
685 rl->tokens = rl->burst;
686 } else if (rl->last_fill < now) {
687 unsigned int add = sat_mul(rl->rate, now - rl->last_fill);
688 unsigned int tokens = sat_add(rl->tokens, add);
689 rl->tokens = MIN(tokens, rl->burst);
692 if (rl->tokens < VLOG_MSG_TOKENS) {
693 if (!rl->n_dropped) {
694 rl->first_dropped = now;
700 rl->tokens -= VLOG_MSG_TOKENS;
704 "Dropped %u log messages in last %u seconds "
705 "due to excessive rate",
706 rl->n_dropped, (unsigned int) (time_now() - rl->first_dropped));
713 vlog_rate_limit(enum vlog_module module, enum vlog_level level,
714 struct vlog_rate_limit *rl, const char *message, ...)
716 if (!vlog_should_drop(module, level, rl)) {
719 va_start(args, message);
720 vlog_valist(module, level, message, args);
728 printf("\nLogging options:\n"
729 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
730 " -v, --verbose set maximum verbosity level\n"
731 " --log-file[=FILE] enable logging to specified FILE\n"
732 " (default: %s/%s.log)\n",
733 ovs_logdir, program_name);