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 /* For fast checking whether we're logging anything for a given module and
91 enum vlog_level min_vlog_levels[VLM_N_MODULES];
93 /* Time at which vlog was initialized, in milliseconds. */
94 static long long int boot_time;
96 /* Searches the 'n_names' in 'names'. Returns the index of a match for
97 * 'target', or 'n_names' if no name matches. */
99 search_name_array(const char *target, const char **names, size_t n_names)
103 for (i = 0; i < n_names; i++) {
105 if (!strcasecmp(names[i], target)) {
112 /* Returns the name for logging level 'level'. */
114 vlog_get_level_name(enum vlog_level level)
116 assert(level < VLL_N_LEVELS);
117 return level_names[level];
120 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
121 * is not the name of a logging level. */
123 vlog_get_level_val(const char *name)
125 return search_name_array(name, level_names, ARRAY_SIZE(level_names));
128 /* Returns the name for logging facility 'facility'. */
130 vlog_get_facility_name(enum vlog_facility facility)
132 assert(facility < VLF_N_FACILITIES);
133 return facilities[facility].name;
136 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
137 * not the name of a logging facility. */
139 vlog_get_facility_val(const char *name)
143 for (i = 0; i < VLF_N_FACILITIES; i++) {
144 if (!strcasecmp(facilities[i].name, name)) {
151 /* Returns the name for logging module 'module'. */
152 const char *vlog_get_module_name(enum vlog_module module)
154 assert(module < VLM_N_MODULES);
155 return module_names[module];
158 /* Returns the logging module named 'name', or VLM_N_MODULES if 'name' is not
159 * the name of a logging module. */
161 vlog_get_module_val(const char *name)
163 return search_name_array(name, module_names, ARRAY_SIZE(module_names));
166 /* Returns the current logging level for the given 'module' and 'facility'. */
168 vlog_get_level(enum vlog_module module, enum vlog_facility facility)
170 assert(module < VLM_N_MODULES);
171 assert(facility < VLF_N_FACILITIES);
172 return levels[module][facility];
176 update_min_level(enum vlog_module module)
178 enum vlog_level min_level = VLL_EMER;
179 enum vlog_facility facility;
181 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
182 min_level = MAX(min_level, levels[module][facility]);
184 min_vlog_levels[module] = min_level;
188 set_facility_level(enum vlog_facility facility, enum vlog_module module,
189 enum vlog_level level)
191 assert(facility >= 0 && facility < VLF_N_FACILITIES);
192 assert(level < VLL_N_LEVELS);
194 if (module == VLM_ANY_MODULE) {
195 for (module = 0; module < VLM_N_MODULES; module++) {
196 levels[module][facility] = level;
197 update_min_level(module);
200 levels[module][facility] = level;
201 update_min_level(module);
205 /* Sets the logging level for the given 'module' and 'facility' to 'level'. */
207 vlog_set_levels(enum vlog_module module, enum vlog_facility facility,
208 enum vlog_level level)
210 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
211 if (facility == VLF_ANY_FACILITY) {
212 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
213 set_facility_level(facility, module, level);
216 set_facility_level(facility, module, level);
221 do_set_pattern(enum vlog_facility facility, const char *pattern)
223 struct facility *f = &facilities[facility];
224 if (!f->default_pattern) {
227 f->default_pattern = false;
229 f->pattern = xstrdup(pattern);
232 /* Sets the pattern for the given 'facility' to 'pattern'. */
234 vlog_set_pattern(enum vlog_facility facility, const char *pattern)
236 assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
237 if (facility == VLF_ANY_FACILITY) {
238 for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
239 do_set_pattern(facility, pattern);
242 do_set_pattern(facility, pattern);
246 /* Set debugging levels:
248 * mod[:facility[:level]] mod2[:facility[:level]] ...
250 * Return null if successful, otherwise an error message that the caller must
254 vlog_set_levels_from_string(const char *s_)
257 char *s = xstrdup(s_);
258 char *module, *facility;
260 for (module = strtok_r(s, ": \t", &save_ptr); module != NULL;
261 module = strtok_r(NULL, ": \t", &save_ptr)) {
262 enum vlog_module e_module;
263 enum vlog_facility e_facility;
265 facility = strtok_r(NULL, ":", &save_ptr);
267 if (!facility || !strcmp(facility, "ANY")) {
268 e_facility = VLF_ANY_FACILITY;
270 e_facility = vlog_get_facility_val(facility);
271 if (e_facility >= VLF_N_FACILITIES) {
272 char *msg = xasprintf("unknown facility \"%s\"", facility);
278 if (!strcmp(module, "PATTERN")) {
279 vlog_set_pattern(e_facility, save_ptr);
283 enum vlog_level e_level;
285 if (!strcmp(module, "ANY")) {
286 e_module = VLM_ANY_MODULE;
288 e_module = vlog_get_module_val(module);
289 if (e_module >= VLM_N_MODULES) {
290 char *msg = xasprintf("unknown module \"%s\"", module);
296 level = strtok_r(NULL, ":", &save_ptr);
297 e_level = level ? vlog_get_level_val(level) : VLL_DBG;
298 if (e_level >= VLL_N_LEVELS) {
299 char *msg = xasprintf("unknown level \"%s\"", level);
304 vlog_set_levels(e_module, e_facility, e_level);
311 /* If 'arg' is null, configure maximum verbosity. Otherwise, sets
312 * configuration according to 'arg' (see vlog_set_levels_from_string()). */
314 vlog_set_verbosity(const char *arg)
317 char *msg = vlog_set_levels_from_string(arg);
319 ofp_fatal(0, "processing \"%s\": %s", arg, msg);
322 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_DBG);
326 /* Initializes the logging subsystem. */
332 openlog(program_name, LOG_NDELAY, LOG_DAEMON);
333 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_WARN);
335 boot_time = time_msec();
341 localtime_r(&now, &tm);
342 strftime(s, sizeof s, "%a, %d %b %Y %H:%M:%S %z", &tm);
343 VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now);
347 /* Closes the logging subsystem. */
354 /* Print the current logging level for each module. */
356 vlog_get_levels(void)
358 struct ds s = DS_EMPTY_INITIALIZER;
359 enum vlog_module module;
361 ds_put_format(&s, " console syslog\n");
362 ds_put_format(&s, " ------- ------\n");
364 for (module = 0; module < VLM_N_MODULES; module++) {
365 ds_put_format(&s, "%-16s %4s %4s\n",
366 vlog_get_module_name(module),
367 vlog_get_level_name(vlog_get_level(module, VLF_CONSOLE)),
368 vlog_get_level_name(vlog_get_level(module, VLF_SYSLOG)));
374 /* Returns true if a log message emitted for the given 'module' and 'level'
375 * would cause some log output, false if that module and level are completely
378 vlog_is_enabled(enum vlog_module module, enum vlog_level level)
380 return min_vlog_levels[module] >= level;
384 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
387 size_t n = strcspn(p + 1, "}");
388 size_t n_copy = MIN(n, out_size - 1);
389 memcpy(out, p + 1, n_copy);
393 strlcpy(out, def, out_size);
399 format_log_message(enum vlog_module module, enum vlog_level level,
400 enum vlog_facility facility, unsigned int msg_num,
401 const char *message, va_list args_, struct ds *s)
408 for (p = facilities[facility].pattern; *p != '\0'; ) {
409 enum { LEFT, RIGHT } justify = RIGHT;
411 size_t length, field, used;
414 ds_put_char(s, *p++);
428 while (isdigit(*p)) {
429 field = (field * 10) + (*p - '0');
436 ds_put_cstr(s, program_name);
439 p = fetch_braces(p, "", tmp, sizeof tmp);
440 ds_put_cstr(s, vlog_get_module_name(module));
443 p = fetch_braces(p, "%Y-%m-%d %H:%M:%S", tmp, sizeof tmp);
444 ds_put_strftime(s, tmp, NULL);
447 va_copy(args, args_);
448 ds_put_format_valist(s, message, args);
452 ds_put_format(s, "%u", msg_num);
455 ds_put_char(s, '\n');
458 ds_put_cstr(s, vlog_get_level_name(level));
461 ds_put_format(s, "%ld", (long int) getpid());
464 ds_put_format(s, "%lld", time_msec() - boot_time);
467 ds_put_char(s, p[-1]);
470 used = s->length - length;
472 size_t n_pad = field - used;
473 if (justify == RIGHT) {
474 ds_put_uninit(s, n_pad);
475 memmove(&s->string[length + n_pad], &s->string[length], used);
476 memset(&s->string[length], pad, n_pad);
478 ds_put_char_multiple(s, pad, n_pad);
484 /* Writes 'message' to the log at the given 'level' and as coming from the
487 * Guaranteed to preserve errno. */
489 vlog_valist(enum vlog_module module, enum vlog_level level,
490 const char *message, va_list args)
492 bool log_console = levels[module][VLF_CONSOLE] >= level;
493 bool log_syslog = levels[module][VLF_SYSLOG] >= level;
494 if (log_console || log_syslog) {
495 int save_errno = errno;
496 static unsigned int msg_num;
500 ds_reserve(&s, 1024);
504 format_log_message(module, level, VLF_CONSOLE, msg_num,
506 ds_put_char(&s, '\n');
507 fputs(ds_cstr(&s), stderr);
511 int syslog_level = syslog_levels[level];
512 char *save_ptr = NULL;
515 format_log_message(module, level, VLF_SYSLOG, msg_num,
517 for (line = strtok_r(s.string, "\n", &save_ptr); line;
518 line = strtok_r(NULL, "\n", &save_ptr)) {
519 syslog(syslog_level, "%s", line);
529 vlog(enum vlog_module module, enum vlog_level level, const char *message, ...)
533 va_start(args, message);
534 vlog_valist(module, level, message, args);
539 vlog_rate_limit(enum vlog_module module, enum vlog_level level,
540 struct vlog_rate_limit *rl, const char *message, ...)
544 if (!vlog_is_enabled(module, level)) {
548 if (rl->tokens < VLOG_MSG_TOKENS) {
549 time_t now = time_now();
550 if (rl->last_fill > now) {
551 /* Last filled in the future? Time must have gone backward, or
552 * 'rl' has not been used before. */
553 rl->tokens = rl->burst;
554 } else if (rl->last_fill < now) {
555 unsigned int add = sat_mul(rl->rate, now - rl->last_fill);
556 unsigned int tokens = sat_add(rl->tokens, add);
557 rl->tokens = MIN(tokens, rl->burst);
560 if (rl->tokens < VLOG_MSG_TOKENS) {
561 if (!rl->n_dropped) {
562 rl->first_dropped = now;
568 rl->tokens -= VLOG_MSG_TOKENS;
570 va_start(args, message);
571 vlog_valist(module, level, message, args);
576 "Dropped %u messages in last %u seconds due to excessive rate",
577 rl->n_dropped, (unsigned int) (time_now() - rl->first_dropped));