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 vlib_log_main_t log_main = {
22 .default_log_level = VLIB_LOG_LEVEL_NOTICE,
23 .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING,
26 .default_rate_limit = 50,
32 vlib_log_main_t *lm = &log_main;
35 i = lm->next - lm->count;
42 static vlib_log_class_data_t *
43 get_class_data (vlib_log_class_t ci)
45 vlib_log_main_t *lm = &log_main;
46 return vec_elt_at_index (lm->classes, (ci >> 16));
49 static vlib_log_subclass_data_t *
50 get_subclass_data (vlib_log_class_t ci)
52 vlib_log_class_data_t *c = get_class_data (ci);
53 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
57 vlib_log_level_to_syslog_priority (vlib_log_level_t level)
61 #define LOG_DISABLED LOG_DEBUG
63 case VLIB_LOG_LEVEL_##uc:\
65 foreach_vlib_log_level
73 format_vlib_log_class (u8 * s, va_list * args)
75 vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
76 vlib_log_class_data_t *c = get_class_data (ci);
77 vlib_log_subclass_data_t *sc = get_subclass_data (ci);
80 return format (s, "%v/%v", c->name, sc->name);
82 return format (s, "%v", c->name, 0);
87 vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
89 vlib_main_t *vm = vlib_get_main ();
90 vlib_log_main_t *lm = &log_main;
92 vlib_log_subclass_data_t *sc = get_subclass_data (class);
94 f64 t = vlib_time_now (vm);
95 f64 delta = t - sc->last_event_timestamp;
97 bool use_formatted_log_entry = true;
99 vec_validate (lm->entries, lm->size);
100 /* make sure we are running on the main thread to avoid use in dataplane
101 code, for dataplane logging consider use of event-logger */
102 ASSERT (vlib_get_thread_index () == 0);
104 if (level > sc->level)
106 use_formatted_log_entry = false;
110 if ((delta > lm->unthrottle_time) ||
111 (sc->is_throttling == 0 && (delta > 1)))
113 sc->last_event_timestamp = t;
114 sc->last_sec_count = 0;
115 sc->is_throttling = 0;
119 sc->last_sec_count++;
120 if (sc->last_sec_count > sc->rate_limit)
122 else if (sc->last_sec_count == sc->rate_limit)
124 vec_reset_length (s);
125 s = format (0, "--- message(s) throttled ---");
126 sc->is_throttling = 1;
133 s = va_format (s, fmt, &va);
137 e = vec_elt_at_index (lm->entries, lm->next);
138 vec_free (e->string);
144 lm->next = (lm->next + 1) % lm->size;
145 if (lm->size > lm->count)
149 if (sc->syslog_level != VLIB_LOG_LEVEL_DISABLED &&
150 level <= sc->syslog_level)
152 u8 *tmp = format (NULL, "%U", format_vlib_log_class, class);
153 if (use_formatted_log_entry)
155 syslog (vlib_log_level_to_syslog_priority (level), "%.*s: %.*s",
156 (int) vec_len (tmp), tmp,
158 (vec_c_string_is_terminated (s) ? 1 : 0)), s);
162 tmp = format (tmp, ": ");
164 tmp = va_format (tmp, fmt, &va);
166 syslog (vlib_log_level_to_syslog_priority (level), "%.*s",
167 (int) (vec_len (tmp) -
168 (vec_c_string_is_terminated (tmp) ? 1 : 0)), tmp);
176 vlib_log_register_class (char *class, char *subclass)
178 vlib_log_main_t *lm = &log_main;
179 vlib_log_class_data_t *c = NULL;
180 vlib_log_subclass_data_t *s;
181 vlib_log_class_data_t *tmp;
182 vec_foreach (tmp, lm->classes)
184 if (vec_len (tmp->name) != strlen (class))
186 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
194 vec_add2 (lm->classes, c, 1);
195 c->index = c - lm->classes;
196 c->name = format (0, "%s", class);
199 vec_add2 (c->subclasses, s, 1);
200 s->index = s - c->subclasses;
201 s->name = subclass ? format (0, "%s", subclass) : 0;
202 s->rate_limit = lm->default_rate_limit;
203 s->level = lm->default_log_level;
204 s->syslog_level = lm->default_syslog_log_level;
205 return (c->index << 16) | (s->index);
209 format_vlib_log_level (u8 * s, va_list * args)
211 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
216 #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
217 foreach_vlib_log_level
220 return format (s, "unknown");
222 return format (s, "%s", t);
226 vlib_log_get_indent ()
228 return log_main.indent;
231 static clib_error_t *
232 vlib_log_init (vlib_main_t * vm)
234 vlib_log_main_t *lm = &log_main;
236 gettimeofday (&lm->time_zero_timeval, 0);
237 lm->time_zero = vlib_time_now (vm);
239 vec_validate (lm->entries, lm->size);
240 lm->log_class = vlib_log_register_class ("log", 0);
241 u8 *tmp = format (NULL, "%U %-10U %-10U ", format_time_float, 0, (f64) 0,
242 format_white_space, 255, format_white_space, 255);
243 log_main.indent = vec_len (tmp);
248 VLIB_INIT_FUNCTION (vlib_log_init);
251 static clib_error_t *
252 show_log (vlib_main_t * vm,
253 unformat_input_t * input, vlib_cli_command_t * cmd)
255 clib_error_t *error = 0;
256 vlib_log_main_t *lm = &log_main;
258 int i = last_log_entry ();
259 int count = lm->count;
262 time_offset = (f64) lm->time_zero_timeval.tv_sec
263 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
267 e = vec_elt_at_index (lm->entries, i);
268 vlib_cli_output (vm, "%U %-10U %-14U %v",
269 format_time_float, 0, e->timestamp + time_offset,
270 format_vlib_log_level, e->level,
271 format_vlib_log_class, e->class, e->string);
272 i = (i + 1) % lm->size;
279 VLIB_CLI_COMMAND (cli_show_log, static) = {
280 .path = "show logging",
281 .short_help = "show logging",
282 .function = show_log,
286 static clib_error_t *
287 show_log_config (vlib_main_t * vm,
288 unformat_input_t * input, vlib_cli_command_t * cmd)
290 clib_error_t *error = 0;
291 vlib_log_main_t *lm = &log_main;
292 vlib_log_class_data_t *c;
293 vlib_log_subclass_data_t *sc;
295 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
296 vlib_cli_output (vm, "Defaults:\n");
297 vlib_cli_output (vm, "%-20s %U", " Log Level:",
298 format_vlib_log_level, lm->default_log_level);
299 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
300 format_vlib_log_level, lm->default_syslog_log_level);
301 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
302 lm->default_rate_limit);
303 vlib_cli_output (vm, "\n");
304 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
305 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
308 u8 *defstr = format (0, "default");
309 vec_foreach (c, lm->classes)
311 vlib_cli_output (vm, "%v", c->name);
312 vec_foreach (sc, c->subclasses)
314 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
315 sc->name ? sc->name : defstr,
316 format_vlib_log_level, sc->level,
317 format_vlib_log_level, sc->syslog_level,
327 VLIB_CLI_COMMAND (cli_show_log_config, static) = {
328 .path = "show logging configuration",
329 .short_help = "show logging configuration",
330 .function = show_log_config,
334 static clib_error_t *
335 clear_log (vlib_main_t * vm,
336 unformat_input_t * input, vlib_cli_command_t * cmd)
338 clib_error_t *error = 0;
339 vlib_log_main_t *lm = &log_main;
341 int i = last_log_entry ();
342 int count = lm->count;
346 e = vec_elt_at_index (lm->entries, i);
347 vec_free (e->string);
348 i = (i + 1) % lm->size;
353 vlib_log_info (lm->log_class, "log cleared");
358 VLIB_CLI_COMMAND (cli_clear_log, static) = {
359 .path = "clear logging",
360 .short_help = "clear logging",
361 .function = clear_log,
366 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
368 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
369 u8 *level_str = NULL;
371 if (unformat (input, "%s", &level_str))
373 #define _(v, uc, lc) \
374 const char __##uc[] = #lc; \
375 if (!strcmp ((const char *) level_str, __##uc)) \
377 *level = VLIB_LOG_LEVEL_##uc; \
381 foreach_vlib_log_level;
386 vec_free (level_str);
391 unformat_vlib_log_class (unformat_input_t * input, va_list * args)
393 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
395 u8 *class_str = NULL;
396 vlib_log_main_t *lm = &log_main;
397 if (unformat (input, "%v", &class_str))
399 vlib_log_class_data_t *cdata;
400 vec_foreach (cdata, lm->classes)
402 if (vec_is_equal (cdata->name, class_str))
410 vec_free (class_str);
414 static clib_error_t *
415 set_log_class (vlib_main_t * vm,
416 unformat_input_t * input, vlib_cli_command_t * cmd)
418 unformat_input_t _line_input, *line_input = &_line_input;
419 clib_error_t *rv = NULL;
421 bool set_rate_limit = false;
422 bool set_level = false;
423 bool set_syslog_level = false;
424 vlib_log_level_t level;
425 vlib_log_level_t syslog_level;
427 /* Get a line of input. */
428 if (!unformat_user (input, unformat_line_input, line_input))
431 vlib_log_class_data_t *class = NULL;
432 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
434 return clib_error_return (0, "unknown log class `%U'",
435 format_unformat_error, line_input);
437 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
439 if (unformat (line_input, "rate-limit %d", &rate_limit))
441 set_rate_limit = true;
445 (line_input, "level %U", unformat_vlib_log_level, &level))
451 (line_input, "syslog-level %U", unformat_vlib_log_level,
454 set_syslog_level = true;
458 return clib_error_return (0, "unknown input `%U'",
459 format_unformat_error, line_input);
465 vlib_log_subclass_data_t *subclass;
466 vec_foreach (subclass, class->subclasses)
468 subclass->level = level;
471 if (set_syslog_level)
473 vlib_log_subclass_data_t *subclass;
474 vec_foreach (subclass, class->subclasses)
476 subclass->syslog_level = syslog_level;
481 vlib_log_subclass_data_t *subclass;
482 vec_foreach (subclass, class->subclasses)
484 subclass->rate_limit = rate_limit;
492 VLIB_CLI_COMMAND (cli_set_log, static) = {
493 .path = "set logging class",
494 .short_help = "set logging class <class> [rate-limit <int>] "
495 "[level <level>] [syslog-level <level>]",
496 .function = set_log_class,
500 static clib_error_t *
501 set_log_unth_time (vlib_main_t * vm,
502 unformat_input_t * input, vlib_cli_command_t * cmd)
504 unformat_input_t _line_input, *line_input = &_line_input;
505 clib_error_t *rv = NULL;
507 vlib_log_main_t *lm = &log_main;
509 /* Get a line of input. */
510 if (!unformat_user (input, unformat_line_input, line_input))
513 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
515 if (unformat (line_input, "%d", &unthrottle_time))
516 lm->unthrottle_time = unthrottle_time;
518 return clib_error_return (0, "unknown input `%U'",
519 format_unformat_error, line_input);
526 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
527 .path = "set logging unthrottle-time",
528 .short_help = "set logging unthrottle-time <int>",
529 .function = set_log_unth_time,
533 static clib_error_t *
534 set_log_size (vlib_main_t * vm,
535 unformat_input_t * input, vlib_cli_command_t * cmd)
537 unformat_input_t _line_input, *line_input = &_line_input;
538 clib_error_t *rv = NULL;
540 vlib_log_main_t *lm = &log_main;
542 /* Get a line of input. */
543 if (!unformat_user (input, unformat_line_input, line_input))
546 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
548 if (unformat (line_input, "%d", &size))
551 vec_validate (lm->entries, lm->size);
554 return clib_error_return (0, "unknown input `%U'",
555 format_unformat_error, line_input);
562 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
563 .path = "set logging size",
564 .short_help = "set logging size <int>",
565 .function = set_log_size,
570 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
572 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
573 vlib_log_subclass_data_t **subclass =
574 va_arg (*args, vlib_log_subclass_data_t **);
576 u8 *subclass_str = NULL;
577 if (unformat (input, "%v", &subclass_str))
579 vlib_log_subclass_data_t *scdata;
580 vec_foreach (scdata, class->subclasses)
582 if (vec_is_equal (scdata->name, subclass_str))
590 vec_free (subclass_str);
594 static clib_error_t *
595 test_log_class_subclass (vlib_main_t * vm,
596 unformat_input_t * input, vlib_cli_command_t * cmd)
598 unformat_input_t _line_input, *line_input = &_line_input;
599 /* Get a line of input. */
600 if (!unformat_user (input, unformat_line_input, line_input))
603 vlib_log_class_data_t *class = NULL;
604 vlib_log_subclass_data_t *subclass = NULL;
605 vlib_log_level_t level;
606 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
608 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
611 (line_input, "%U", unformat_vlib_log_subclass, class,
615 (class->index << 16) | (subclass->index), "%U",
616 format_unformat_input, line_input);
620 return clib_error_return (0,
621 "unknown log subclass near beginning of `%U'",
622 format_unformat_error, line_input);
627 return clib_error_return (0,
628 "unknown log class near beginning of `%U'",
629 format_unformat_error, line_input);
634 return clib_error_return (0, "unknown log level near beginning of `%U'",
635 format_unformat_error, line_input);
641 VLIB_CLI_COMMAND (cli_test_log, static) = {
643 .short_help = "test log <level> <class> <subclass> <message>",
644 .function = test_log_class_subclass,
648 static clib_error_t *
649 log_config (vlib_main_t * vm, unformat_input_t * input)
651 vlib_log_main_t *lm = &log_main;
653 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
655 if (unformat (input, "size %d", &lm->size))
656 vec_validate (lm->entries, lm->size);
657 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
659 else if (unformat (input, "default-log-level %U",
660 unformat_vlib_log_level, &lm->default_log_level))
662 else if (unformat (input, "default-syslog-log-level %U",
663 unformat_vlib_log_level,
664 &lm->default_syslog_log_level))
668 return unformat_parse_error (input);
675 VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
678 * fd.io coding-style-patch-verification: ON
681 * eval: (c-set-style "gnu")