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>
23 vlib_log_level_t level;
24 vlib_log_class_t class;
33 // level of log messages kept for this subclass
34 vlib_log_level_t level;
35 // level of log messages sent to syslog for this subclass
36 vlib_log_level_t syslog_level;
37 // flag saying whether this subclass is logged to syslog
38 f64 last_event_timestamp;
42 } vlib_log_subclass_data_t;
48 vlib_log_subclass_data_t *subclasses;
49 } vlib_log_class_data_t;
53 vlib_log_entry_t *entries;
54 vlib_log_class_data_t *classes;
55 int size, next, count;
57 /* our own log class */
58 vlib_log_class_t log_class;
60 int default_rate_limit;
61 int default_log_level;
62 int default_syslog_log_level;
67 vlib_log_main_t log_main = {
68 .default_log_level = VLIB_LOG_LEVEL_NOTICE,
69 .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING,
72 .default_rate_limit = 50,
78 vlib_log_main_t *lm = &log_main;
81 i = lm->next - lm->count;
88 static vlib_log_class_data_t *
89 get_class_data (vlib_log_class_t ci)
91 vlib_log_main_t *lm = &log_main;
92 return vec_elt_at_index (lm->classes, (ci >> 16));
95 static vlib_log_subclass_data_t *
96 get_subclass_data (vlib_log_class_t ci)
98 vlib_log_class_data_t *c = get_class_data (ci);
99 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
103 vlib_log_level_to_syslog_priority (vlib_log_level_t level)
107 #define LOG_DISABLED LOG_DEBUG
109 case VLIB_LOG_LEVEL_##uc:\
111 foreach_vlib_log_level
119 format_vlib_log_class (u8 * s, va_list * args)
121 vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
122 vlib_log_class_data_t *c = get_class_data (ci);
123 vlib_log_subclass_data_t *sc = get_subclass_data (ci);
126 return format (s, "%v/%v", c->name, sc->name);
128 return format (s, "%v", c->name, 0);
133 vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
135 vlib_main_t *vm = vlib_get_main ();
136 vlib_log_main_t *lm = &log_main;
138 vlib_log_subclass_data_t *sc = get_subclass_data (class);
140 f64 t = vlib_time_now (vm);
141 f64 delta = t - sc->last_event_timestamp;
143 bool use_formatted_log_entry = true;
145 /* make sure we are running on the main thread to avoid use in dataplane
146 code, for dataplane logging consider use of event-logger */
147 ASSERT (vlib_get_thread_index () == 0);
149 if (level > sc->level)
151 use_formatted_log_entry = false;
155 if ((delta > lm->unthrottle_time) ||
156 (sc->is_throttling == 0 && (delta > 1)))
158 sc->last_event_timestamp = t;
159 sc->last_sec_count = 0;
160 sc->is_throttling = 0;
164 sc->last_sec_count++;
165 if (sc->last_sec_count > sc->rate_limit)
167 else if (sc->last_sec_count == sc->rate_limit)
169 vec_reset_length (s);
170 s = format (0, "--- message(s) throttled ---");
171 sc->is_throttling = 1;
178 s = va_format (s, fmt, &va);
182 e = vec_elt_at_index (lm->entries, lm->next);
183 vec_free (e->string);
189 lm->next = (lm->next + 1) % lm->size;
190 if (lm->size > lm->count)
194 if (sc->syslog_level != VLIB_LOG_LEVEL_DISABLED &&
195 level <= sc->syslog_level)
197 u8 *tmp = format (NULL, "%U", format_vlib_log_class, class);
198 if (use_formatted_log_entry)
200 syslog (vlib_log_level_to_syslog_priority (level), "%.*s: %.*s",
202 vec_len (s) - (vec_c_string_is_terminated (s) ? 1 : 0), s);
206 tmp = format (tmp, ": ");
208 tmp = va_format (tmp, fmt, &va);
210 syslog (vlib_log_level_to_syslog_priority (level), "%.*s",
211 vec_len (tmp) - (vec_c_string_is_terminated (tmp) ? 1 : 0),
220 vlib_log_register_class (char *class, char *subclass)
222 vlib_log_main_t *lm = &log_main;
223 vlib_log_class_data_t *c = NULL;
224 vlib_log_subclass_data_t *s;
225 vlib_log_class_data_t *tmp;
226 vec_foreach (tmp, lm->classes)
228 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
236 vec_add2 (lm->classes, c, 1);
237 c->index = c - lm->classes;
238 c->name = format (0, "%s", class);
241 vec_add2 (c->subclasses, s, 1);
242 s->index = s - c->subclasses;
243 s->name = subclass ? format (0, "%s", subclass) : 0;
244 s->rate_limit = lm->default_rate_limit;
245 s->level = lm->default_log_level;
246 s->syslog_level = lm->default_syslog_log_level;
247 return (c->index << 16) | (s->index);
251 format_vlib_log_level (u8 * s, va_list * args)
253 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
258 #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
259 foreach_vlib_log_level
262 return format (s, "unknown");
264 return format (s, "%s", t);
268 vlib_log_get_indent ()
270 return log_main.indent;
273 static clib_error_t *
274 vlib_log_init (vlib_main_t * vm)
276 vlib_log_main_t *lm = &log_main;
277 vec_validate (lm->entries, lm->size);
278 lm->log_class = vlib_log_register_class ("log", 0);
279 u8 *tmp = format (NULL, "%U %-10U %-10U ", format_time_float, 0, (f64) 0,
280 format_white_space, 255, format_white_space, 255);
281 log_main.indent = vec_len (tmp);
286 VLIB_INIT_FUNCTION (vlib_log_init);
289 static clib_error_t *
290 show_log (vlib_main_t * vm,
291 unformat_input_t * input, vlib_cli_command_t * cmd)
293 clib_error_t *error = 0;
294 vlib_log_main_t *lm = &log_main;
296 int i = last_log_entry ();
297 int count = lm->count;
301 e = vec_elt_at_index (lm->entries, i);
302 vlib_cli_output (vm, "%U %-10U %-10U %v",
303 format_time_float, 0, e->timestamp,
304 format_vlib_log_level, e->level,
305 format_vlib_log_class, e->class, e->string);
306 i = (i + 1) % lm->size;
313 VLIB_CLI_COMMAND (cli_show_log, static) = {
314 .path = "show logging",
315 .short_help = "show logging",
316 .function = show_log,
320 static clib_error_t *
321 show_log_config (vlib_main_t * vm,
322 unformat_input_t * input, vlib_cli_command_t * cmd)
324 clib_error_t *error = 0;
325 vlib_log_main_t *lm = &log_main;
326 vlib_log_class_data_t *c;
327 vlib_log_subclass_data_t *sc;
329 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
330 vlib_cli_output (vm, "Defaults:\n");
331 vlib_cli_output (vm, "%-20s %U", " Log Level:",
332 format_vlib_log_level, lm->default_log_level);
333 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
334 format_vlib_log_level, lm->default_syslog_log_level);
335 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
336 lm->default_rate_limit);
337 vlib_cli_output (vm, "\n");
338 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
339 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
341 vec_foreach (c, lm->classes)
343 vlib_cli_output (vm, "%s", c->name);
344 vec_foreach (sc, c->subclasses)
346 vlib_cli_output (vm, " %-20s %-14U %-14U %d",
347 sc->name ? (char *) sc->name : "default",
348 format_vlib_log_level, sc->level,
349 format_vlib_log_level, sc->syslog_level,
358 VLIB_CLI_COMMAND (cli_show_log_config, static) = {
359 .path = "show logging configuration",
360 .short_help = "show logging configuration",
361 .function = show_log_config,
365 static clib_error_t *
366 clear_log (vlib_main_t * vm,
367 unformat_input_t * input, vlib_cli_command_t * cmd)
369 clib_error_t *error = 0;
370 vlib_log_main_t *lm = &log_main;
372 int i = last_log_entry ();
373 int count = lm->count;
377 e = vec_elt_at_index (lm->entries, i);
378 vec_free (e->string);
379 i = (i + 1) % lm->size;
384 vlib_log_info (lm->log_class, "log cleared");
389 VLIB_CLI_COMMAND (cli_clear_log, static) = {
390 .path = "clear logging",
391 .short_help = "clear logging",
392 .function = clear_log,
397 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
399 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
400 u8 *level_str = NULL;
402 if (unformat (input, "%s", &level_str))
404 #define _(v, uc, lc) \
405 const char __##uc[] = #lc; \
406 if (!strcmp ((const char *) level_str, __##uc)) \
408 *level = VLIB_LOG_LEVEL_##uc; \
412 foreach_vlib_log_level;
417 vec_free (level_str);
422 unformat_vlib_log_class (unformat_input_t * input, va_list * args)
424 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
426 u8 *class_str = NULL;
427 vlib_log_main_t *lm = &log_main;
428 if (unformat (input, "%v", &class_str))
430 vlib_log_class_data_t *cdata;
431 vec_foreach (cdata, lm->classes)
433 if (vec_is_equal (cdata->name, class_str))
441 vec_free (class_str);
445 static clib_error_t *
446 set_log_class (vlib_main_t * vm,
447 unformat_input_t * input, vlib_cli_command_t * cmd)
449 unformat_input_t _line_input, *line_input = &_line_input;
450 clib_error_t *rv = NULL;
452 bool set_rate_limit = false;
453 bool set_level = false;
454 bool set_syslog_level = false;
455 vlib_log_level_t level;
456 vlib_log_level_t syslog_level;
458 /* Get a line of input. */
459 if (!unformat_user (input, unformat_line_input, line_input))
462 vlib_log_class_data_t *class = NULL;
463 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
465 return clib_error_return (0, "unknown log class `%U'",
466 format_unformat_error, line_input);
468 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
470 if (unformat (line_input, "rate-limit %d", &rate_limit))
472 set_rate_limit = true;
476 (line_input, "level %U", unformat_vlib_log_level, &level))
482 (line_input, "syslog-level %U", unformat_vlib_log_level,
485 set_syslog_level = true;
489 return clib_error_return (0, "unknown input `%U'",
490 format_unformat_error, line_input);
496 vlib_log_subclass_data_t *subclass;
497 vec_foreach (subclass, class->subclasses)
499 subclass->level = level;
502 if (set_syslog_level)
504 vlib_log_subclass_data_t *subclass;
505 vec_foreach (subclass, class->subclasses)
507 subclass->syslog_level = syslog_level;
512 vlib_log_subclass_data_t *subclass;
513 vec_foreach (subclass, class->subclasses)
515 subclass->rate_limit = rate_limit;
523 VLIB_CLI_COMMAND (cli_set_log, static) = {
524 .path = "set logging class",
525 .short_help = "set loggging class <class> [rate-limit <int>] "
526 "[level <level>] [syslog-level <level>]",
527 .function = set_log_class,
531 static clib_error_t *
532 set_log_unth_time (vlib_main_t * vm,
533 unformat_input_t * input, vlib_cli_command_t * cmd)
535 unformat_input_t _line_input, *line_input = &_line_input;
536 clib_error_t *rv = NULL;
538 vlib_log_main_t *lm = &log_main;
540 /* Get a line of input. */
541 if (!unformat_user (input, unformat_line_input, line_input))
544 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
546 if (unformat (line_input, "%d", &unthrottle_time))
547 lm->unthrottle_time = unthrottle_time;
549 return clib_error_return (0, "unknown input `%U'",
550 format_unformat_error, line_input);
557 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
558 .path = "set logging unthrottle-time",
559 .short_help = "set logging unthrottle-time <int>",
560 .function = set_log_unth_time,
564 static clib_error_t *
565 set_log_size (vlib_main_t * vm,
566 unformat_input_t * input, vlib_cli_command_t * cmd)
568 unformat_input_t _line_input, *line_input = &_line_input;
569 clib_error_t *rv = NULL;
571 vlib_log_main_t *lm = &log_main;
573 /* Get a line of input. */
574 if (!unformat_user (input, unformat_line_input, line_input))
577 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
579 if (unformat (line_input, "%d", &size))
582 vec_validate (lm->entries, lm->size);
585 return clib_error_return (0, "unknown input `%U'",
586 format_unformat_error, line_input);
593 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
594 .path = "set logging size",
595 .short_help = "set logging size <int>",
596 .function = set_log_size,
601 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
603 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
604 vlib_log_subclass_data_t **subclass =
605 va_arg (*args, vlib_log_subclass_data_t **);
607 u8 *subclass_str = NULL;
608 if (unformat (input, "%v", &subclass_str))
610 vlib_log_subclass_data_t *scdata;
611 vec_foreach (scdata, class->subclasses)
613 if (vec_is_equal (scdata->name, subclass_str))
621 vec_free (subclass_str);
625 static clib_error_t *
626 test_log_class_subclass (vlib_main_t * vm,
627 unformat_input_t * input, vlib_cli_command_t * cmd)
629 unformat_input_t _line_input, *line_input = &_line_input;
630 /* Get a line of input. */
631 if (!unformat_user (input, unformat_line_input, line_input))
634 vlib_log_class_data_t *class = NULL;
635 vlib_log_subclass_data_t *subclass = NULL;
636 vlib_log_level_t level;
637 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
639 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
642 (line_input, "%U", unformat_vlib_log_subclass, class,
646 (class->index << 16) | (subclass->index), "%U",
647 format_unformat_input, line_input);
651 return clib_error_return (0,
652 "unknown log subclass near beginning of `%U'",
653 format_unformat_error, line_input);
658 return clib_error_return (0,
659 "unknown log class near beginning of `%U'",
660 format_unformat_error, line_input);
665 return clib_error_return (0, "unknown log level near beginning of `%U'",
666 format_unformat_error, line_input);
672 VLIB_CLI_COMMAND (cli_test_log, static) = {
674 .short_help = "test log <class> <subclass> <level> <message",
675 .function = test_log_class_subclass,
679 static clib_error_t *
680 log_config (vlib_main_t * vm, unformat_input_t * input)
682 vlib_log_main_t *lm = &log_main;
684 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
686 if (unformat (input, "size %d", &lm->size))
687 vec_validate (lm->entries, lm->size);
688 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
690 else if (unformat (input, "default-log-level %U",
691 unformat_vlib_log_level, &lm->default_log_level))
693 else if (unformat (input, "default-syslog-log-level %U",
694 unformat_vlib_log_level,
695 &lm->default_syslog_log_level))
699 return unformat_parse_error (input);
706 VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
709 * fd.io coding-style-patch-verification: ON
712 * eval: (c-set-style "gnu")