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>
21 #define VLIB_LOG_DEFAULT_SIZE 50
22 #define VLIB_LOG_DEFAULT_RATE_LIMIT 10
23 #define VLIB_LOG_DEFAULT_UNTHROTTLE_TIME 3
24 #define VLIB_LOG_DEFAULT_LOG_LEVEL VLIB_LOG_LEVEL_CRIT
28 vlib_log_level_t level;
29 vlib_log_class_t class;
38 // level of log messages kept for this subclass
39 vlib_log_level_t level;
40 // level of log messages sent to syslog for this subclass
41 vlib_log_level_t syslog_level;
42 // flag saying whether this subclass is logged to syslog
44 f64 last_event_timestamp;
48 } vlib_log_subclass_data_t;
54 vlib_log_subclass_data_t *subclasses;
55 } vlib_log_class_data_t;
59 vlib_log_entry_t *entries;
60 vlib_log_class_data_t *classes;
61 int size, next, count;
63 /* our own log class */
64 vlib_log_class_t log_class;
66 int default_rate_limit;
71 vlib_log_main_t log_main;
77 vlib_log_main_t *lm = &log_main;
80 i = lm->next - lm->count;
87 static vlib_log_class_data_t *
88 get_class_data (vlib_log_class_t ci)
90 vlib_log_main_t *lm = &log_main;
91 return vec_elt_at_index (lm->classes, (ci >> 16));
94 static vlib_log_subclass_data_t *
95 get_subclass_data (vlib_log_class_t ci)
97 vlib_log_class_data_t *c = get_class_data (ci);
98 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
102 vlib_log_level_to_syslog_priority (vlib_log_level_t level)
107 case VLIB_LOG_LEVEL_##uc:\
109 foreach_vlib_log_level
116 format_vlib_log_class (u8 * s, va_list * args)
118 vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
119 vlib_log_class_data_t *c = get_class_data (ci);
120 vlib_log_subclass_data_t *sc = get_subclass_data (ci);
123 return format (s, "%v/%v", c->name, sc->name);
125 return format (s, "%v", c->name, 0);
130 vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
132 vlib_main_t *vm = vlib_get_main ();
133 vlib_log_main_t *lm = &log_main;
135 vlib_log_subclass_data_t *sc = get_subclass_data (class);
137 f64 t = vlib_time_now (vm);
138 f64 delta = t - sc->last_event_timestamp;
140 bool use_formatted_log_entry = true;
142 /* make sure we are running on the main thread to avoid use in dataplane
143 code, for dataplane logging consider use of event-logger */
144 ASSERT (vlib_get_thread_index () == 0);
146 if (level > sc->level)
148 use_formatted_log_entry = false;
152 if ((delta > lm->unthrottle_time) ||
153 (sc->is_throttling == 0 && (delta > 1)))
155 sc->last_event_timestamp = t;
156 sc->last_sec_count = 0;
157 sc->is_throttling = 0;
161 sc->last_sec_count++;
162 if (sc->last_sec_count > sc->rate_limit)
164 else if (sc->last_sec_count == sc->rate_limit)
166 vec_reset_length (s);
167 s = format (0, "--- message(s) throttled ---");
168 sc->is_throttling = 1;
175 s = va_format (s, fmt, &va);
179 e = vec_elt_at_index (lm->entries, lm->next);
180 vec_free (e->string);
186 lm->next = (lm->next + 1) % lm->size;
187 if (lm->size > lm->count)
191 if (sc->syslog_enabled && level <= sc->syslog_level)
193 u8 *tmp = format (NULL, "%U", format_vlib_log_class, class);
194 if (use_formatted_log_entry)
196 syslog (vlib_log_level_to_syslog_priority (level), "%.*s: %.*s",
198 vec_len (s) - (vec_c_string_is_terminated (s) ? 1 : 0), s);
202 tmp = format (tmp, ": ");
204 tmp = va_format (tmp, fmt, &va);
206 syslog (vlib_log_level_to_syslog_priority (level), "%.*s",
207 vec_len (tmp) - (vec_c_string_is_terminated (tmp) ? 1 : 0),
216 vlib_log_register_class (char *class, char *subclass)
218 vlib_log_main_t *lm = &log_main;
219 vlib_log_class_data_t *c = NULL;
220 vlib_log_subclass_data_t *s;
221 vlib_log_class_data_t *tmp;
222 vec_foreach (tmp, lm->classes)
224 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
232 vec_add2 (lm->classes, c, 1);
233 c->index = c - lm->classes;
234 c->name = format (0, "%s", class);
237 vec_add2 (c->subclasses, s, 1);
238 s->index = s - c->subclasses;
239 s->name = subclass ? format (0, "%s", subclass) : 0;
240 s->rate_limit = lm->default_rate_limit;
241 s->level = VLIB_LOG_DEFAULT_LOG_LEVEL;
242 return (c->index << 16) | (s->index);
246 format_vlib_log_level (u8 * s, va_list * args)
248 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
253 #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
254 foreach_vlib_log_level
257 return format (s, "unknown");
259 return format (s, "%s", t);
263 vlib_log_get_indent ()
265 return log_main.indent;
268 static clib_error_t *
269 vlib_log_init (vlib_main_t * vm)
271 vlib_log_main_t *lm = &log_main;
272 lm->size = VLIB_LOG_DEFAULT_SIZE;
273 vec_validate (lm->entries, lm->size);
274 lm->log_class = vlib_log_register_class ("log", 0);
275 u8 *tmp = format (NULL, "%U %-10U %-10U ", format_time_float, 0, (f64) 0,
276 format_white_space, 255, format_white_space, 255);
277 log_main.indent = vec_len (tmp);
282 VLIB_INIT_FUNCTION (vlib_log_init);
285 static clib_error_t *
286 show_log (vlib_main_t * vm,
287 unformat_input_t * input, vlib_cli_command_t * cmd)
289 clib_error_t *error = 0;
290 vlib_log_main_t *lm = &log_main;
292 int i = last_log_entry ();
293 int count = lm->count;
297 e = vec_elt_at_index (lm->entries, i);
298 vlib_cli_output (vm, "%U %-10U %-10U %v (%d)",
299 format_time_float, 0, e->timestamp,
300 format_vlib_log_level, e->level,
301 format_vlib_log_class, e->class, e->string, i);
302 i = (i + 1) % lm->size;
309 VLIB_CLI_COMMAND (cli_show_log, static) = {
310 .path = "show logging",
311 .short_help = "show logging",
312 .function = show_log,
316 static clib_error_t *
317 clear_log (vlib_main_t * vm,
318 unformat_input_t * input, vlib_cli_command_t * cmd)
320 clib_error_t *error = 0;
321 vlib_log_main_t *lm = &log_main;
323 int i = last_log_entry ();
324 int count = lm->count;
328 e = vec_elt_at_index (lm->entries, i);
329 vec_free (e->string);
330 i = (i + 1) % lm->size;
335 vlib_log_info (lm->log_class, "log cleared");
340 VLIB_CLI_COMMAND (cli_clear_log, static) = {
341 .path = "clear logging",
342 .short_help = "clear logging",
343 .function = clear_log,
348 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
350 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
351 u8 *level_str = NULL;
353 if (unformat (input, "%v", &level_str))
355 #define _(v, uc, lc) \
356 const char __##uc[] = #lc; \
357 if (!memcmp (level_str, __##uc, sizeof (__##uc))) \
359 *level = VLIB_LOG_LEVEL_##uc; \
363 foreach_vlib_log_level;
368 vec_free (level_str);
373 unformat_vlib_log_class (unformat_input_t * input, va_list * args)
375 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
377 u8 *class_str = NULL;
378 vlib_log_main_t *lm = &log_main;
379 if (unformat (input, "%v", &class_str))
381 vlib_log_class_data_t *cdata;
382 vec_foreach (cdata, lm->classes)
384 if (vec_is_equal (cdata->name, class_str))
392 vec_free (class_str);
396 static clib_error_t *
397 set_log_class (vlib_main_t * vm,
398 unformat_input_t * input, vlib_cli_command_t * cmd)
400 unformat_input_t _line_input, *line_input = &_line_input;
401 clib_error_t *rv = NULL;
403 bool set_rate_limit = false;
404 bool set_level = false;
405 bool set_syslog_level = false;
406 vlib_log_level_t level;
407 vlib_log_level_t syslog_level;
409 /* Get a line of input. */
410 if (!unformat_user (input, unformat_line_input, line_input))
413 vlib_log_class_data_t *class = NULL;
414 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
416 return clib_error_return (0, "unknown log class `%U'",
417 format_unformat_error, line_input);
419 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
421 if (unformat (line_input, "rate-limit %d", &rate_limit))
423 set_rate_limit = true;
427 (line_input, "level %U", unformat_vlib_log_level, &level))
433 (line_input, "syslog-level %U", unformat_vlib_log_level,
436 set_syslog_level = true;
440 return clib_error_return (0, "unknown input `%U'",
441 format_unformat_error, line_input);
447 vlib_log_subclass_data_t *subclass;
448 vec_foreach (subclass, class->subclasses)
450 subclass->level = level;
453 if (set_syslog_level)
455 vlib_log_subclass_data_t *subclass;
456 vec_foreach (subclass, class->subclasses)
458 subclass->syslog_level = syslog_level;
459 subclass->syslog_enabled = true;
464 vlib_log_subclass_data_t *subclass;
465 vec_foreach (subclass, class->subclasses)
467 subclass->syslog_enabled = false;
472 vlib_log_subclass_data_t *subclass;
473 vec_foreach (subclass, class->subclasses)
475 subclass->rate_limit = rate_limit;
483 VLIB_CLI_COMMAND (cli_set_log, static) = {
484 .path = "set logging class",
485 .short_help = "set loggging class <class> [rate-limit <int>] "
486 "[level <level>] [syslog-level <level>]",
487 .function = set_log_class,
491 static clib_error_t *
492 set_log_unth_time (vlib_main_t * vm,
493 unformat_input_t * input, vlib_cli_command_t * cmd)
495 unformat_input_t _line_input, *line_input = &_line_input;
496 clib_error_t *rv = NULL;
498 vlib_log_main_t *lm = &log_main;
500 /* Get a line of input. */
501 if (!unformat_user (input, unformat_line_input, line_input))
504 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
506 if (unformat (line_input, "%d", &unthrottle_time))
507 lm->unthrottle_time = unthrottle_time;
509 return clib_error_return (0, "unknown input `%U'",
510 format_unformat_error, line_input);
517 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
518 .path = "set logging unthrottle-time",
519 .short_help = "set logging unthrottle-time <int>",
520 .function = set_log_unth_time,
524 static clib_error_t *
525 set_log_size (vlib_main_t * vm,
526 unformat_input_t * input, vlib_cli_command_t * cmd)
528 unformat_input_t _line_input, *line_input = &_line_input;
529 clib_error_t *rv = NULL;
531 vlib_log_main_t *lm = &log_main;
533 /* Get a line of input. */
534 if (!unformat_user (input, unformat_line_input, line_input))
537 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
539 if (unformat (line_input, "%d", &size))
542 vec_validate (lm->entries, lm->size);
545 return clib_error_return (0, "unknown input `%U'",
546 format_unformat_error, line_input);
553 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
554 .path = "set logging size",
555 .short_help = "set logging size <int>",
556 .function = set_log_size,
561 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
563 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
564 vlib_log_subclass_data_t **subclass =
565 va_arg (*args, vlib_log_subclass_data_t **);
567 u8 *subclass_str = NULL;
568 if (unformat (input, "%v", &subclass_str))
570 vlib_log_subclass_data_t *scdata;
571 vec_foreach (scdata, class->subclasses)
573 if (vec_is_equal (scdata->name, subclass_str))
581 vec_free (subclass_str);
585 static clib_error_t *
586 test_log_class_subclass (vlib_main_t * vm,
587 unformat_input_t * input, vlib_cli_command_t * cmd)
589 unformat_input_t _line_input, *line_input = &_line_input;
590 /* Get a line of input. */
591 if (!unformat_user (input, unformat_line_input, line_input))
594 vlib_log_class_data_t *class = NULL;
595 vlib_log_subclass_data_t *subclass = NULL;
596 vlib_log_level_t level;
597 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
599 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
602 (line_input, "%U", unformat_vlib_log_subclass, class,
606 (class->index << 16) | (subclass->index), "%U",
607 format_unformat_input, line_input);
611 return clib_error_return (0,
612 "unknown log subclass near beginning of `%U'",
613 format_unformat_error, line_input);
618 return clib_error_return (0,
619 "unknown log class near beginning of `%U'",
620 format_unformat_error, line_input);
625 return clib_error_return (0, "unknown log level near beginning of `%U'",
626 format_unformat_error, line_input);
632 VLIB_CLI_COMMAND (cli_test_log, static) = {
634 .short_help = "test log <class> <subclass> <level> <message",
635 .function = test_log_class_subclass,
639 static clib_error_t *
640 log_config (vlib_main_t * vm, unformat_input_t * input)
642 vlib_log_main_t *lm = &log_main;
645 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
647 if (unformat (input, "size %d", &size))
650 vec_validate (lm->entries, lm->size);
652 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
654 // nothing to do here
658 return unformat_parse_error (input);
665 VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
668 * fd.io coding-style-patch-verification: ON
671 * eval: (c-set-style "gnu")