blob: 27d2e16b006c85051f21cb3cebdf4aa6b1584676 [file] [log] [blame]
Damjan Marion07a38572018-01-21 06:44:18 -08001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <stdbool.h>
17#include <vlib/vlib.h>
18#include <vlib/log.h>
Damjan Marion06d82262020-10-21 12:43:40 +020019#include <vlib/unix/unix.h>
Damjan Marion07a38572018-01-21 06:44:18 -080020#include <syslog.h>
21
Damjan Marione78fab82018-04-18 17:03:28 +020022vlib_log_main_t log_main = {
23 .default_log_level = VLIB_LOG_LEVEL_NOTICE,
24 .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING,
25 .unthrottle_time = 3,
26 .size = 512,
27 .default_rate_limit = 50,
28};
Damjan Marion07a38572018-01-21 06:44:18 -080029
Damjan Marion06d82262020-10-21 12:43:40 +020030static 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 */
39};
40
Paul Vinciguerra03f1af22019-06-25 22:30:19 -040041int
Damjan Marion07a38572018-01-21 06:44:18 -080042last_log_entry ()
43{
44 vlib_log_main_t *lm = &log_main;
45 int i;
46
47 i = lm->next - lm->count;
48
49 if (i < 0)
50 i += lm->size;
51 return i;
52}
53
54static vlib_log_class_data_t *
55get_class_data (vlib_log_class_t ci)
56{
57 vlib_log_main_t *lm = &log_main;
58 return vec_elt_at_index (lm->classes, (ci >> 16));
59}
60
61static vlib_log_subclass_data_t *
62get_subclass_data (vlib_log_class_t ci)
63{
64 vlib_log_class_data_t *c = get_class_data (ci);
65 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
66}
67
68static int
69vlib_log_level_to_syslog_priority (vlib_log_level_t level)
70{
71 switch (level)
72 {
Damjan Marione78fab82018-04-18 17:03:28 +020073#define LOG_DISABLED LOG_DEBUG
Damjan Marion07a38572018-01-21 06:44:18 -080074#define _(n,uc,lc) \
75 case VLIB_LOG_LEVEL_##uc:\
76 return LOG_##uc;
77 foreach_vlib_log_level
78#undef _
Damjan Marione78fab82018-04-18 17:03:28 +020079#undef LOG_DISABLED
Damjan Marion07a38572018-01-21 06:44:18 -080080 }
81 return LOG_DEBUG;
82}
83
84u8 *
85format_vlib_log_class (u8 * s, va_list * args)
86{
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);
90
91 if (sc->name)
92 return format (s, "%v/%v", c->name, sc->name);
93 else
94 return format (s, "%v", c->name, 0);
95}
96
Damjan Marion06d82262020-10-21 12:43:40 +020097u8 *
98format_indent (u8 * s, va_list * args)
99{
100 u8 *v = va_arg (*args, u8 *);
101 u32 indent = va_arg (*args, u32);
102 u8 *c;
103
104 /* *INDENT-OFF* */
105 vec_foreach (c, v)
106 {
107 vec_add (s, c, 1);
108 if (c[0] == '\n')
109 for (u32 i = 0; i < indent; i++)
110 vec_add1 (s, (u8) ' ');
111 }
112 /* *INDENT-ON* */
113 return s;
114}
115
116static int
117log_level_is_enabled (vlib_log_level_t level, vlib_log_level_t configured)
118{
119 if (configured == VLIB_LOG_LEVEL_DISABLED)
120 return 0;
121 if (level > configured)
122 return 0;
123 return 1;
124}
Damjan Marion07a38572018-01-21 06:44:18 -0800125
126void
127vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
128{
129 vlib_main_t *vm = vlib_get_main ();
130 vlib_log_main_t *lm = &log_main;
131 vlib_log_entry_t *e;
132 vlib_log_subclass_data_t *sc = get_subclass_data (class);
133 va_list va;
134 f64 t = vlib_time_now (vm);
135 f64 delta = t - sc->last_event_timestamp;
Damjan Marion06d82262020-10-21 12:43:40 +0200136 int log_enabled = log_level_is_enabled (level, sc->level);
137 int syslog_enabled = log_level_is_enabled (level, sc->syslog_level);
Damjan Marion07a38572018-01-21 06:44:18 -0800138 u8 *s = 0;
Damjan Marion07a38572018-01-21 06:44:18 -0800139
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);
143
Damjan Marion06d82262020-10-21 12:43:40 +0200144 if ((log_enabled || syslog_enabled) == 0)
145 return;
146
147 vec_validate (lm->entries, lm->size);
Damjan Marion07a38572018-01-21 06:44:18 -0800148
149 if ((delta > lm->unthrottle_time) ||
150 (sc->is_throttling == 0 && (delta > 1)))
151 {
152 sc->last_event_timestamp = t;
153 sc->last_sec_count = 0;
154 sc->is_throttling = 0;
155 }
156 else
157 {
158 sc->last_sec_count++;
159 if (sc->last_sec_count > sc->rate_limit)
160 return;
161 else if (sc->last_sec_count == sc->rate_limit)
162 {
163 vec_reset_length (s);
Damjan Marion06d82262020-10-21 12:43:40 +0200164 s = format (s, "--- message(s) throttled ---");
Damjan Marion07a38572018-01-21 06:44:18 -0800165 sc->is_throttling = 1;
166 }
167 }
168
169 if (s == 0)
170 {
171 va_start (va, fmt);
172 s = va_format (s, fmt, &va);
173 va_end (va);
174 }
175
Damjan Marion06d82262020-10-21 12:43:40 +0200176 if (syslog_enabled)
Damjan Marion07a38572018-01-21 06:44:18 -0800177 {
Damjan Marion06d82262020-10-21 12:43:40 +0200178 u8 *l = 0;
179 if (unix_main.flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG))
Damjan Marion07a38572018-01-21 06:44:18 -0800180 {
Damjan Marion06d82262020-10-21 12:43:40 +0200181 int indent = 0;
182 int with_colors = (unix_main.flags & UNIX_FLAG_NOCOLOR) == 0;
183 u8 *fmt;
184 if (with_colors)
185 {
186 l = format (l, "\x1b[%um", 90 + colors[level]);
187 indent = vec_len (l);
188 }
189 fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length);
190 l = format (l, (char *) fmt, format_vlib_log_class, class,
191 format_vlib_log_level, level);
192 vec_free (fmt);
193 indent = vec_len (l) - indent;
194 if (with_colors)
195 l = format (l, "\x1b[0m");
196 l = format (l, "%U", format_indent, s, indent);
197 fformat (stderr, "%v\n", l);
198 fflush (stderr);
Damjan Marion07a38572018-01-21 06:44:18 -0800199 }
200 else
201 {
Damjan Marion06d82262020-10-21 12:43:40 +0200202 l = format (l, "%U", format_vlib_log_class, class);
203 int prio = vlib_log_level_to_syslog_priority (level);
204 int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
205
206 syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
207 (int) vec_len (s) - is_term, s);
Damjan Marion07a38572018-01-21 06:44:18 -0800208 }
Damjan Marion06d82262020-10-21 12:43:40 +0200209 vec_free (l);
Damjan Marion07a38572018-01-21 06:44:18 -0800210 }
211
Damjan Marion06d82262020-10-21 12:43:40 +0200212 if (log_enabled)
213 {
214 e = vec_elt_at_index (lm->entries, lm->next);
215 vec_free (e->string);
216 e->level = level;
217 e->class = class;
218 e->string = s;
219 e->timestamp = t;
220 s = 0;
221
222 lm->next = (lm->next + 1) % lm->size;
223 if (lm->size > lm->count)
224 lm->count++;
225 }
226
227 vec_free (s);
Damjan Marion07a38572018-01-21 06:44:18 -0800228}
229
Dave Barach8dc954a2020-02-05 17:31:09 -0500230static vlib_log_class_t
231vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
Damjan Marion07a38572018-01-21 06:44:18 -0800232{
233 vlib_log_main_t *lm = &log_main;
234 vlib_log_class_data_t *c = NULL;
235 vlib_log_subclass_data_t *s;
236 vlib_log_class_data_t *tmp;
Damjan Marion055e6402020-10-21 19:43:36 +0200237 vlib_log_class_config_t *cc = 0, *scc = 0;
238 uword *p;
239 u8 *str;
Damjan Marion06d82262020-10-21 12:43:40 +0200240 u32 length = 0;
241
Damjan Marion055e6402020-10-21 19:43:36 +0200242 if ((p = hash_get_mem (lm->config_index_by_name, class)))
243 cc = vec_elt_at_index (lm->configs, p[0]);
244
245 str = format (0, "%s/%s%c", class, subclass, 0);
246 if ((p = hash_get_mem (lm->config_index_by_name, (char *) str)))
247 scc = vec_elt_at_index (lm->configs, p[0]);
248 vec_free (str);
249
Damjan Marion07a38572018-01-21 06:44:18 -0800250 vec_foreach (tmp, lm->classes)
251 {
Su Wang34321b32019-01-11 12:46:05 -0500252 if (vec_len (tmp->name) != strlen (class))
253 continue;
Damjan Marion07a38572018-01-21 06:44:18 -0800254 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
255 {
256 c = tmp;
257 break;
258 }
259 }
260 if (!c)
261 {
262 vec_add2 (lm->classes, c, 1);
263 c->index = c - lm->classes;
264 c->name = format (0, "%s", class);
Damjan Marion06d82262020-10-21 12:43:40 +0200265 length = vec_len (c->name);
Damjan Marion07a38572018-01-21 06:44:18 -0800266 }
267
268 vec_add2 (c->subclasses, s, 1);
269 s->index = s - c->subclasses;
270 s->name = subclass ? format (0, "%s", subclass) : 0;
Damjan Marion055e6402020-10-21 19:43:36 +0200271
272 if (scc && scc->rate_limit != ~0)
273 s->rate_limit = scc->rate_limit;
274 else if (cc && cc->rate_limit != ~0)
275 s->rate_limit = cc->rate_limit;
276 else if (limit)
277 s->rate_limit = limit;
278 else
279 s->rate_limit = lm->default_rate_limit;
280
281 if (scc && scc->level != ~0)
282 s->level = scc->level;
283 else if (cc && cc->level != ~0)
284 s->level = cc->level;
285 else
286 s->level = lm->default_log_level;
287
288 if (scc && scc->syslog_level != ~0)
289 s->syslog_level = scc->syslog_level;
290 else if (cc && cc->syslog_level != ~0)
291 s->syslog_level = cc->syslog_level;
292 else
293 s->syslog_level = lm->default_syslog_log_level;
294
Damjan Marion06d82262020-10-21 12:43:40 +0200295 if (subclass)
296 length += 1 + vec_len (s->name);
297 if (length > lm->max_class_name_length)
298 lm->max_class_name_length = length;
Damjan Marion07a38572018-01-21 06:44:18 -0800299 return (c->index << 16) | (s->index);
300}
301
Dave Barach8dc954a2020-02-05 17:31:09 -0500302vlib_log_class_t
303vlib_log_register_class (char *class, char *subclass)
304{
305 return vlib_log_register_class_internal (class, subclass,
306 0 /* default rate limit */ );
307}
308
309vlib_log_class_t
310vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit)
311{
312 return vlib_log_register_class_internal (class, subclass, limit);
313}
314
315
Damjan Marion07a38572018-01-21 06:44:18 -0800316u8 *
317format_vlib_log_level (u8 * s, va_list * args)
318{
319 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
320 char *t = 0;
321
322 switch (i)
323 {
324#define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
325 foreach_vlib_log_level
326#undef _
327 default:
328 return format (s, "unknown");
329 }
330 return format (s, "%s", t);
331}
332
Damjan Marion07a38572018-01-21 06:44:18 -0800333static clib_error_t *
334vlib_log_init (vlib_main_t * vm)
335{
336 vlib_log_main_t *lm = &log_main;
Dave Barach10a690b2018-09-27 08:46:15 -0400337
338 gettimeofday (&lm->time_zero_timeval, 0);
339 lm->time_zero = vlib_time_now (vm);
340
Damjan Marion07a38572018-01-21 06:44:18 -0800341 vec_validate (lm->entries, lm->size);
342 lm->log_class = vlib_log_register_class ("log", 0);
Damjan Marion07a38572018-01-21 06:44:18 -0800343 return 0;
344}
345
346VLIB_INIT_FUNCTION (vlib_log_init);
347
348
349static clib_error_t *
350show_log (vlib_main_t * vm,
351 unformat_input_t * input, vlib_cli_command_t * cmd)
352{
353 clib_error_t *error = 0;
354 vlib_log_main_t *lm = &log_main;
355 vlib_log_entry_t *e;
356 int i = last_log_entry ();
357 int count = lm->count;
Dave Barach10a690b2018-09-27 08:46:15 -0400358 f64 time_offset;
359
360 time_offset = (f64) lm->time_zero_timeval.tv_sec
361 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
Damjan Marion07a38572018-01-21 06:44:18 -0800362
363 while (count--)
364 {
365 e = vec_elt_at_index (lm->entries, i);
Paul Vinciguerrac9832e32019-10-30 14:43:08 -0400366 vlib_cli_output (vm, "%U %-10U %-14U %v",
Dave Barach10a690b2018-09-27 08:46:15 -0400367 format_time_float, 0, e->timestamp + time_offset,
Damjan Marion07a38572018-01-21 06:44:18 -0800368 format_vlib_log_level, e->level,
Damjan Marione78fab82018-04-18 17:03:28 +0200369 format_vlib_log_class, e->class, e->string);
Damjan Marion07a38572018-01-21 06:44:18 -0800370 i = (i + 1) % lm->size;
371 }
372
373 return error;
374}
375
376/* *INDENT-OFF* */
377VLIB_CLI_COMMAND (cli_show_log, static) = {
378 .path = "show logging",
379 .short_help = "show logging",
380 .function = show_log,
381};
382/* *INDENT-ON* */
383
384static clib_error_t *
Damjan Marione78fab82018-04-18 17:03:28 +0200385show_log_config (vlib_main_t * vm,
386 unformat_input_t * input, vlib_cli_command_t * cmd)
387{
388 clib_error_t *error = 0;
389 vlib_log_main_t *lm = &log_main;
390 vlib_log_class_data_t *c;
391 vlib_log_subclass_data_t *sc;
392
393 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
394 vlib_cli_output (vm, "Defaults:\n");
395 vlib_cli_output (vm, "%-20s %U", " Log Level:",
396 format_vlib_log_level, lm->default_log_level);
397 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
398 format_vlib_log_level, lm->default_syslog_log_level);
399 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
400 lm->default_rate_limit);
401 vlib_cli_output (vm, "\n");
402 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
403 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
404
Jerome Tollete4db8032018-10-02 22:54:30 +0200405
406 u8 *defstr = format (0, "default");
Damjan Marione78fab82018-04-18 17:03:28 +0200407 vec_foreach (c, lm->classes)
408 {
Jerome Tollete4db8032018-10-02 22:54:30 +0200409 vlib_cli_output (vm, "%v", c->name);
Damjan Marione78fab82018-04-18 17:03:28 +0200410 vec_foreach (sc, c->subclasses)
411 {
Jerome Tollete4db8032018-10-02 22:54:30 +0200412 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
413 sc->name ? sc->name : defstr,
Damjan Marione78fab82018-04-18 17:03:28 +0200414 format_vlib_log_level, sc->level,
415 format_vlib_log_level, sc->syslog_level,
416 sc->rate_limit);
417 }
418 }
Jerome Tollete4db8032018-10-02 22:54:30 +0200419 vec_free (defstr);
Damjan Marione78fab82018-04-18 17:03:28 +0200420
421 return error;
422}
423
424/* *INDENT-OFF* */
425VLIB_CLI_COMMAND (cli_show_log_config, static) = {
426 .path = "show logging configuration",
427 .short_help = "show logging configuration",
428 .function = show_log_config,
429};
430/* *INDENT-ON* */
431
432static clib_error_t *
Damjan Marion07a38572018-01-21 06:44:18 -0800433clear_log (vlib_main_t * vm,
434 unformat_input_t * input, vlib_cli_command_t * cmd)
435{
436 clib_error_t *error = 0;
437 vlib_log_main_t *lm = &log_main;
438 vlib_log_entry_t *e;
439 int i = last_log_entry ();
440 int count = lm->count;
441
442 while (count--)
443 {
444 e = vec_elt_at_index (lm->entries, i);
445 vec_free (e->string);
446 i = (i + 1) % lm->size;
447 }
448
449 lm->count = 0;
450 lm->next = 0;
451 vlib_log_info (lm->log_class, "log cleared");
452 return error;
453}
454
455/* *INDENT-OFF* */
456VLIB_CLI_COMMAND (cli_clear_log, static) = {
457 .path = "clear logging",
458 .short_help = "clear logging",
459 .function = clear_log,
460};
461/* *INDENT-ON* */
462
463static uword
464unformat_vlib_log_level (unformat_input_t * input, va_list * args)
465{
466 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
467 u8 *level_str = NULL;
468 uword rv = 1;
Steven526ea3e2018-04-25 11:29:00 -0700469 if (unformat (input, "%s", &level_str))
Damjan Marion07a38572018-01-21 06:44:18 -0800470 {
471#define _(v, uc, lc) \
472 const char __##uc[] = #lc; \
Steven526ea3e2018-04-25 11:29:00 -0700473 if (!strcmp ((const char *) level_str, __##uc)) \
Damjan Marion07a38572018-01-21 06:44:18 -0800474 { \
475 *level = VLIB_LOG_LEVEL_##uc; \
476 rv = 1; \
477 goto done; \
478 }
479 foreach_vlib_log_level;
480 rv = 0;
481#undef _
482 }
483done:
484 vec_free (level_str);
485 return rv;
486}
487
488static uword
489unformat_vlib_log_class (unformat_input_t * input, va_list * args)
490{
491 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
492 uword rv = 0;
493 u8 *class_str = NULL;
494 vlib_log_main_t *lm = &log_main;
495 if (unformat (input, "%v", &class_str))
496 {
497 vlib_log_class_data_t *cdata;
498 vec_foreach (cdata, lm->classes)
499 {
500 if (vec_is_equal (cdata->name, class_str))
501 {
502 *class = cdata;
503 rv = 1;
504 break;
505 }
506 }
507 }
508 vec_free (class_str);
509 return rv;
510}
511
512static clib_error_t *
513set_log_class (vlib_main_t * vm,
514 unformat_input_t * input, vlib_cli_command_t * cmd)
515{
516 unformat_input_t _line_input, *line_input = &_line_input;
517 clib_error_t *rv = NULL;
518 int rate_limit;
519 bool set_rate_limit = false;
520 bool set_level = false;
521 bool set_syslog_level = false;
522 vlib_log_level_t level;
523 vlib_log_level_t syslog_level;
524
525 /* Get a line of input. */
526 if (!unformat_user (input, unformat_line_input, line_input))
527 return 0;
528
529 vlib_log_class_data_t *class = NULL;
530 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
531 {
532 return clib_error_return (0, "unknown log class `%U'",
533 format_unformat_error, line_input);
534 }
535 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
536 {
537 if (unformat (line_input, "rate-limit %d", &rate_limit))
538 {
539 set_rate_limit = true;
540 }
541 else
542 if (unformat
543 (line_input, "level %U", unformat_vlib_log_level, &level))
544 {
545 set_level = true;
546 }
547 else
548 if (unformat
549 (line_input, "syslog-level %U", unformat_vlib_log_level,
550 &syslog_level))
551 {
552 set_syslog_level = true;
553 }
554 else
555 {
556 return clib_error_return (0, "unknown input `%U'",
557 format_unformat_error, line_input);
558 }
559 }
560
561 if (set_level)
562 {
563 vlib_log_subclass_data_t *subclass;
564 vec_foreach (subclass, class->subclasses)
565 {
566 subclass->level = level;
567 }
568 }
569 if (set_syslog_level)
570 {
571 vlib_log_subclass_data_t *subclass;
572 vec_foreach (subclass, class->subclasses)
573 {
574 subclass->syslog_level = syslog_level;
Damjan Marion07a38572018-01-21 06:44:18 -0800575 }
576 }
577 if (set_rate_limit)
578 {
579 vlib_log_subclass_data_t *subclass;
580 vec_foreach (subclass, class->subclasses)
581 {
582 subclass->rate_limit = rate_limit;
583 }
584 }
585
586 return rv;
587}
588
589/* *INDENT-OFF* */
590VLIB_CLI_COMMAND (cli_set_log, static) = {
591 .path = "set logging class",
Paul Vinciguerra43d8cf62019-10-30 11:14:58 -0400592 .short_help = "set logging class <class> [rate-limit <int>] "
Damjan Marion07a38572018-01-21 06:44:18 -0800593 "[level <level>] [syslog-level <level>]",
594 .function = set_log_class,
595};
596/* *INDENT-ON* */
597
598static clib_error_t *
599set_log_unth_time (vlib_main_t * vm,
600 unformat_input_t * input, vlib_cli_command_t * cmd)
601{
602 unformat_input_t _line_input, *line_input = &_line_input;
603 clib_error_t *rv = NULL;
604 int unthrottle_time;
605 vlib_log_main_t *lm = &log_main;
606
607 /* Get a line of input. */
608 if (!unformat_user (input, unformat_line_input, line_input))
609 return 0;
610
611 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
612 {
613 if (unformat (line_input, "%d", &unthrottle_time))
614 lm->unthrottle_time = unthrottle_time;
615 else
616 return clib_error_return (0, "unknown input `%U'",
617 format_unformat_error, line_input);
618 }
619
620 return rv;
621}
622
623/* *INDENT-OFF* */
624VLIB_CLI_COMMAND (cli_set_log_params, static) = {
625 .path = "set logging unthrottle-time",
626 .short_help = "set logging unthrottle-time <int>",
627 .function = set_log_unth_time,
628};
629/* *INDENT-ON* */
630
631static clib_error_t *
632set_log_size (vlib_main_t * vm,
633 unformat_input_t * input, vlib_cli_command_t * cmd)
634{
635 unformat_input_t _line_input, *line_input = &_line_input;
636 clib_error_t *rv = NULL;
637 int size;
638 vlib_log_main_t *lm = &log_main;
639
640 /* Get a line of input. */
641 if (!unformat_user (input, unformat_line_input, line_input))
642 return 0;
643
644 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
645 {
646 if (unformat (line_input, "%d", &size))
647 {
648 lm->size = size;
649 vec_validate (lm->entries, lm->size);
650 }
651 else
652 return clib_error_return (0, "unknown input `%U'",
653 format_unformat_error, line_input);
654 }
655
656 return rv;
657}
658
659/* *INDENT-OFF* */
660VLIB_CLI_COMMAND (cli_set_log_size, static) = {
661 .path = "set logging size",
662 .short_help = "set logging size <int>",
663 .function = set_log_size,
664};
665/* *INDENT-ON* */
666
667static uword
668unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
669{
670 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
671 vlib_log_subclass_data_t **subclass =
672 va_arg (*args, vlib_log_subclass_data_t **);
673 uword rv = 0;
674 u8 *subclass_str = NULL;
675 if (unformat (input, "%v", &subclass_str))
676 {
677 vlib_log_subclass_data_t *scdata;
678 vec_foreach (scdata, class->subclasses)
679 {
680 if (vec_is_equal (scdata->name, subclass_str))
681 {
682 rv = 1;
683 *subclass = scdata;
684 break;
685 }
686 }
687 }
688 vec_free (subclass_str);
689 return rv;
690}
691
692static clib_error_t *
693test_log_class_subclass (vlib_main_t * vm,
694 unformat_input_t * input, vlib_cli_command_t * cmd)
695{
696 unformat_input_t _line_input, *line_input = &_line_input;
697 /* Get a line of input. */
698 if (!unformat_user (input, unformat_line_input, line_input))
699 return 0;
700
701 vlib_log_class_data_t *class = NULL;
702 vlib_log_subclass_data_t *subclass = NULL;
703 vlib_log_level_t level;
704 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
705 {
706 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
707 {
708 if (unformat
709 (line_input, "%U", unformat_vlib_log_subclass, class,
710 &subclass))
711 {
712 vlib_log (level,
713 (class->index << 16) | (subclass->index), "%U",
714 format_unformat_input, line_input);
715 }
716 else
717 {
718 return clib_error_return (0,
719 "unknown log subclass near beginning of `%U'",
720 format_unformat_error, line_input);
721 }
722 }
723 else
724 {
725 return clib_error_return (0,
726 "unknown log class near beginning of `%U'",
727 format_unformat_error, line_input);
728 }
729 }
730 else
731 {
732 return clib_error_return (0, "unknown log level near beginning of `%U'",
733 format_unformat_error, line_input);
734 }
735 return 0;
736}
737
738/* *INDENT-OFF* */
739VLIB_CLI_COMMAND (cli_test_log, static) = {
740 .path = "test log",
Su Wang34321b32019-01-11 12:46:05 -0500741 .short_help = "test log <level> <class> <subclass> <message>",
Damjan Marion07a38572018-01-21 06:44:18 -0800742 .function = test_log_class_subclass,
743};
744/* *INDENT-ON* */
745
746static clib_error_t *
Damjan Marion055e6402020-10-21 19:43:36 +0200747log_config_class (vlib_main_t * vm, char *name, unformat_input_t * input)
748{
749 vlib_log_main_t *lm = &log_main;
750 vlib_log_class_config_t *cc, tmp;
751 uword *p;
752
753 if (lm->config_index_by_name == 0)
754 lm->config_index_by_name = hash_create_string (0, sizeof (uword));
755
756 p = hash_get_mem (lm->config_index_by_name, name);
757
758 if (p)
759 return clib_error_return (0, "logging class '%s' already configured",
760 name);
761
762 clib_memset_u8 (&tmp, 0xff, sizeof (vlib_log_class_config_t));
763
764 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
765 {
766 if (unformat (input, "level %U", unformat_vlib_log_level, &tmp.level))
767 ;
768 else if (unformat (input, "syslog-level %U", unformat_vlib_log_level,
769 &tmp.syslog_level))
770 ;
771 else if (unformat (input, "rate-limit %u", &tmp.rate_limit))
772 ;
773 else
774 return clib_error_return (0, "unknown input '%U'",
775 format_unformat_error, input);
776 }
777
778 vec_add2 (lm->configs, cc, 1);
779 clib_memcpy_fast (cc, &tmp, sizeof (vlib_log_class_config_t));
780 cc->name = name;
781 hash_set_mem (lm->config_index_by_name, name, cc - lm->configs);
782 return 0;
783}
784
785static clib_error_t *
Damjan Marion07a38572018-01-21 06:44:18 -0800786log_config (vlib_main_t * vm, unformat_input_t * input)
787{
788 vlib_log_main_t *lm = &log_main;
Damjan Marion055e6402020-10-21 19:43:36 +0200789 unformat_input_t sub_input;
790 u8 *class = 0;
Damjan Marion07a38572018-01-21 06:44:18 -0800791
792 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
793 {
Damjan Marione78fab82018-04-18 17:03:28 +0200794 if (unformat (input, "size %d", &lm->size))
795 vec_validate (lm->entries, lm->size);
Damjan Marion07a38572018-01-21 06:44:18 -0800796 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
Damjan Marione78fab82018-04-18 17:03:28 +0200797 ;
798 else if (unformat (input, "default-log-level %U",
799 unformat_vlib_log_level, &lm->default_log_level))
800 ;
801 else if (unformat (input, "default-syslog-log-level %U",
802 unformat_vlib_log_level,
803 &lm->default_syslog_log_level))
804 ;
Damjan Marion055e6402020-10-21 19:43:36 +0200805 else if (unformat (input, "class %s %U", &class,
806 unformat_vlib_cli_sub_input, &sub_input))
807 {
808 clib_error_t *err;
809 err = log_config_class (vm, (char *) class, &sub_input);
810 class = 0;
811 unformat_free (&sub_input);
812 if (err)
813 return err;
814 }
Damjan Marion07a38572018-01-21 06:44:18 -0800815 else
816 {
817 return unformat_parse_error (input);
818 }
819 }
820
821 return 0;
822}
823
824VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
825
826/*
827 * fd.io coding-style-patch-verification: ON
828 *
829 * Local Variables:
830 * eval: (c-set-style "gnu")
831 * End:
832 */