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>
21 #include <vppinfra/elog.h>
23 vlib_log_main_t log_main = {
24 .default_log_level = VLIB_LOG_LEVEL_NOTICE,
25 .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING,
29 .default_rate_limit = 50,
33 VLIB_REGISTER_LOG_CLASS (log_log, static) = {
38 static const int colors[] = {
39 [VLIB_LOG_LEVEL_EMERG] = 1, /* red */
40 [VLIB_LOG_LEVEL_ALERT] = 1, /* red */
41 [VLIB_LOG_LEVEL_CRIT] = 1, /* red */
42 [VLIB_LOG_LEVEL_ERR] = 1, /* red */
43 [VLIB_LOG_LEVEL_WARNING] = 3, /* yellow */
44 [VLIB_LOG_LEVEL_NOTICE] = 2, /* green */
45 [VLIB_LOG_LEVEL_INFO] = 4, /* blue */
46 [VLIB_LOG_LEVEL_DEBUG] = 6, /* cyan */
49 static const int log_level_to_syslog_priority[] = {
50 [VLIB_LOG_LEVEL_EMERG] = LOG_EMERG,
51 [VLIB_LOG_LEVEL_ALERT] = LOG_ALERT,
52 [VLIB_LOG_LEVEL_CRIT] = LOG_CRIT,
53 [VLIB_LOG_LEVEL_ERR] = LOG_ERR,
54 [VLIB_LOG_LEVEL_WARNING] = LOG_WARNING,
55 [VLIB_LOG_LEVEL_NOTICE] = LOG_NOTICE,
56 [VLIB_LOG_LEVEL_INFO] = LOG_INFO,
57 [VLIB_LOG_LEVEL_DEBUG] = LOG_DEBUG,
58 [VLIB_LOG_LEVEL_DISABLED] = LOG_DEBUG,
64 vlib_log_main_t *lm = &log_main;
67 i = lm->next - lm->count;
74 static vlib_log_class_data_t *
75 get_class_data (vlib_log_class_t ci)
77 vlib_log_main_t *lm = &log_main;
78 return vec_elt_at_index (lm->classes, (ci >> 16));
81 static vlib_log_subclass_data_t *
82 get_subclass_data (vlib_log_class_t ci)
84 vlib_log_class_data_t *c = get_class_data (ci);
85 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
89 format_vlib_log_class (u8 * s, va_list * args)
91 vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
92 vlib_log_class_data_t *c = get_class_data (ci);
93 vlib_log_subclass_data_t *sc = get_subclass_data (ci);
96 return format (s, "%v/%v", c->name, sc->name);
98 return format (s, "%v", c->name, 0);
102 format_indent (u8 * s, va_list * args)
104 u8 *v = va_arg (*args, u8 *);
105 u32 indent = va_arg (*args, u32);
113 for (u32 i = 0; i < indent; i++)
114 vec_add1 (s, (u8) ' ');
121 log_level_is_enabled (vlib_log_level_t level, vlib_log_level_t configured)
123 if (configured == VLIB_LOG_LEVEL_DISABLED)
125 if (level > configured)
131 vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
133 vlib_main_t *vm = vlib_get_main ();
134 vlib_log_main_t *lm = &log_main;
136 vlib_log_subclass_data_t *sc = get_subclass_data (class);
138 f64 t = vlib_time_now (vm);
139 f64 delta = t - sc->last_event_timestamp;
140 int log_enabled = log_level_is_enabled (level, sc->level);
141 int syslog_enabled = log_level_is_enabled (level, sc->syslog_level);
144 /* make sure we are running on the main thread to avoid use in dataplane
145 code, for dataplane logging consider use of event-logger */
146 ASSERT (vlib_get_thread_index () == 0);
148 if ((log_enabled || syslog_enabled) == 0)
151 vec_validate (lm->entries, lm->size);
153 if ((delta > lm->unthrottle_time) ||
154 (sc->is_throttling == 0 && (delta > 1)))
156 sc->last_event_timestamp = t;
157 sc->last_sec_count = 0;
158 sc->is_throttling = 0;
162 sc->last_sec_count++;
163 if (sc->last_sec_count > sc->rate_limit)
165 else if (sc->last_sec_count == sc->rate_limit)
167 vec_reset_length (s);
168 s = format (s, "--- message(s) throttled ---");
169 sc->is_throttling = 1;
176 s = va_format (s, fmt, &va);
183 if (unix_main.flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG))
186 int with_colors = (unix_main.flags & UNIX_FLAG_NOCOLOR) == 0;
190 l = format (l, "\x1b[%um", 90 + colors[level]);
191 indent = vec_len (l);
193 fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length);
194 vec_terminate_c_string (fmt);
195 l = format (l, (char *) fmt, format_vlib_log_class, class,
196 format_vlib_log_level, level);
198 indent = vec_len (l) - indent;
200 l = format (l, "\x1b[0m");
201 l = format (l, "%U", format_indent, s, indent);
202 fformat (stderr, "%v\n", l);
207 l = format (l, "%U", format_vlib_log_class, class);
208 int prio = log_level_to_syslog_priority[level];
209 int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
211 syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
212 (int) vec_len (s) - is_term, s);
219 e = vec_elt_at_index (lm->entries, lm->next);
220 vec_free (e->string);
230 ELOG_TYPE_DECLARE(ee) =
232 .format = "log-%s: %s",
233 .format_args = "t4T4",
252 ed = ELOG_DATA (&vlib_global_main.elog_main, ee);
253 ed->log_level = level;
255 elog_string (&vlib_global_main.elog_main, "%v", e->string);
258 lm->next = (lm->next + 1) % lm->size;
259 if (lm->size > lm->count)
266 static vlib_log_class_t
267 vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
269 vlib_log_main_t *lm = &log_main;
270 vlib_log_class_data_t *c = NULL;
271 vlib_log_subclass_data_t *s;
272 vlib_log_class_data_t *tmp;
273 vlib_log_class_config_t *cc = 0, *scc = 0;
278 if ((p = hash_get_mem (lm->config_index_by_name, class)))
279 cc = vec_elt_at_index (lm->configs, p[0]);
281 str = format (0, "%s/%s%c", class, subclass, 0);
282 if ((p = hash_get_mem (lm->config_index_by_name, (char *) str)))
283 scc = vec_elt_at_index (lm->configs, p[0]);
286 vec_foreach (tmp, lm->classes)
288 if (vec_len (tmp->name) != strlen (class))
290 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
298 vec_add2 (lm->classes, c, 1);
299 c->index = c - lm->classes;
300 c->name = format (0, "%s", class);
302 length = vec_len (c->name);
304 vec_add2 (c->subclasses, s, 1);
305 s->index = s - c->subclasses;
306 s->name = subclass ? format (0, "%s", subclass) : 0;
308 if (scc && scc->rate_limit != ~0)
309 s->rate_limit = scc->rate_limit;
310 else if (cc && cc->rate_limit != ~0)
311 s->rate_limit = cc->rate_limit;
313 s->rate_limit = limit;
315 s->rate_limit = lm->default_rate_limit;
317 if (scc && scc->level != ~0)
318 s->level = scc->level;
319 else if (cc && cc->level != ~0)
320 s->level = cc->level;
322 s->level = lm->default_log_level;
324 if (scc && scc->syslog_level != ~0)
325 s->syslog_level = scc->syslog_level;
326 else if (cc && cc->syslog_level != ~0)
327 s->syslog_level = cc->syslog_level;
329 s->syslog_level = lm->default_syslog_log_level;
332 length += 1 + vec_len (s->name);
333 if (length > lm->max_class_name_length)
334 lm->max_class_name_length = length;
335 return (c->index << 16) | (s->index);
339 vlib_log_register_class (char *class, char *subclass)
341 return vlib_log_register_class_internal (class, subclass,
342 0 /* default rate limit */ );
346 vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit)
348 return vlib_log_register_class_internal (class, subclass, limit);
353 format_vlib_log_level (u8 * s, va_list * args)
355 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
360 #define _(uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
361 foreach_vlib_log_level
364 return format (s, "unknown");
366 return format (s, "%s", t);
369 static clib_error_t *
370 vlib_log_init (vlib_main_t * vm)
372 vlib_log_main_t *lm = &log_main;
373 vlib_log_class_registration_t *r = lm->registrations;
375 gettimeofday (&lm->time_zero_timeval, 0);
376 lm->time_zero = vlib_time_now (vm);
378 vec_validate (lm->entries, lm->size);
382 r->class = vlib_log_register_class (r->class_name, r->subclass_name);
383 if (r->default_level)
384 get_subclass_data (r->class)->level = r->default_level;
385 if (r->default_syslog_level)
386 get_subclass_data (r->class)->syslog_level = r->default_syslog_level;
390 r = lm->registrations;
393 vlib_log_debug (r->class, "initialized");
399 VLIB_INIT_FUNCTION (vlib_log_init);
402 static clib_error_t *
403 show_log (vlib_main_t * vm,
404 unformat_input_t * input, vlib_cli_command_t * cmd)
406 clib_error_t *error = 0;
407 vlib_log_main_t *lm = &log_main;
409 int i = last_log_entry ();
410 int count = lm->count;
413 time_offset = (f64) lm->time_zero_timeval.tv_sec
414 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
418 e = vec_elt_at_index (lm->entries, i);
419 vlib_cli_output (vm, "%U %-10U %-14U %v",
420 format_time_float, 0, e->timestamp + time_offset,
421 format_vlib_log_level, e->level,
422 format_vlib_log_class, e->class, e->string);
423 i = (i + 1) % lm->size;
430 VLIB_CLI_COMMAND (cli_show_log, static) = {
431 .path = "show logging",
432 .short_help = "show logging",
433 .function = show_log,
437 static clib_error_t *
438 show_log_config (vlib_main_t * vm,
439 unformat_input_t * input, vlib_cli_command_t * cmd)
441 clib_error_t *error = 0;
442 vlib_log_main_t *lm = &log_main;
443 vlib_log_class_data_t *c;
444 vlib_log_subclass_data_t *sc;
446 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
447 vlib_cli_output (vm, "Defaults:\n");
448 vlib_cli_output (vm, "%-20s %U", " Log Level:",
449 format_vlib_log_level, lm->default_log_level);
450 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
451 format_vlib_log_level, lm->default_syslog_log_level);
452 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
453 lm->default_rate_limit);
454 vlib_cli_output (vm, "\n");
455 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
456 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
459 u8 *defstr = format (0, "default");
460 vec_foreach (c, lm->classes)
462 vlib_cli_output (vm, "%v", c->name);
463 vec_foreach (sc, c->subclasses)
465 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
466 sc->name ? sc->name : defstr,
467 format_vlib_log_level, sc->level,
468 format_vlib_log_level, sc->syslog_level,
478 VLIB_CLI_COMMAND (cli_show_log_config, static) = {
479 .path = "show logging configuration",
480 .short_help = "show logging configuration",
481 .function = show_log_config,
485 static clib_error_t *
486 clear_log (vlib_main_t * vm,
487 unformat_input_t * input, vlib_cli_command_t * cmd)
489 clib_error_t *error = 0;
490 vlib_log_main_t *lm = &log_main;
492 int i = last_log_entry ();
493 int count = lm->count;
497 e = vec_elt_at_index (lm->entries, i);
498 vec_free (e->string);
499 i = (i + 1) % lm->size;
504 vlib_log_info (log_log.class, "log cleared");
509 VLIB_CLI_COMMAND (cli_clear_log, static) = {
510 .path = "clear logging",
511 .short_help = "clear logging",
512 .function = clear_log,
517 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
519 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
520 u8 *level_str = NULL;
522 if (unformat (input, "%s", &level_str))
525 const char __##uc[] = #lc; \
526 if (!strcmp ((const char *) level_str, __##uc)) \
528 *level = VLIB_LOG_LEVEL_##uc; \
532 foreach_vlib_log_level;
537 vec_free (level_str);
542 unformat_vlib_log_class (unformat_input_t * input, va_list * args)
544 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
546 u8 *class_str = NULL;
547 vlib_log_main_t *lm = &log_main;
548 if (unformat (input, "%v", &class_str))
550 vlib_log_class_data_t *cdata;
551 vec_foreach (cdata, lm->classes)
553 if (vec_is_equal (cdata->name, class_str))
561 vec_free (class_str);
565 static clib_error_t *
566 set_log_class (vlib_main_t * vm,
567 unformat_input_t * input, vlib_cli_command_t * cmd)
569 unformat_input_t _line_input, *line_input = &_line_input;
570 clib_error_t *rv = NULL;
572 bool set_rate_limit = false;
573 bool set_level = false;
574 bool set_syslog_level = false;
575 vlib_log_level_t level;
576 vlib_log_level_t syslog_level;
578 /* Get a line of input. */
579 if (!unformat_user (input, unformat_line_input, line_input))
582 vlib_log_class_data_t *class = NULL;
583 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
585 return clib_error_return (0, "unknown log class `%U'",
586 format_unformat_error, line_input);
588 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
590 if (unformat (line_input, "rate-limit %d", &rate_limit))
592 set_rate_limit = true;
596 (line_input, "level %U", unformat_vlib_log_level, &level))
602 (line_input, "syslog-level %U", unformat_vlib_log_level,
605 set_syslog_level = true;
609 return clib_error_return (0, "unknown input `%U'",
610 format_unformat_error, line_input);
616 vlib_log_subclass_data_t *subclass;
617 vec_foreach (subclass, class->subclasses)
619 subclass->level = level;
622 if (set_syslog_level)
624 vlib_log_subclass_data_t *subclass;
625 vec_foreach (subclass, class->subclasses)
627 subclass->syslog_level = syslog_level;
632 vlib_log_subclass_data_t *subclass;
633 vec_foreach (subclass, class->subclasses)
635 subclass->rate_limit = rate_limit;
643 VLIB_CLI_COMMAND (cli_set_log, static) = {
644 .path = "set logging class",
645 .short_help = "set logging class <class> [rate-limit <int>] "
646 "[level <level>] [syslog-level <level>]",
647 .function = set_log_class,
651 static clib_error_t *
652 set_log_unth_time (vlib_main_t * vm,
653 unformat_input_t * input, vlib_cli_command_t * cmd)
655 unformat_input_t _line_input, *line_input = &_line_input;
656 clib_error_t *rv = NULL;
658 vlib_log_main_t *lm = &log_main;
660 /* Get a line of input. */
661 if (!unformat_user (input, unformat_line_input, line_input))
664 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
666 if (unformat (line_input, "%d", &unthrottle_time))
667 lm->unthrottle_time = unthrottle_time;
669 return clib_error_return (0, "unknown input `%U'",
670 format_unformat_error, line_input);
677 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
678 .path = "set logging unthrottle-time",
679 .short_help = "set logging unthrottle-time <int>",
680 .function = set_log_unth_time,
684 static clib_error_t *
685 set_log_size (vlib_main_t * vm,
686 unformat_input_t * input, vlib_cli_command_t * cmd)
688 unformat_input_t _line_input, *line_input = &_line_input;
689 clib_error_t *rv = NULL;
691 vlib_log_main_t *lm = &log_main;
693 /* Get a line of input. */
694 if (!unformat_user (input, unformat_line_input, line_input))
697 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
699 if (unformat (line_input, "%d", &size))
702 vec_validate (lm->entries, lm->size);
705 return clib_error_return (0, "unknown input `%U'",
706 format_unformat_error, line_input);
713 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
714 .path = "set logging size",
715 .short_help = "set logging size <int>",
716 .function = set_log_size,
721 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
723 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
724 vlib_log_subclass_data_t **subclass =
725 va_arg (*args, vlib_log_subclass_data_t **);
727 u8 *subclass_str = NULL;
728 if (unformat (input, "%v", &subclass_str))
730 vlib_log_subclass_data_t *scdata;
731 vec_foreach (scdata, class->subclasses)
733 if (vec_is_equal (scdata->name, subclass_str))
741 vec_free (subclass_str);
745 static clib_error_t *
746 test_log_class_subclass (vlib_main_t * vm,
747 unformat_input_t * input, vlib_cli_command_t * cmd)
749 unformat_input_t _line_input, *line_input = &_line_input;
750 /* Get a line of input. */
751 if (!unformat_user (input, unformat_line_input, line_input))
754 vlib_log_class_data_t *class = NULL;
755 vlib_log_subclass_data_t *subclass = NULL;
756 vlib_log_level_t level;
757 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
759 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
762 (line_input, "%U", unformat_vlib_log_subclass, class,
766 (class->index << 16) | (subclass->index), "%U",
767 format_unformat_input, line_input);
771 return clib_error_return (0,
772 "unknown log subclass near beginning of `%U'",
773 format_unformat_error, line_input);
778 return clib_error_return (0,
779 "unknown log class near beginning of `%U'",
780 format_unformat_error, line_input);
785 return clib_error_return (0, "unknown log level near beginning of `%U'",
786 format_unformat_error, line_input);
792 VLIB_CLI_COMMAND (cli_test_log, static) = {
794 .short_help = "test log <level> <class> <subclass> <message>",
795 .function = test_log_class_subclass,
799 static clib_error_t *
800 log_config_class (vlib_main_t * vm, char *name, unformat_input_t * input)
802 vlib_log_main_t *lm = &log_main;
803 vlib_log_class_config_t *cc, tmp;
806 if (lm->config_index_by_name == 0)
807 lm->config_index_by_name = hash_create_string (0, sizeof (uword));
809 p = hash_get_mem (lm->config_index_by_name, name);
812 return clib_error_return (0, "logging class '%s' already configured",
815 clib_memset_u8 (&tmp, 0xff, sizeof (vlib_log_class_config_t));
817 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
819 if (unformat (input, "level %U", unformat_vlib_log_level, &tmp.level))
821 else if (unformat (input, "syslog-level %U", unformat_vlib_log_level,
824 else if (unformat (input, "rate-limit %u", &tmp.rate_limit))
827 return clib_error_return (0, "unknown input '%U'",
828 format_unformat_error, input);
831 vec_add2 (lm->configs, cc, 1);
832 clib_memcpy_fast (cc, &tmp, sizeof (vlib_log_class_config_t));
834 hash_set_mem (lm->config_index_by_name, name, cc - lm->configs);
838 static clib_error_t *
839 log_config (vlib_main_t * vm, unformat_input_t * input)
841 vlib_log_main_t *lm = &log_main;
842 unformat_input_t sub_input;
845 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
847 if (unformat (input, "size %d", &lm->size))
848 vec_validate (lm->entries, lm->size);
849 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
851 else if (unformat (input, "default-log-level %U",
852 unformat_vlib_log_level, &lm->default_log_level))
854 else if (unformat (input, "default-syslog-log-level %U",
855 unformat_vlib_log_level,
856 &lm->default_syslog_log_level))
858 else if (unformat (input, "add-to-elog"))
860 else if (unformat (input, "class %s %U", &class,
861 unformat_vlib_cli_sub_input, &sub_input))
864 err = log_config_class (vm, (char *) class, &sub_input);
866 unformat_free (&sub_input);
872 return unformat_parse_error (input);
879 VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
882 * fd.io coding-style-patch-verification: ON
885 * eval: (c-set-style "gnu")