2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
22 vlib_log_main_t log_main = {
23 .default_log_level = VLIB_LOG_LEVEL_NOTICE,
24 .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING,
27 .default_rate_limit = 50,
30 static const int colors[] = {
31 [VLIB_LOG_LEVEL_EMERG] = 1, /* red */
32 [VLIB_LOG_LEVEL_ALERT] = 1, /* red */
33 [VLIB_LOG_LEVEL_CRIT] = 1, /* red */
34 [VLIB_LOG_LEVEL_ERR] = 1, /* red */
35 [VLIB_LOG_LEVEL_WARNING] = 3, /* yellow */
36 [VLIB_LOG_LEVEL_NOTICE] = 2, /* green */
37 [VLIB_LOG_LEVEL_INFO] = 4, /* blue */
38 [VLIB_LOG_LEVEL_DEBUG] = 6, /* cyan */
44 vlib_log_main_t *lm = &log_main;
47 i = lm->next - lm->count;
54 static vlib_log_class_data_t *
55 get_class_data (vlib_log_class_t ci)
57 vlib_log_main_t *lm = &log_main;
58 return vec_elt_at_index (lm->classes, (ci >> 16));
61 static vlib_log_subclass_data_t *
62 get_subclass_data (vlib_log_class_t ci)
64 vlib_log_class_data_t *c = get_class_data (ci);
65 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
69 vlib_log_level_to_syslog_priority (vlib_log_level_t level)
73 #define LOG_DISABLED LOG_DEBUG
75 case VLIB_LOG_LEVEL_##uc:\
77 foreach_vlib_log_level
85 format_vlib_log_class (u8 * s, va_list * args)
87 vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
88 vlib_log_class_data_t *c = get_class_data (ci);
89 vlib_log_subclass_data_t *sc = get_subclass_data (ci);
92 return format (s, "%v/%v", c->name, sc->name);
94 return format (s, "%v", c->name, 0);
98 format_indent (u8 * s, va_list * args)
100 u8 *v = va_arg (*args, u8 *);
101 u32 indent = va_arg (*args, u32);
109 for (u32 i = 0; i < indent; i++)
110 vec_add1 (s, (u8) ' ');
117 log_level_is_enabled (vlib_log_level_t level, vlib_log_level_t configured)
119 if (configured == VLIB_LOG_LEVEL_DISABLED)
121 if (level > configured)
127 vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
129 vlib_main_t *vm = vlib_get_main ();
130 vlib_log_main_t *lm = &log_main;
132 vlib_log_subclass_data_t *sc = get_subclass_data (class);
134 f64 t = vlib_time_now (vm);
135 f64 delta = t - sc->last_event_timestamp;
136 int log_enabled = log_level_is_enabled (level, sc->level);
137 int syslog_enabled = log_level_is_enabled (level, sc->syslog_level);
140 /* make sure we are running on the main thread to avoid use in dataplane
141 code, for dataplane logging consider use of event-logger */
142 ASSERT (vlib_get_thread_index () == 0);
144 if ((log_enabled || syslog_enabled) == 0)
147 vec_validate (lm->entries, lm->size);
149 if ((delta > lm->unthrottle_time) ||
150 (sc->is_throttling == 0 && (delta > 1)))
152 sc->last_event_timestamp = t;
153 sc->last_sec_count = 0;
154 sc->is_throttling = 0;
158 sc->last_sec_count++;
159 if (sc->last_sec_count > sc->rate_limit)
161 else if (sc->last_sec_count == sc->rate_limit)
163 vec_reset_length (s);
164 s = format (s, "--- message(s) throttled ---");
165 sc->is_throttling = 1;
172 s = va_format (s, fmt, &va);
179 if (unix_main.flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG))
182 int with_colors = (unix_main.flags & UNIX_FLAG_NOCOLOR) == 0;
186 l = format (l, "\x1b[%um", 90 + colors[level]);
187 indent = vec_len (l);
189 fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length);
190 l = format (l, (char *) fmt, format_vlib_log_class, class,
191 format_vlib_log_level, level);
193 indent = vec_len (l) - indent;
195 l = format (l, "\x1b[0m");
196 l = format (l, "%U", format_indent, s, indent);
197 fformat (stderr, "%v\n", l);
202 l = format (l, "%U", format_vlib_log_class, class);
203 int prio = vlib_log_level_to_syslog_priority (level);
204 int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
206 syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
207 (int) vec_len (s) - is_term, s);
214 e = vec_elt_at_index (lm->entries, lm->next);
215 vec_free (e->string);
222 lm->next = (lm->next + 1) % lm->size;
223 if (lm->size > lm->count)
230 static vlib_log_class_t
231 vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
233 vlib_log_main_t *lm = &log_main;
234 vlib_log_class_data_t *c = NULL;
235 vlib_log_subclass_data_t *s;
236 vlib_log_class_data_t *tmp;
239 vec_foreach (tmp, lm->classes)
241 if (vec_len (tmp->name) != strlen (class))
243 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
251 vec_add2 (lm->classes, c, 1);
252 c->index = c - lm->classes;
253 c->name = format (0, "%s", class);
254 length = vec_len (c->name);
257 vec_add2 (c->subclasses, s, 1);
258 s->index = s - c->subclasses;
259 s->name = subclass ? format (0, "%s", subclass) : 0;
260 s->rate_limit = (limit == 0) ? lm->default_rate_limit : limit;
261 s->level = lm->default_log_level;
262 s->syslog_level = lm->default_syslog_log_level;
264 length += 1 + vec_len (s->name);
265 if (length > lm->max_class_name_length)
266 lm->max_class_name_length = length;
267 return (c->index << 16) | (s->index);
271 vlib_log_register_class (char *class, char *subclass)
273 return vlib_log_register_class_internal (class, subclass,
274 0 /* default rate limit */ );
278 vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit)
280 return vlib_log_register_class_internal (class, subclass, limit);
285 format_vlib_log_level (u8 * s, va_list * args)
287 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
292 #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
293 foreach_vlib_log_level
296 return format (s, "unknown");
298 return format (s, "%s", t);
301 static clib_error_t *
302 vlib_log_init (vlib_main_t * vm)
304 vlib_log_main_t *lm = &log_main;
306 gettimeofday (&lm->time_zero_timeval, 0);
307 lm->time_zero = vlib_time_now (vm);
309 vec_validate (lm->entries, lm->size);
310 lm->log_class = vlib_log_register_class ("log", 0);
314 VLIB_INIT_FUNCTION (vlib_log_init);
317 static clib_error_t *
318 show_log (vlib_main_t * vm,
319 unformat_input_t * input, vlib_cli_command_t * cmd)
321 clib_error_t *error = 0;
322 vlib_log_main_t *lm = &log_main;
324 int i = last_log_entry ();
325 int count = lm->count;
328 time_offset = (f64) lm->time_zero_timeval.tv_sec
329 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
333 e = vec_elt_at_index (lm->entries, i);
334 vlib_cli_output (vm, "%U %-10U %-14U %v",
335 format_time_float, 0, e->timestamp + time_offset,
336 format_vlib_log_level, e->level,
337 format_vlib_log_class, e->class, e->string);
338 i = (i + 1) % lm->size;
345 VLIB_CLI_COMMAND (cli_show_log, static) = {
346 .path = "show logging",
347 .short_help = "show logging",
348 .function = show_log,
352 static clib_error_t *
353 show_log_config (vlib_main_t * vm,
354 unformat_input_t * input, vlib_cli_command_t * cmd)
356 clib_error_t *error = 0;
357 vlib_log_main_t *lm = &log_main;
358 vlib_log_class_data_t *c;
359 vlib_log_subclass_data_t *sc;
361 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
362 vlib_cli_output (vm, "Defaults:\n");
363 vlib_cli_output (vm, "%-20s %U", " Log Level:",
364 format_vlib_log_level, lm->default_log_level);
365 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
366 format_vlib_log_level, lm->default_syslog_log_level);
367 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
368 lm->default_rate_limit);
369 vlib_cli_output (vm, "\n");
370 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
371 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
374 u8 *defstr = format (0, "default");
375 vec_foreach (c, lm->classes)
377 vlib_cli_output (vm, "%v", c->name);
378 vec_foreach (sc, c->subclasses)
380 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
381 sc->name ? sc->name : defstr,
382 format_vlib_log_level, sc->level,
383 format_vlib_log_level, sc->syslog_level,
393 VLIB_CLI_COMMAND (cli_show_log_config, static) = {
394 .path = "show logging configuration",
395 .short_help = "show logging configuration",
396 .function = show_log_config,
400 static clib_error_t *
401 clear_log (vlib_main_t * vm,
402 unformat_input_t * input, vlib_cli_command_t * cmd)
404 clib_error_t *error = 0;
405 vlib_log_main_t *lm = &log_main;
407 int i = last_log_entry ();
408 int count = lm->count;
412 e = vec_elt_at_index (lm->entries, i);
413 vec_free (e->string);
414 i = (i + 1) % lm->size;
419 vlib_log_info (lm->log_class, "log cleared");
424 VLIB_CLI_COMMAND (cli_clear_log, static) = {
425 .path = "clear logging",
426 .short_help = "clear logging",
427 .function = clear_log,
432 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
434 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
435 u8 *level_str = NULL;
437 if (unformat (input, "%s", &level_str))
439 #define _(v, uc, lc) \
440 const char __##uc[] = #lc; \
441 if (!strcmp ((const char *) level_str, __##uc)) \
443 *level = VLIB_LOG_LEVEL_##uc; \
447 foreach_vlib_log_level;
452 vec_free (level_str);
457 unformat_vlib_log_class (unformat_input_t * input, va_list * args)
459 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
461 u8 *class_str = NULL;
462 vlib_log_main_t *lm = &log_main;
463 if (unformat (input, "%v", &class_str))
465 vlib_log_class_data_t *cdata;
466 vec_foreach (cdata, lm->classes)
468 if (vec_is_equal (cdata->name, class_str))
476 vec_free (class_str);
480 static clib_error_t *
481 set_log_class (vlib_main_t * vm,
482 unformat_input_t * input, vlib_cli_command_t * cmd)
484 unformat_input_t _line_input, *line_input = &_line_input;
485 clib_error_t *rv = NULL;
487 bool set_rate_limit = false;
488 bool set_level = false;
489 bool set_syslog_level = false;
490 vlib_log_level_t level;
491 vlib_log_level_t syslog_level;
493 /* Get a line of input. */
494 if (!unformat_user (input, unformat_line_input, line_input))
497 vlib_log_class_data_t *class = NULL;
498 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
500 return clib_error_return (0, "unknown log class `%U'",
501 format_unformat_error, line_input);
503 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
505 if (unformat (line_input, "rate-limit %d", &rate_limit))
507 set_rate_limit = true;
511 (line_input, "level %U", unformat_vlib_log_level, &level))
517 (line_input, "syslog-level %U", unformat_vlib_log_level,
520 set_syslog_level = true;
524 return clib_error_return (0, "unknown input `%U'",
525 format_unformat_error, line_input);
531 vlib_log_subclass_data_t *subclass;
532 vec_foreach (subclass, class->subclasses)
534 subclass->level = level;
537 if (set_syslog_level)
539 vlib_log_subclass_data_t *subclass;
540 vec_foreach (subclass, class->subclasses)
542 subclass->syslog_level = syslog_level;
547 vlib_log_subclass_data_t *subclass;
548 vec_foreach (subclass, class->subclasses)
550 subclass->rate_limit = rate_limit;
558 VLIB_CLI_COMMAND (cli_set_log, static) = {
559 .path = "set logging class",
560 .short_help = "set logging class <class> [rate-limit <int>] "
561 "[level <level>] [syslog-level <level>]",
562 .function = set_log_class,
566 static clib_error_t *
567 set_log_unth_time (vlib_main_t * vm,
568 unformat_input_t * input, vlib_cli_command_t * cmd)
570 unformat_input_t _line_input, *line_input = &_line_input;
571 clib_error_t *rv = NULL;
573 vlib_log_main_t *lm = &log_main;
575 /* Get a line of input. */
576 if (!unformat_user (input, unformat_line_input, line_input))
579 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
581 if (unformat (line_input, "%d", &unthrottle_time))
582 lm->unthrottle_time = unthrottle_time;
584 return clib_error_return (0, "unknown input `%U'",
585 format_unformat_error, line_input);
592 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
593 .path = "set logging unthrottle-time",
594 .short_help = "set logging unthrottle-time <int>",
595 .function = set_log_unth_time,
599 static clib_error_t *
600 set_log_size (vlib_main_t * vm,
601 unformat_input_t * input, vlib_cli_command_t * cmd)
603 unformat_input_t _line_input, *line_input = &_line_input;
604 clib_error_t *rv = NULL;
606 vlib_log_main_t *lm = &log_main;
608 /* Get a line of input. */
609 if (!unformat_user (input, unformat_line_input, line_input))
612 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
614 if (unformat (line_input, "%d", &size))
617 vec_validate (lm->entries, lm->size);
620 return clib_error_return (0, "unknown input `%U'",
621 format_unformat_error, line_input);
628 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
629 .path = "set logging size",
630 .short_help = "set logging size <int>",
631 .function = set_log_size,
636 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
638 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
639 vlib_log_subclass_data_t **subclass =
640 va_arg (*args, vlib_log_subclass_data_t **);
642 u8 *subclass_str = NULL;
643 if (unformat (input, "%v", &subclass_str))
645 vlib_log_subclass_data_t *scdata;
646 vec_foreach (scdata, class->subclasses)
648 if (vec_is_equal (scdata->name, subclass_str))
656 vec_free (subclass_str);
660 static clib_error_t *
661 test_log_class_subclass (vlib_main_t * vm,
662 unformat_input_t * input, vlib_cli_command_t * cmd)
664 unformat_input_t _line_input, *line_input = &_line_input;
665 /* Get a line of input. */
666 if (!unformat_user (input, unformat_line_input, line_input))
669 vlib_log_class_data_t *class = NULL;
670 vlib_log_subclass_data_t *subclass = NULL;
671 vlib_log_level_t level;
672 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
674 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
677 (line_input, "%U", unformat_vlib_log_subclass, class,
681 (class->index << 16) | (subclass->index), "%U",
682 format_unformat_input, line_input);
686 return clib_error_return (0,
687 "unknown log subclass near beginning of `%U'",
688 format_unformat_error, line_input);
693 return clib_error_return (0,
694 "unknown log class near beginning of `%U'",
695 format_unformat_error, line_input);
700 return clib_error_return (0, "unknown log level near beginning of `%U'",
701 format_unformat_error, line_input);
707 VLIB_CLI_COMMAND (cli_test_log, static) = {
709 .short_help = "test log <level> <class> <subclass> <message>",
710 .function = test_log_class_subclass,
714 static clib_error_t *
715 log_config (vlib_main_t * vm, unformat_input_t * input)
717 vlib_log_main_t *lm = &log_main;
719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
721 if (unformat (input, "size %d", &lm->size))
722 vec_validate (lm->entries, lm->size);
723 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
725 else if (unformat (input, "default-log-level %U",
726 unformat_vlib_log_level, &lm->default_log_level))
728 else if (unformat (input, "default-syslog-log-level %U",
729 unformat_vlib_log_level,
730 &lm->default_syslog_log_level))
734 return unformat_parse_error (input);
741 VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
744 * fd.io coding-style-patch-verification: ON
747 * eval: (c-set-style "gnu")