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 vec_terminate_c_string (fmt);
191 l = format (l, (char *) fmt, format_vlib_log_class, class,
192 format_vlib_log_level, level);
194 indent = vec_len (l) - indent;
196 l = format (l, "\x1b[0m");
197 l = format (l, "%U", format_indent, s, indent);
198 fformat (stderr, "%v\n", l);
203 l = format (l, "%U", format_vlib_log_class, class);
204 int prio = vlib_log_level_to_syslog_priority (level);
205 int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
207 syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
208 (int) vec_len (s) - is_term, s);
215 e = vec_elt_at_index (lm->entries, lm->next);
216 vec_free (e->string);
223 lm->next = (lm->next + 1) % lm->size;
224 if (lm->size > lm->count)
231 static vlib_log_class_t
232 vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
234 vlib_log_main_t *lm = &log_main;
235 vlib_log_class_data_t *c = NULL;
236 vlib_log_subclass_data_t *s;
237 vlib_log_class_data_t *tmp;
238 vlib_log_class_config_t *cc = 0, *scc = 0;
243 if ((p = hash_get_mem (lm->config_index_by_name, class)))
244 cc = vec_elt_at_index (lm->configs, p[0]);
246 str = format (0, "%s/%s%c", class, subclass, 0);
247 if ((p = hash_get_mem (lm->config_index_by_name, (char *) str)))
248 scc = vec_elt_at_index (lm->configs, p[0]);
251 vec_foreach (tmp, lm->classes)
253 if (vec_len (tmp->name) != strlen (class))
255 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
263 vec_add2 (lm->classes, c, 1);
264 c->index = c - lm->classes;
265 c->name = format (0, "%s", class);
266 length = vec_len (c->name);
269 vec_add2 (c->subclasses, s, 1);
270 s->index = s - c->subclasses;
271 s->name = subclass ? format (0, "%s", subclass) : 0;
273 if (scc && scc->rate_limit != ~0)
274 s->rate_limit = scc->rate_limit;
275 else if (cc && cc->rate_limit != ~0)
276 s->rate_limit = cc->rate_limit;
278 s->rate_limit = limit;
280 s->rate_limit = lm->default_rate_limit;
282 if (scc && scc->level != ~0)
283 s->level = scc->level;
284 else if (cc && cc->level != ~0)
285 s->level = cc->level;
287 s->level = lm->default_log_level;
289 if (scc && scc->syslog_level != ~0)
290 s->syslog_level = scc->syslog_level;
291 else if (cc && cc->syslog_level != ~0)
292 s->syslog_level = cc->syslog_level;
294 s->syslog_level = lm->default_syslog_log_level;
297 length += 1 + vec_len (s->name);
298 if (length > lm->max_class_name_length)
299 lm->max_class_name_length = length;
300 return (c->index << 16) | (s->index);
304 vlib_log_register_class (char *class, char *subclass)
306 return vlib_log_register_class_internal (class, subclass,
307 0 /* default rate limit */ );
311 vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit)
313 return vlib_log_register_class_internal (class, subclass, limit);
318 format_vlib_log_level (u8 * s, va_list * args)
320 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
325 #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
326 foreach_vlib_log_level
329 return format (s, "unknown");
331 return format (s, "%s", t);
334 static clib_error_t *
335 vlib_log_init (vlib_main_t * vm)
337 vlib_log_main_t *lm = &log_main;
339 gettimeofday (&lm->time_zero_timeval, 0);
340 lm->time_zero = vlib_time_now (vm);
342 vec_validate (lm->entries, lm->size);
343 lm->log_class = vlib_log_register_class ("log", 0);
347 VLIB_INIT_FUNCTION (vlib_log_init);
350 static clib_error_t *
351 show_log (vlib_main_t * vm,
352 unformat_input_t * input, vlib_cli_command_t * cmd)
354 clib_error_t *error = 0;
355 vlib_log_main_t *lm = &log_main;
357 int i = last_log_entry ();
358 int count = lm->count;
361 time_offset = (f64) lm->time_zero_timeval.tv_sec
362 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
366 e = vec_elt_at_index (lm->entries, i);
367 vlib_cli_output (vm, "%U %-10U %-14U %v",
368 format_time_float, 0, e->timestamp + time_offset,
369 format_vlib_log_level, e->level,
370 format_vlib_log_class, e->class, e->string);
371 i = (i + 1) % lm->size;
378 VLIB_CLI_COMMAND (cli_show_log, static) = {
379 .path = "show logging",
380 .short_help = "show logging",
381 .function = show_log,
385 static clib_error_t *
386 show_log_config (vlib_main_t * vm,
387 unformat_input_t * input, vlib_cli_command_t * cmd)
389 clib_error_t *error = 0;
390 vlib_log_main_t *lm = &log_main;
391 vlib_log_class_data_t *c;
392 vlib_log_subclass_data_t *sc;
394 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
395 vlib_cli_output (vm, "Defaults:\n");
396 vlib_cli_output (vm, "%-20s %U", " Log Level:",
397 format_vlib_log_level, lm->default_log_level);
398 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
399 format_vlib_log_level, lm->default_syslog_log_level);
400 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
401 lm->default_rate_limit);
402 vlib_cli_output (vm, "\n");
403 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
404 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
407 u8 *defstr = format (0, "default");
408 vec_foreach (c, lm->classes)
410 vlib_cli_output (vm, "%v", c->name);
411 vec_foreach (sc, c->subclasses)
413 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
414 sc->name ? sc->name : defstr,
415 format_vlib_log_level, sc->level,
416 format_vlib_log_level, sc->syslog_level,
426 VLIB_CLI_COMMAND (cli_show_log_config, static) = {
427 .path = "show logging configuration",
428 .short_help = "show logging configuration",
429 .function = show_log_config,
433 static clib_error_t *
434 clear_log (vlib_main_t * vm,
435 unformat_input_t * input, vlib_cli_command_t * cmd)
437 clib_error_t *error = 0;
438 vlib_log_main_t *lm = &log_main;
440 int i = last_log_entry ();
441 int count = lm->count;
445 e = vec_elt_at_index (lm->entries, i);
446 vec_free (e->string);
447 i = (i + 1) % lm->size;
452 vlib_log_info (lm->log_class, "log cleared");
457 VLIB_CLI_COMMAND (cli_clear_log, static) = {
458 .path = "clear logging",
459 .short_help = "clear logging",
460 .function = clear_log,
465 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
467 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
468 u8 *level_str = NULL;
470 if (unformat (input, "%s", &level_str))
472 #define _(v, uc, lc) \
473 const char __##uc[] = #lc; \
474 if (!strcmp ((const char *) level_str, __##uc)) \
476 *level = VLIB_LOG_LEVEL_##uc; \
480 foreach_vlib_log_level;
485 vec_free (level_str);
490 unformat_vlib_log_class (unformat_input_t * input, va_list * args)
492 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
494 u8 *class_str = NULL;
495 vlib_log_main_t *lm = &log_main;
496 if (unformat (input, "%v", &class_str))
498 vlib_log_class_data_t *cdata;
499 vec_foreach (cdata, lm->classes)
501 if (vec_is_equal (cdata->name, class_str))
509 vec_free (class_str);
513 static clib_error_t *
514 set_log_class (vlib_main_t * vm,
515 unformat_input_t * input, vlib_cli_command_t * cmd)
517 unformat_input_t _line_input, *line_input = &_line_input;
518 clib_error_t *rv = NULL;
520 bool set_rate_limit = false;
521 bool set_level = false;
522 bool set_syslog_level = false;
523 vlib_log_level_t level;
524 vlib_log_level_t syslog_level;
526 /* Get a line of input. */
527 if (!unformat_user (input, unformat_line_input, line_input))
530 vlib_log_class_data_t *class = NULL;
531 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
533 return clib_error_return (0, "unknown log class `%U'",
534 format_unformat_error, line_input);
536 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
538 if (unformat (line_input, "rate-limit %d", &rate_limit))
540 set_rate_limit = true;
544 (line_input, "level %U", unformat_vlib_log_level, &level))
550 (line_input, "syslog-level %U", unformat_vlib_log_level,
553 set_syslog_level = true;
557 return clib_error_return (0, "unknown input `%U'",
558 format_unformat_error, line_input);
564 vlib_log_subclass_data_t *subclass;
565 vec_foreach (subclass, class->subclasses)
567 subclass->level = level;
570 if (set_syslog_level)
572 vlib_log_subclass_data_t *subclass;
573 vec_foreach (subclass, class->subclasses)
575 subclass->syslog_level = syslog_level;
580 vlib_log_subclass_data_t *subclass;
581 vec_foreach (subclass, class->subclasses)
583 subclass->rate_limit = rate_limit;
591 VLIB_CLI_COMMAND (cli_set_log, static) = {
592 .path = "set logging class",
593 .short_help = "set logging class <class> [rate-limit <int>] "
594 "[level <level>] [syslog-level <level>]",
595 .function = set_log_class,
599 static clib_error_t *
600 set_log_unth_time (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", &unthrottle_time))
615 lm->unthrottle_time = unthrottle_time;
617 return clib_error_return (0, "unknown input `%U'",
618 format_unformat_error, line_input);
625 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
626 .path = "set logging unthrottle-time",
627 .short_help = "set logging unthrottle-time <int>",
628 .function = set_log_unth_time,
632 static clib_error_t *
633 set_log_size (vlib_main_t * vm,
634 unformat_input_t * input, vlib_cli_command_t * cmd)
636 unformat_input_t _line_input, *line_input = &_line_input;
637 clib_error_t *rv = NULL;
639 vlib_log_main_t *lm = &log_main;
641 /* Get a line of input. */
642 if (!unformat_user (input, unformat_line_input, line_input))
645 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
647 if (unformat (line_input, "%d", &size))
650 vec_validate (lm->entries, lm->size);
653 return clib_error_return (0, "unknown input `%U'",
654 format_unformat_error, line_input);
661 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
662 .path = "set logging size",
663 .short_help = "set logging size <int>",
664 .function = set_log_size,
669 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
671 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
672 vlib_log_subclass_data_t **subclass =
673 va_arg (*args, vlib_log_subclass_data_t **);
675 u8 *subclass_str = NULL;
676 if (unformat (input, "%v", &subclass_str))
678 vlib_log_subclass_data_t *scdata;
679 vec_foreach (scdata, class->subclasses)
681 if (vec_is_equal (scdata->name, subclass_str))
689 vec_free (subclass_str);
693 static clib_error_t *
694 test_log_class_subclass (vlib_main_t * vm,
695 unformat_input_t * input, vlib_cli_command_t * cmd)
697 unformat_input_t _line_input, *line_input = &_line_input;
698 /* Get a line of input. */
699 if (!unformat_user (input, unformat_line_input, line_input))
702 vlib_log_class_data_t *class = NULL;
703 vlib_log_subclass_data_t *subclass = NULL;
704 vlib_log_level_t level;
705 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
707 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
710 (line_input, "%U", unformat_vlib_log_subclass, class,
714 (class->index << 16) | (subclass->index), "%U",
715 format_unformat_input, line_input);
719 return clib_error_return (0,
720 "unknown log subclass near beginning of `%U'",
721 format_unformat_error, line_input);
726 return clib_error_return (0,
727 "unknown log class near beginning of `%U'",
728 format_unformat_error, line_input);
733 return clib_error_return (0, "unknown log level near beginning of `%U'",
734 format_unformat_error, line_input);
740 VLIB_CLI_COMMAND (cli_test_log, static) = {
742 .short_help = "test log <level> <class> <subclass> <message>",
743 .function = test_log_class_subclass,
747 static clib_error_t *
748 log_config_class (vlib_main_t * vm, char *name, unformat_input_t * input)
750 vlib_log_main_t *lm = &log_main;
751 vlib_log_class_config_t *cc, tmp;
754 if (lm->config_index_by_name == 0)
755 lm->config_index_by_name = hash_create_string (0, sizeof (uword));
757 p = hash_get_mem (lm->config_index_by_name, name);
760 return clib_error_return (0, "logging class '%s' already configured",
763 clib_memset_u8 (&tmp, 0xff, sizeof (vlib_log_class_config_t));
765 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
767 if (unformat (input, "level %U", unformat_vlib_log_level, &tmp.level))
769 else if (unformat (input, "syslog-level %U", unformat_vlib_log_level,
772 else if (unformat (input, "rate-limit %u", &tmp.rate_limit))
775 return clib_error_return (0, "unknown input '%U'",
776 format_unformat_error, input);
779 vec_add2 (lm->configs, cc, 1);
780 clib_memcpy_fast (cc, &tmp, sizeof (vlib_log_class_config_t));
782 hash_set_mem (lm->config_index_by_name, name, cc - lm->configs);
786 static clib_error_t *
787 log_config (vlib_main_t * vm, unformat_input_t * input)
789 vlib_log_main_t *lm = &log_main;
790 unformat_input_t sub_input;
793 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
795 if (unformat (input, "size %d", &lm->size))
796 vec_validate (lm->entries, lm->size);
797 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
799 else if (unformat (input, "default-log-level %U",
800 unformat_vlib_log_level, &lm->default_log_level))
802 else if (unformat (input, "default-syslog-log-level %U",
803 unformat_vlib_log_level,
804 &lm->default_syslog_log_level))
806 else if (unformat (input, "class %s %U", &class,
807 unformat_vlib_cli_sub_input, &sub_input))
810 err = log_config_class (vm, (char *) class, &sub_input);
812 unformat_free (&sub_input);
818 return unformat_parse_error (input);
825 VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
828 * fd.io coding-style-patch-verification: ON
831 * eval: (c-set-style "gnu")