1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
42 #include <sys/types.h>
46 #include "dynamic-string.h"
51 #define THIS_MODULE VLM_vlog
53 /* Name for each logging level. */
54 static const char *level_names[VLL_N_LEVELS] = {
55 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) #NAME,
60 /* Syslog value for each logging level. */
61 static int syslog_levels[VLL_N_LEVELS] = {
62 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL) SYSLOG_LEVEL,
67 /* Name for each logging module */
68 static const char *module_names[VLM_N_MODULES] = {
69 #define VLOG_MODULE(NAME) #NAME,
70 #include "vlog-modules.def"
74 /* Information about each facility. */
76 const char *name; /* Name. */
77 char *pattern; /* Current pattern. */
78 bool default_pattern; /* Whether current pattern is the default. */
80 static struct facility facilities[VLF_N_FACILITIES] = {
81 #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
86 /* Current log levels. */
87 static int levels[VLM_N_MODULES][VLF_N_FACILITIES];
89 /* Time at which vlog was initialized, in milliseconds. */
90 static long long int boot_time;
92 /* Searches the 'n_names' in 'names'. Returns the index of a match for
93 * 'target', or 'n_names' if no name matches. */
95 search_name_array(const char *target, const char **names, size_t n_names)
99 for (i = 0; i < n_names; i++) {
101 if (!strcasecmp(names[i], target)) {
108 /* Returns the name for logging level 'level'. */
110 vlog_get_level_name(enum vlog_level level)
112 assert(level < VLL_N_LEVELS);
113 return level_names[level];
116 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
117 * is not the name of a logging level. */
119 vlog_get_level_val(const char *name)
121 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
124 /* Returns the name for logging facility 'facility'. */
126 vlog_get_facility_name(enum vlog_facility facility)
128 assert(facility < VLF_N_FACILITIES);
129 return facilities[facility].name;
132 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
133 * not the name of a logging facility. */
135 vlog_get_facility_val(const char *name)
139 for (i = 0; i < VLF_N_FACILITIES; i++) {
140 if (!strcasecmp(facilities[i].name, name)) {
147 /* Returns the name for logging module 'module'. */
148 const char *vlog_get_module_name(enum vlog_module module)
150 assert(module < VLM_N_MODULES);
151 return module_names[module];
154 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
155 * the name of a logging module. */
157 vlog_get_module_val(const char *name)
159 return search_name_array(name, module_names, ARRAY_SIZE(module_names));
162 /* Returns the current logging level for the given 'module' and 'facility'. */
164 vlog_get_level(enum vlog_module module, enum vlog_facility facility)
166 assert(module < VLM_N_MODULES);
167 assert(facility < VLF_N_FACILITIES);
168 return levels[module][facility];
172 set_facility_level(enum vlog_facility facility, enum vlog_module module,
173 enum vlog_level level)
175 assert(facility >= 0 && facility < VLF_N_FACILITIES);
176 assert(level < VLL_N_LEVELS);
178 if (module == VLM_ANY_MODULE) {
179 for (module = 0; module < VLM_N_MODULES; module++) {
180 levels[module][facility] = level;
183 levels[module][facility] = level;
187 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
189 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
190 enum vlog_level level)
192 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
193 if (facility == VLF_ANY_FACILITY) {
194 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
195 set_facility_level(facility, module, level);
198 set_facility_level(facility, module, level);
203 do_set_pattern(enum vlog_facility facility, const char *pattern)
205 struct facility *f = &facilities[facility];
206 if (!f->default_pattern) {
209 f->default_pattern = false;
211 f->pattern = xstrdup(pattern);
214 /* Sets the pattern for the given 'facility' to 'pattern'. */
216 vlog_set_pattern(enum vlog_facility facility, const char *pattern)
218 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
219 if (facility == VLF_ANY_FACILITY) {
220 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
221 do_set_pattern(facility, pattern);
224 do_set_pattern(facility, pattern);
228 /* Set debugging levels:
230 * mod[:facility[:level]] mod2[:facility[:level]] ...
232 * Return null if successful, otherwise an error message that the caller must
236 vlog_set_levels_from_string(const char *s_)
239 char *s = xstrdup(s_);
240 char *module, *facility;
242 for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
243 module = strtok_r(NULL, ": \t", &save_ptr)) {
244 enum vlog_module e_module;
245 enum vlog_facility e_facility;
247 facility = strtok_r(NULL, ":", &save_ptr);
249 if (!facility || !strcmp(facility, "ANY")) {
250 e_facility = VLF_ANY_FACILITY;
252 e_facility = vlog_get_facility_val(facility);
253 if (e_facility >= VLF_N_FACILITIES) {
254 char *msg = xasprintf("unknown facility \"%s\"", facility);
260 if (!strcmp(module, "PATTERN")) {
261 vlog_set_pattern(e_facility, save_ptr);
265 enum vlog_level e_level;
267 if (!strcmp(module, "ANY")) {
268 e_module = VLM_ANY_MODULE;
270 e_module = vlog_get_module_val(module);
271 if (e_module >= VLM_N_MODULES) {
272 char *msg = xasprintf("unknown module \"%s\"", module);
278 level = strtok_r(NULL, ":", &save_ptr);
279 e_level = level ? vlog_get_level_val(level) : VLL_DBG;
280 if (e_level >= VLL_N_LEVELS) {
281 char *msg = xasprintf("unknown level \"%s\"", level);
286 vlog_set_levels(e_module, e_facility, e_level);
293 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
294 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
296 vlog_set_verbosity(const char *arg)
299 char *msg = vlog_set_levels_from_string(arg);
301 ofp_fatal(0, "processing \"%s\": %s", arg, msg);
304 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_DBG);
308 /* Initializes the logging subsystem. */
314 openlog(program_name, LOG_NDELAY, LOG_DAEMON);
315 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_WARN);
317 boot_time = time_msec();
323 localtime_r(&now, &tm);
324 strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm);
325 VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
329 /* Closes the logging subsystem. */
336 /* Print the current logging level for each module. */
338 vlog_get_levels(void)
340 struct ds s = DS_EMPTY_INITIALIZER;
341 enum vlog_module module;
343 ds_put_format(&s, " console syslog\n");
344 ds_put_format(&s, " ------- ------\n");
346 for (module = 0; module < VLM_N_MODULES; module++) {
347 ds_put_format(&s, "%-16s %4s %4s\n",
348 vlog_get_module_name(module),
349 vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
350 vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
356 /* Returns true if a log message emitted for the given 'module' and 'level'
357 * would cause some log output, false if that module and level are completely
360 vlog_is_enabled(enum vlog_module module, enum vlog_level level)
362 return (levels[module][VLF_CONSOLE] >= level
363 || levels[module][VLF_SYSLOG] >= level);
367 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
370 size_t n = strcspn(p + 1, "}");
371 size_t n_copy = MIN(n, out_size - 1);
372 memcpy(out, p + 1, n_copy);
376 strlcpy(out, def, out_size);
382 format_log_message(enum vlog_module module, enum vlog_level level,
383 enum vlog_facility facility, unsigned int msg_num,
384 const char *message, va_list args_, struct ds *s)
391 for (p = facilities[facility].pattern; *p != '\0'; ) {
392 enum { LEFT, RIGHT } justify = RIGHT;
394 size_t length, field, used;
397 ds_put_char(s, *p++);
411 while (isdigit(*p)) {
412 field = (field * 10) + (*p - '0');
419 ds_put_cstr(s, program_name);
422 p = fetch_braces(p, "", tmp, sizeof tmp);
423 ds_put_cstr(s, vlog_get_module_name(module));
426 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
427 ds_put_strftime(s, tmp, NULL);
430 va_copy(args, args_);
431 ds_put_format_valist(s, message, args);
435 ds_put_format(s, "%u", msg_num);
438 ds_put_char(s, '\n');
441 ds_put_cstr(s, vlog_get_level_name(level));
444 ds_put_format(s, "%ld", (long int) getpid());
447 ds_put_format(s, "%lld", time_msec() - boot_time);
450 ds_put_char(s, p[-1]);
453 used = s->length - length;
455 size_t n_pad = field - used;
456 if (justify == RIGHT) {
457 ds_put_uninit(s, n_pad);
458 memmove(&s->string[length + n_pad], &s->string[length], used);
459 memset(&s->string[length], pad, n_pad);
461 ds_put_char_multiple(s, pad, n_pad);
467 /* Writes 'message' to the log at the given 'level' and as coming from the
470 * Guaranteed to preserve errno. */
472 vlog_valist(enum vlog_module module, enum vlog_level level,
473 const char *message, va_list args)
475 bool log_console = levels[module][VLF_CONSOLE] >= level;
476 bool log_syslog = levels[module][VLF_SYSLOG] >= level;
477 if (log_console || log_syslog) {
478 int save_errno = errno;
479 static unsigned int msg_num;
483 ds_reserve(&s, 1024);
487 format_log_message(module, level, VLF_CONSOLE, msg_num,
489 ds_put_char(&s, '\n');
490 fputs(ds_cstr(&s), stderr);
494 int syslog_level = syslog_levels[level];
495 char *save_ptr = NULL;
498 format_log_message(module, level, VLF_SYSLOG, msg_num,
500 for (line = strtok_r(s.string, "\n", &save_ptr); line;
501 line = strtok_r(NULL, "\n", &save_ptr)) {
502 syslog(syslog_level, "%s", line);
512 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
516 va_start(args, message);
517 vlog_valist(module, level, message, args);
522 vlog_rate_limit(enum vlog_module module, enum vlog_level level,
523 struct vlog_rate_limit *rl, const char *message, ...)
527 if (!vlog_is_enabled(module, level)) {
531 if (rl->tokens < VLOG_MSG_TOKENS) {
532 time_t now = time_now();
533 if (rl->last_fill > now) {
534 /* Last filled in the future? Time must have gone backward, or
535 * 'rl' has not been used before. */
536 rl->tokens = rl->burst;
537 } else if (rl->last_fill < now) {
538 unsigned int add = sat_mul(rl->rate, now - rl->last_fill);
539 unsigned int tokens = sat_add(rl->tokens, add);
540 rl->tokens = MIN(tokens, rl->burst);
543 if (rl->tokens < VLOG_MSG_TOKENS) {
544 if (!rl->n_dropped) {
545 rl->first_dropped = now;
551 rl->tokens -= VLOG_MSG_TOKENS;
553 va_start(args, message);
554 vlog_valist(module, level, message, args);
559 "Dropped %u messages in last %u seconds due to excessive rate",
560 rl->n_dropped, (unsigned int) (time_now() - rl->first_dropped));