]>
git.proxmox.com Git - ovs.git/blob - lib/vlog.c
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 /* The log modules. */
53 #if USE_LINKER_SECTIONS
54 extern struct vlog_module
*__start_vlog_modules
[];
55 extern struct vlog_module
*__stop_vlog_modules
[];
56 #define vlog_modules __start_vlog_modules
57 #define n_vlog_modules (__stop_vlog_modules - __start_vlog_modules)
59 #define VLOG_MODULE VLOG_DEFINE_MODULE__
60 #include "vlog-modules.def"
63 struct vlog_module
*vlog_modules
[] = {
64 #define VLOG_MODULE(NAME) &VLM_##NAME,
65 #include "vlog-modules.def"
68 #define n_vlog_modules ARRAY_SIZE(vlog_modules)
71 /* Information about each facility. */
73 const char *name
; /* Name. */
74 char *pattern
; /* Current pattern. */
75 bool default_pattern
; /* Whether current pattern is the default. */
77 static struct facility facilities
[VLF_N_FACILITIES
] = {
78 #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
83 /* Time at which vlog was initialized, in milliseconds. */
84 static long long int boot_time
;
86 /* VLF_FILE configuration. */
87 static char *log_file_name
;
88 static FILE *log_file
;
90 /* vlog initialized? */
91 static bool vlog_inited
;
93 static void format_log_message(const struct vlog_module
*, enum vlog_level
,
94 enum vlog_facility
, unsigned int msg_num
,
95 const char *message
, va_list, struct ds
*)
98 /* Searches the 'n_names' in 'names'. Returns the index of a match for
99 * 'target', or 'n_names' if no name matches. */
101 search_name_array(const char *target
, const char **names
, size_t n_names
)
105 for (i
= 0; i
< n_names
; i
++) {
107 if (!strcasecmp(names
[i
], target
)) {
114 /* Returns the name for logging level 'level'. */
116 vlog_get_level_name(enum vlog_level level
)
118 assert(level
< VLL_N_LEVELS
);
119 return level_names
[level
];
122 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
123 * is not the name of a logging level. */
125 vlog_get_level_val(const char *name
)
127 return search_name_array(name
, level_names
, ARRAY_SIZE(level_names
));
130 /* Returns the name for logging facility 'facility'. */
132 vlog_get_facility_name(enum vlog_facility facility
)
134 assert(facility
< VLF_N_FACILITIES
);
135 return facilities
[facility
].name
;
138 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
139 * not the name of a logging facility. */
141 vlog_get_facility_val(const char *name
)
145 for (i
= 0; i
< VLF_N_FACILITIES
; i
++) {
146 if (!strcasecmp(facilities
[i
].name
, name
)) {
153 /* Returns the name for logging module 'module'. */
155 vlog_get_module_name(const struct vlog_module
*module
)
160 /* Returns the logging module named 'name', or NULL if 'name' is not the name
161 * of a logging module. */
163 vlog_module_from_name(const char *name
)
165 struct vlog_module
**mp
;
167 for (mp
= vlog_modules
; mp
< &vlog_modules
[n_vlog_modules
]; mp
++) {
168 if (!strcasecmp(name
, (*mp
)->name
)) {
175 /* Returns the current logging level for the given 'module' and 'facility'. */
177 vlog_get_level(const struct vlog_module
*module
, enum vlog_facility facility
)
179 assert(facility
< VLF_N_FACILITIES
);
180 return module
->levels
[facility
];
184 update_min_level(struct vlog_module
*module
)
186 enum vlog_facility facility
;
188 module
->min_level
= VLL_EMER
;
189 for (facility
= 0; facility
< VLF_N_FACILITIES
; facility
++) {
190 if (log_file
|| facility
!= VLF_FILE
) {
191 enum vlog_level level
= module
->levels
[facility
];
192 if (level
> module
->min_level
) {
193 module
->min_level
= level
;
200 set_facility_level(enum vlog_facility facility
, struct vlog_module
*module
,
201 enum vlog_level level
)
203 assert(facility
>= 0 && facility
< VLF_N_FACILITIES
);
204 assert(level
< VLL_N_LEVELS
);
207 struct vlog_module
**mp
;
209 for (mp
= vlog_modules
; mp
< &vlog_modules
[n_vlog_modules
]; mp
++) {
210 (*mp
)->levels
[facility
] = level
;
211 update_min_level(*mp
);
214 module
->levels
[facility
] = level
;
215 update_min_level(module
);
219 /* Sets the logging level for the given 'module' and 'facility' to 'level'. A
220 * null 'module' or a 'facility' of VLF_ANY_FACILITY is treated as a wildcard
221 * across all modules or facilities, respectively. */
223 vlog_set_levels(struct vlog_module
*module
, enum vlog_facility facility
,
224 enum vlog_level level
)
226 assert(facility
< VLF_N_FACILITIES
|| facility
== VLF_ANY_FACILITY
);
227 if (facility
== VLF_ANY_FACILITY
) {
228 for (facility
= 0; facility
< VLF_N_FACILITIES
; facility
++) {
229 set_facility_level(facility
, module
, level
);
232 set_facility_level(facility
, module
, level
);
237 do_set_pattern(enum vlog_facility facility
, const char *pattern
)
239 struct facility
*f
= &facilities
[facility
];
240 if (!f
->default_pattern
) {
243 f
->default_pattern
= false;
245 f
->pattern
= xstrdup(pattern
);
248 /* Sets the pattern for the given 'facility' to 'pattern'. */
250 vlog_set_pattern(enum vlog_facility facility
, const char *pattern
)
252 assert(facility
< VLF_N_FACILITIES
|| facility
== VLF_ANY_FACILITY
);
253 if (facility
== VLF_ANY_FACILITY
) {
254 for (facility
= 0; facility
< VLF_N_FACILITIES
; facility
++) {
255 do_set_pattern(facility
, pattern
);
258 do_set_pattern(facility
, pattern
);
262 /* Returns the name of the log file used by VLF_FILE, or a null pointer if no
263 * log file has been set. (A non-null return value does not assert that the
264 * named log file is in use: if vlog_set_log_file() or vlog_reopen_log_file()
265 * fails, it still sets the log file name.) */
267 vlog_get_log_file(void)
269 return log_file_name
;
272 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
273 * default file name if 'file_name' is null. Returns 0 if successful,
274 * otherwise a positive errno value. */
276 vlog_set_log_file(const char *file_name
)
278 char *old_log_file_name
;
279 struct vlog_module
**mp
;
282 /* Close old log file. */
284 VLOG_INFO("closing log file");
289 /* Update log file name and free old name. The ordering is important
290 * because 'file_name' might be 'log_file_name' or some suffix of it. */
291 old_log_file_name
= log_file_name
;
292 log_file_name
= (file_name
294 : xasprintf("%s/%s.log", ovs_logdir
, program_name
));
295 free(old_log_file_name
);
296 file_name
= NULL
; /* Might have been freed. */
298 /* Open new log file and update min_levels[] to reflect whether we actually
299 * have a log_file. */
300 log_file
= fopen(log_file_name
, "a");
301 for (mp
= vlog_modules
; mp
< &vlog_modules
[n_vlog_modules
]; mp
++) {
302 update_min_level(*mp
);
305 /* Log success or failure. */
307 VLOG_WARN("failed to open %s for logging: %s",
308 log_file_name
, strerror(errno
));
311 VLOG_INFO("opened log file %s", log_file_name
);
318 /* Closes and then attempts to re-open the current log file. (This is useful
319 * just after log rotation, to ensure that the new log file starts being used.)
320 * Returns 0 if successful, otherwise a positive errno value. */
322 vlog_reopen_log_file(void)
324 return log_file_name
? vlog_set_log_file(log_file_name
) : 0;
327 /* Set debugging levels:
329 * mod[:facility[:level]] mod2[:facility[:level]] ...
331 * Return null if successful, otherwise an error message that the caller must
335 vlog_set_levels_from_string(const char *s_
)
337 char *save_ptr
= NULL
;
338 char *s
= xstrdup(s_
);
339 char *module
, *facility
;
341 for (module
= strtok_r(s
, ": \t", &save_ptr
); module
!= NULL
;
342 module
= strtok_r(NULL
, ": \t", &save_ptr
)) {
343 struct vlog_module
*e_module
;
344 enum vlog_facility e_facility
;
346 facility
= strtok_r(NULL
, ":", &save_ptr
);
348 if (!facility
|| !strcmp(facility
, "ANY")) {
349 e_facility
= VLF_ANY_FACILITY
;
351 e_facility
= vlog_get_facility_val(facility
);
352 if (e_facility
>= VLF_N_FACILITIES
) {
353 char *msg
= xasprintf("unknown facility \"%s\"", facility
);
359 if (!strcmp(module
, "PATTERN")) {
360 vlog_set_pattern(e_facility
, save_ptr
);
364 enum vlog_level e_level
;
366 if (!strcmp(module
, "ANY")) {
369 e_module
= vlog_module_from_name(module
);
371 char *msg
= xasprintf("unknown module \"%s\"", module
);
377 level
= strtok_r(NULL
, ":", &save_ptr
);
378 e_level
= level
? vlog_get_level_val(level
) : VLL_DBG
;
379 if (e_level
>= VLL_N_LEVELS
) {
380 char *msg
= xasprintf("unknown level \"%s\"", level
);
385 vlog_set_levels(e_module
, e_facility
, e_level
);
392 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
393 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
395 vlog_set_verbosity(const char *arg
)
398 char *msg
= vlog_set_levels_from_string(arg
);
400 ovs_fatal(0, "processing \"%s\": %s", arg
, msg
);
403 vlog_set_levels(NULL
, VLF_ANY_FACILITY
, VLL_DBG
);
408 vlog_unixctl_set(struct unixctl_conn
*conn
,
409 const char *args
, void *aux OVS_UNUSED
)
411 char *msg
= vlog_set_levels_from_string(args
);
412 unixctl_command_reply(conn
, msg
? 501 : 202, msg
);
417 vlog_unixctl_list(struct unixctl_conn
*conn
,
418 const char *args OVS_UNUSED
, void *aux OVS_UNUSED
)
420 char *msg
= vlog_get_levels();
421 unixctl_command_reply(conn
, 200, msg
);
426 vlog_unixctl_reopen(struct unixctl_conn
*conn
,
427 const char *args OVS_UNUSED
, void *aux OVS_UNUSED
)
430 int error
= vlog_reopen_log_file();
432 unixctl_command_reply(conn
, 503, strerror(errno
));
434 unixctl_command_reply(conn
, 202, NULL
);
437 unixctl_command_reply(conn
, 403, "Logging to file not configured");
441 /* Initializes the logging subsystem and registers its unixctl server
453 openlog(program_name
, LOG_NDELAY
, LOG_DAEMON
);
455 boot_time
= time_msec();
461 localtime_r(&now
, &tm
);
462 strftime(s
, sizeof s
, "%a, %d %b %Y %H:%M:%S %z", &tm
);
463 VLOG_ERR("current time is negative: %s (%ld)", s
, (long int) now
);
466 unixctl_command_register("vlog/set", vlog_unixctl_set
, NULL
);
467 unixctl_command_register("vlog/list", vlog_unixctl_list
, NULL
);
468 unixctl_command_register("vlog/reopen", vlog_unixctl_reopen
, NULL
);
471 /* Closes the logging subsystem. */
481 /* Print the current logging level for each module. */
483 vlog_get_levels(void)
485 struct ds s
= DS_EMPTY_INITIALIZER
;
486 struct vlog_module
**mp
;
488 ds_put_format(&s
, " console syslog file\n");
489 ds_put_format(&s
, " ------- ------ ------\n");
491 for (mp
= vlog_modules
; mp
< &vlog_modules
[n_vlog_modules
]; mp
++) {
492 ds_put_format(&s
, "%-16s %4s %4s %4s\n",
493 vlog_get_module_name(*mp
),
494 vlog_get_level_name(vlog_get_level(*mp
, VLF_CONSOLE
)),
495 vlog_get_level_name(vlog_get_level(*mp
, VLF_SYSLOG
)),
496 vlog_get_level_name(vlog_get_level(*mp
, VLF_FILE
)));
502 /* Returns true if a log message emitted for the given 'module' and 'level'
503 * would cause some log output, false if that module and level are completely
506 vlog_is_enabled(const struct vlog_module
*module
, enum vlog_level level
)
508 return module
->min_level
>= level
;
512 fetch_braces(const char *p
, const char *def
, char *out
, size_t out_size
)
515 size_t n
= strcspn(p
+ 1, "}");
516 size_t n_copy
= MIN(n
, out_size
- 1);
517 memcpy(out
, p
+ 1, n_copy
);
521 ovs_strlcpy(out
, def
, out_size
);
527 format_log_message(const struct vlog_module
*module
, enum vlog_level level
,
528 enum vlog_facility facility
, unsigned int msg_num
,
529 const char *message
, va_list args_
, struct ds
*s
)
536 for (p
= facilities
[facility
].pattern
; *p
!= '\0'; ) {
537 enum { LEFT
, RIGHT
} justify
= RIGHT
;
539 size_t length
, field
, used
;
542 ds_put_char(s
, *p
++);
556 while (isdigit((unsigned char)*p
)) {
557 field
= (field
* 10) + (*p
- '0');
564 ds_put_cstr(s
, program_name
);
567 p
= fetch_braces(p
, "", tmp
, sizeof tmp
);
568 ds_put_cstr(s
, vlog_get_module_name(module
));
571 p
= fetch_braces(p
, "%Y-%m-%d %H:%M:%S", tmp
, sizeof tmp
);
572 ds_put_strftime(s
, tmp
, NULL
);
575 /* Format user-supplied log message and trim trailing new-lines. */
577 va_copy(args
, args_
);
578 ds_put_format_valist(s
, message
, args
);
580 while (s
->length
> length
&& s
->string
[s
->length
- 1] == '\n') {
585 ds_put_format(s
, "%u", msg_num
);
588 ds_put_char(s
, '\n');
591 ds_put_cstr(s
, vlog_get_level_name(level
));
594 ds_put_format(s
, "%ld", (long int) getpid());
597 ds_put_format(s
, "%lld", time_msec() - boot_time
);
600 ds_put_char(s
, p
[-1]);
603 used
= s
->length
- length
;
605 size_t n_pad
= field
- used
;
606 if (justify
== RIGHT
) {
607 ds_put_uninit(s
, n_pad
);
608 memmove(&s
->string
[length
+ n_pad
], &s
->string
[length
], used
);
609 memset(&s
->string
[length
], pad
, n_pad
);
611 ds_put_char_multiple(s
, pad
, n_pad
);
617 /* Writes 'message' to the log at the given 'level' and as coming from the
620 * Guaranteed to preserve errno. */
622 vlog_valist(const struct vlog_module
*module
, enum vlog_level level
,
623 const char *message
, va_list args
)
625 bool log_to_console
= module
->levels
[VLF_CONSOLE
] >= level
;
626 bool log_to_syslog
= module
->levels
[VLF_SYSLOG
] >= level
;
627 bool log_to_file
= module
->levels
[VLF_FILE
] >= level
&& log_file
;
628 if (log_to_console
|| log_to_syslog
|| log_to_file
) {
629 int save_errno
= errno
;
630 static unsigned int msg_num
;
636 ds_reserve(&s
, 1024);
639 if (log_to_console
) {
640 format_log_message(module
, level
, VLF_CONSOLE
, msg_num
,
642 ds_put_char(&s
, '\n');
643 fputs(ds_cstr(&s
), stderr
);
647 int syslog_level
= syslog_levels
[level
];
648 char *save_ptr
= NULL
;
651 format_log_message(module
, level
, VLF_SYSLOG
, msg_num
,
653 for (line
= strtok_r(s
.string
, "\n", &save_ptr
); line
;
654 line
= strtok_r(NULL
, "\n", &save_ptr
)) {
655 syslog(syslog_level
, "%s", line
);
660 format_log_message(module
, level
, VLF_FILE
, msg_num
,
662 ds_put_char(&s
, '\n');
663 fputs(ds_cstr(&s
), log_file
);
673 vlog(const struct vlog_module
*module
, enum vlog_level level
,
674 const char *message
, ...)
678 va_start(args
, message
);
679 vlog_valist(module
, level
, message
, args
);
684 vlog_should_drop(const struct vlog_module
*module
, enum vlog_level level
,
685 struct vlog_rate_limit
*rl
)
687 if (!vlog_is_enabled(module
, level
)) {
691 if (rl
->tokens
< VLOG_MSG_TOKENS
) {
692 time_t now
= time_now();
693 if (rl
->last_fill
> now
) {
694 /* Last filled in the future? Time must have gone backward, or
695 * 'rl' has not been used before. */
696 rl
->tokens
= rl
->burst
;
697 } else if (rl
->last_fill
< now
) {
698 unsigned int add
= sat_mul(rl
->rate
, now
- rl
->last_fill
);
699 unsigned int tokens
= sat_add(rl
->tokens
, add
);
700 rl
->tokens
= MIN(tokens
, rl
->burst
);
703 if (rl
->tokens
< VLOG_MSG_TOKENS
) {
704 if (!rl
->n_dropped
) {
705 rl
->first_dropped
= now
;
711 rl
->tokens
-= VLOG_MSG_TOKENS
;
715 "Dropped %u log messages in last %u seconds "
716 "due to excessive rate",
717 rl
->n_dropped
, (unsigned int) (time_now() - rl
->first_dropped
));
724 vlog_rate_limit(const struct vlog_module
*module
, enum vlog_level level
,
725 struct vlog_rate_limit
*rl
, const char *message
, ...)
727 if (!vlog_should_drop(module
, level
, rl
)) {
730 va_start(args
, message
);
731 vlog_valist(module
, level
, message
, args
);
739 printf("\nLogging options:\n"
740 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
741 " -v, --verbose set maximum verbosity level\n"
742 " --log-file[=FILE] enable logging to specified FILE\n"
743 " (default: %s/%s.log)\n",
744 ovs_logdir
, program_name
);