blob: f176062fd0cb322cbe3ae86e2373dcb209b33a1a [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>
Dave Barachbc867c32020-11-25 10:07:09 -050021#include <vppinfra/elog.h>
Damjan Marion07a38572018-01-21 06:44:18 -080022
Damjan Marione78fab82018-04-18 17:03:28 +020023vlib_log_main_t log_main = {
24 .default_log_level = VLIB_LOG_LEVEL_NOTICE,
25 .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING,
26 .unthrottle_time = 3,
27 .size = 512,
Dave Barachbc867c32020-11-25 10:07:09 -050028 .add_to_elog = 1,
Damjan Marione78fab82018-04-18 17:03:28 +020029 .default_rate_limit = 50,
30};
Damjan Marion07a38572018-01-21 06:44:18 -080031
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020032/* *INDENT-OFF* */
33VLIB_REGISTER_LOG_CLASS (log_log, static) = {
34 .class_name = "log",
35};
36/* *INDENT-ON* */
37
Damjan Marion06d82262020-10-21 12:43:40 +020038static 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 */
47};
48
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020049static 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,
59};
60
Paul Vinciguerra03f1af22019-06-25 22:30:19 -040061int
Damjan Marion07a38572018-01-21 06:44:18 -080062last_log_entry ()
63{
64 vlib_log_main_t *lm = &log_main;
65 int i;
66
67 i = lm->next - lm->count;
68
69 if (i < 0)
70 i += lm->size;
71 return i;
72}
73
74static vlib_log_class_data_t *
75get_class_data (vlib_log_class_t ci)
76{
77 vlib_log_main_t *lm = &log_main;
78 return vec_elt_at_index (lm->classes, (ci >> 16));
79}
80
81static vlib_log_subclass_data_t *
82get_subclass_data (vlib_log_class_t ci)
83{
84 vlib_log_class_data_t *c = get_class_data (ci);
85 return vec_elt_at_index (c->subclasses, (ci & 0xffff));
86}
87
Damjan Marion07a38572018-01-21 06:44:18 -080088u8 *
89format_vlib_log_class (u8 * s, va_list * args)
90{
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);
94
95 if (sc->name)
96 return format (s, "%v/%v", c->name, sc->name);
97 else
98 return format (s, "%v", c->name, 0);
99}
100
Damjan Marion06d82262020-10-21 12:43:40 +0200101u8 *
102format_indent (u8 * s, va_list * args)
103{
104 u8 *v = va_arg (*args, u8 *);
105 u32 indent = va_arg (*args, u32);
106 u8 *c;
107
108 /* *INDENT-OFF* */
109 vec_foreach (c, v)
110 {
111 vec_add (s, c, 1);
112 if (c[0] == '\n')
113 for (u32 i = 0; i < indent; i++)
114 vec_add1 (s, (u8) ' ');
115 }
116 /* *INDENT-ON* */
117 return s;
118}
119
120static int
121log_level_is_enabled (vlib_log_level_t level, vlib_log_level_t configured)
122{
123 if (configured == VLIB_LOG_LEVEL_DISABLED)
124 return 0;
125 if (level > configured)
126 return 0;
127 return 1;
128}
Damjan Marion07a38572018-01-21 06:44:18 -0800129
130void
131vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
132{
133 vlib_main_t *vm = vlib_get_main ();
134 vlib_log_main_t *lm = &log_main;
135 vlib_log_entry_t *e;
136 vlib_log_subclass_data_t *sc = get_subclass_data (class);
137 va_list va;
138 f64 t = vlib_time_now (vm);
139 f64 delta = t - sc->last_event_timestamp;
Damjan Marion06d82262020-10-21 12:43:40 +0200140 int log_enabled = log_level_is_enabled (level, sc->level);
141 int syslog_enabled = log_level_is_enabled (level, sc->syslog_level);
Damjan Marion07a38572018-01-21 06:44:18 -0800142 u8 *s = 0;
Damjan Marion07a38572018-01-21 06:44:18 -0800143
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);
147
Damjan Marion06d82262020-10-21 12:43:40 +0200148 if ((log_enabled || syslog_enabled) == 0)
149 return;
150
151 vec_validate (lm->entries, lm->size);
Damjan Marion07a38572018-01-21 06:44:18 -0800152
153 if ((delta > lm->unthrottle_time) ||
154 (sc->is_throttling == 0 && (delta > 1)))
155 {
156 sc->last_event_timestamp = t;
157 sc->last_sec_count = 0;
158 sc->is_throttling = 0;
159 }
160 else
161 {
162 sc->last_sec_count++;
163 if (sc->last_sec_count > sc->rate_limit)
164 return;
165 else if (sc->last_sec_count == sc->rate_limit)
166 {
167 vec_reset_length (s);
Damjan Marion06d82262020-10-21 12:43:40 +0200168 s = format (s, "--- message(s) throttled ---");
Damjan Marion07a38572018-01-21 06:44:18 -0800169 sc->is_throttling = 1;
170 }
171 }
172
173 if (s == 0)
174 {
175 va_start (va, fmt);
176 s = va_format (s, fmt, &va);
177 va_end (va);
178 }
179
Damjan Marion06d82262020-10-21 12:43:40 +0200180 if (syslog_enabled)
Damjan Marion07a38572018-01-21 06:44:18 -0800181 {
Damjan Marion06d82262020-10-21 12:43:40 +0200182 u8 *l = 0;
183 if (unix_main.flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG))
Damjan Marion07a38572018-01-21 06:44:18 -0800184 {
Damjan Marion06d82262020-10-21 12:43:40 +0200185 int indent = 0;
186 int with_colors = (unix_main.flags & UNIX_FLAG_NOCOLOR) == 0;
187 u8 *fmt;
188 if (with_colors)
189 {
190 l = format (l, "\x1b[%um", 90 + colors[level]);
191 indent = vec_len (l);
192 }
193 fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length);
jiangxiaoming94a92aa2020-10-22 10:08:36 +0800194 vec_terminate_c_string (fmt);
Damjan Marion06d82262020-10-21 12:43:40 +0200195 l = format (l, (char *) fmt, format_vlib_log_class, class,
196 format_vlib_log_level, level);
197 vec_free (fmt);
198 indent = vec_len (l) - indent;
199 if (with_colors)
200 l = format (l, "\x1b[0m");
201 l = format (l, "%U", format_indent, s, indent);
202 fformat (stderr, "%v\n", l);
203 fflush (stderr);
Damjan Marion07a38572018-01-21 06:44:18 -0800204 }
205 else
206 {
Damjan Marion06d82262020-10-21 12:43:40 +0200207 l = format (l, "%U", format_vlib_log_class, class);
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200208 int prio = log_level_to_syslog_priority[level];
Damjan Marion06d82262020-10-21 12:43:40 +0200209 int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
210
211 syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
212 (int) vec_len (s) - is_term, s);
Damjan Marion07a38572018-01-21 06:44:18 -0800213 }
Damjan Marion06d82262020-10-21 12:43:40 +0200214 vec_free (l);
Damjan Marion07a38572018-01-21 06:44:18 -0800215 }
216
Damjan Marion06d82262020-10-21 12:43:40 +0200217 if (log_enabled)
218 {
219 e = vec_elt_at_index (lm->entries, lm->next);
220 vec_free (e->string);
221 e->level = level;
222 e->class = class;
223 e->string = s;
224 e->timestamp = t;
225 s = 0;
226
Dave Barachbc867c32020-11-25 10:07:09 -0500227 if (lm->add_to_elog)
228 {
luoyaozu2d3da802022-11-23 15:59:17 +0800229 ELOG_TYPE_DECLARE(ee) =
Dave Barachbc867c32020-11-25 10:07:09 -0500230 {
231 .format = "log-%s: %s",
232 .format_args = "t4T4",
luoyaozu2d3da802022-11-23 15:59:17 +0800233 .n_enum_strings = VLIB_LOG_N_LEVELS,
Dave Barachbc867c32020-11-25 10:07:09 -0500234 .enum_strings = {
luoyaozu2d3da802022-11-23 15:59:17 +0800235 "unknown",
Dave Barachbc867c32020-11-25 10:07:09 -0500236 "emerg",
237 "alert",
238 "crit",
239 "err",
240 "warn",
241 "notice",
242 "info",
243 "debug",
244 "disabled",
245 },
246 };
luoyaozu2d3da802022-11-23 15:59:17 +0800247 struct
248 {
249 u32 log_level;
250 u32 string_index;
251 } * ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100252 ed = ELOG_DATA (&vlib_global_main.elog_main, ee);
Dave Barachbc867c32020-11-25 10:07:09 -0500253 ed->log_level = level;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100254 ed->string_index =
Damjan Marion4926cdd2021-12-22 12:25:49 +0100255 elog_string (&vlib_global_main.elog_main, "%v%c", e->string, 0);
Dave Barachbc867c32020-11-25 10:07:09 -0500256 }
257
Damjan Marion06d82262020-10-21 12:43:40 +0200258 lm->next = (lm->next + 1) % lm->size;
259 if (lm->size > lm->count)
260 lm->count++;
261 }
262
263 vec_free (s);
Damjan Marion07a38572018-01-21 06:44:18 -0800264}
265
Dave Barach8dc954a2020-02-05 17:31:09 -0500266static vlib_log_class_t
267vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
Damjan Marion07a38572018-01-21 06:44:18 -0800268{
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;
Damjan Marion055e6402020-10-21 19:43:36 +0200273 vlib_log_class_config_t *cc = 0, *scc = 0;
274 uword *p;
275 u8 *str;
Damjan Marion06d82262020-10-21 12:43:40 +0200276 u32 length = 0;
277
Damjan Marion055e6402020-10-21 19:43:36 +0200278 if ((p = hash_get_mem (lm->config_index_by_name, class)))
279 cc = vec_elt_at_index (lm->configs, p[0]);
280
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]);
284 vec_free (str);
285
Damjan Marion07a38572018-01-21 06:44:18 -0800286 vec_foreach (tmp, lm->classes)
287 {
Su Wang34321b32019-01-11 12:46:05 -0500288 if (vec_len (tmp->name) != strlen (class))
289 continue;
Damjan Marion07a38572018-01-21 06:44:18 -0800290 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
291 {
292 c = tmp;
293 break;
294 }
295 }
296 if (!c)
297 {
298 vec_add2 (lm->classes, c, 1);
299 c->index = c - lm->classes;
300 c->name = format (0, "%s", class);
301 }
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200302 length = vec_len (c->name);
Damjan Marion07a38572018-01-21 06:44:18 -0800303
304 vec_add2 (c->subclasses, s, 1);
305 s->index = s - c->subclasses;
306 s->name = subclass ? format (0, "%s", subclass) : 0;
Damjan Marion055e6402020-10-21 19:43:36 +0200307
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;
312 else if (limit)
313 s->rate_limit = limit;
314 else
315 s->rate_limit = lm->default_rate_limit;
316
317 if (scc && scc->level != ~0)
318 s->level = scc->level;
319 else if (cc && cc->level != ~0)
320 s->level = cc->level;
321 else
322 s->level = lm->default_log_level;
323
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;
328 else
329 s->syslog_level = lm->default_syslog_log_level;
330
Damjan Marion06d82262020-10-21 12:43:40 +0200331 if (subclass)
332 length += 1 + vec_len (s->name);
333 if (length > lm->max_class_name_length)
334 lm->max_class_name_length = length;
Damjan Marion07a38572018-01-21 06:44:18 -0800335 return (c->index << 16) | (s->index);
336}
337
Dave Barach8dc954a2020-02-05 17:31:09 -0500338vlib_log_class_t
339vlib_log_register_class (char *class, char *subclass)
340{
341 return vlib_log_register_class_internal (class, subclass,
342 0 /* default rate limit */ );
343}
344
345vlib_log_class_t
346vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit)
347{
348 return vlib_log_register_class_internal (class, subclass, limit);
349}
350
351
Damjan Marion07a38572018-01-21 06:44:18 -0800352u8 *
353format_vlib_log_level (u8 * s, va_list * args)
354{
355 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
356 char *t = 0;
357
358 switch (i)
359 {
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200360#define _(uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
Damjan Marion07a38572018-01-21 06:44:18 -0800361 foreach_vlib_log_level
362#undef _
363 default:
364 return format (s, "unknown");
365 }
366 return format (s, "%s", t);
367}
368
Damjan Marion62d656a2022-03-09 16:10:54 +0100369clib_error_t *
370vlib_log_init (vlib_main_t *vm)
Damjan Marion07a38572018-01-21 06:44:18 -0800371{
372 vlib_log_main_t *lm = &log_main;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200373 vlib_log_class_registration_t *r = lm->registrations;
Dave Barach10a690b2018-09-27 08:46:15 -0400374
375 gettimeofday (&lm->time_zero_timeval, 0);
376 lm->time_zero = vlib_time_now (vm);
377
Damjan Marion07a38572018-01-21 06:44:18 -0800378 vec_validate (lm->entries, lm->size);
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200379
380 while (r)
381 {
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;
387 r = r->next;
388 }
389
390 r = lm->registrations;
391 while (r)
392 {
393 vlib_log_debug (r->class, "initialized");
394 r = r->next;
395 }
Damjan Marion07a38572018-01-21 06:44:18 -0800396 return 0;
397}
398
Damjan Marion07a38572018-01-21 06:44:18 -0800399static clib_error_t *
400show_log (vlib_main_t * vm,
401 unformat_input_t * input, vlib_cli_command_t * cmd)
402{
403 clib_error_t *error = 0;
404 vlib_log_main_t *lm = &log_main;
405 vlib_log_entry_t *e;
406 int i = last_log_entry ();
407 int count = lm->count;
Dave Barach10a690b2018-09-27 08:46:15 -0400408 f64 time_offset;
409
410 time_offset = (f64) lm->time_zero_timeval.tv_sec
411 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
Damjan Marion07a38572018-01-21 06:44:18 -0800412
413 while (count--)
414 {
415 e = vec_elt_at_index (lm->entries, i);
Andreas Schultz972dc172020-05-15 11:50:07 +0200416 vlib_cli_output (vm, "%U %-10U %-14U %v", format_time_float, NULL,
417 e->timestamp + time_offset, format_vlib_log_level,
418 e->level, format_vlib_log_class, e->class, e->string);
Damjan Marion07a38572018-01-21 06:44:18 -0800419 i = (i + 1) % lm->size;
420 }
421
422 return error;
423}
424
425/* *INDENT-OFF* */
426VLIB_CLI_COMMAND (cli_show_log, static) = {
427 .path = "show logging",
428 .short_help = "show logging",
429 .function = show_log,
430};
431/* *INDENT-ON* */
432
433static clib_error_t *
Damjan Marione78fab82018-04-18 17:03:28 +0200434show_log_config (vlib_main_t * vm,
435 unformat_input_t * input, vlib_cli_command_t * cmd)
436{
437 clib_error_t *error = 0;
438 vlib_log_main_t *lm = &log_main;
439 vlib_log_class_data_t *c;
440 vlib_log_subclass_data_t *sc;
441
442 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
443 vlib_cli_output (vm, "Defaults:\n");
444 vlib_cli_output (vm, "%-20s %U", " Log Level:",
445 format_vlib_log_level, lm->default_log_level);
446 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
447 format_vlib_log_level, lm->default_syslog_log_level);
448 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
449 lm->default_rate_limit);
450 vlib_cli_output (vm, "\n");
451 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
452 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
453
Jerome Tollete4db8032018-10-02 22:54:30 +0200454
455 u8 *defstr = format (0, "default");
Damjan Marione78fab82018-04-18 17:03:28 +0200456 vec_foreach (c, lm->classes)
457 {
Jerome Tollete4db8032018-10-02 22:54:30 +0200458 vlib_cli_output (vm, "%v", c->name);
Damjan Marione78fab82018-04-18 17:03:28 +0200459 vec_foreach (sc, c->subclasses)
460 {
Jerome Tollete4db8032018-10-02 22:54:30 +0200461 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
462 sc->name ? sc->name : defstr,
Damjan Marione78fab82018-04-18 17:03:28 +0200463 format_vlib_log_level, sc->level,
464 format_vlib_log_level, sc->syslog_level,
465 sc->rate_limit);
466 }
467 }
Jerome Tollete4db8032018-10-02 22:54:30 +0200468 vec_free (defstr);
Damjan Marione78fab82018-04-18 17:03:28 +0200469
470 return error;
471}
472
473/* *INDENT-OFF* */
474VLIB_CLI_COMMAND (cli_show_log_config, static) = {
475 .path = "show logging configuration",
476 .short_help = "show logging configuration",
477 .function = show_log_config,
478};
479/* *INDENT-ON* */
480
481static clib_error_t *
Damjan Marion07a38572018-01-21 06:44:18 -0800482clear_log (vlib_main_t * vm,
483 unformat_input_t * input, vlib_cli_command_t * cmd)
484{
485 clib_error_t *error = 0;
486 vlib_log_main_t *lm = &log_main;
487 vlib_log_entry_t *e;
488 int i = last_log_entry ();
489 int count = lm->count;
490
491 while (count--)
492 {
493 e = vec_elt_at_index (lm->entries, i);
494 vec_free (e->string);
495 i = (i + 1) % lm->size;
496 }
497
498 lm->count = 0;
499 lm->next = 0;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200500 vlib_log_info (log_log.class, "log cleared");
Damjan Marion07a38572018-01-21 06:44:18 -0800501 return error;
502}
503
504/* *INDENT-OFF* */
505VLIB_CLI_COMMAND (cli_clear_log, static) = {
506 .path = "clear logging",
507 .short_help = "clear logging",
508 .function = clear_log,
509};
510/* *INDENT-ON* */
511
512static uword
513unformat_vlib_log_level (unformat_input_t * input, va_list * args)
514{
515 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
516 u8 *level_str = NULL;
517 uword rv = 1;
Steven526ea3e2018-04-25 11:29:00 -0700518 if (unformat (input, "%s", &level_str))
Damjan Marion07a38572018-01-21 06:44:18 -0800519 {
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200520#define _(uc, lc) \
Damjan Marion07a38572018-01-21 06:44:18 -0800521 const char __##uc[] = #lc; \
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200522 if (!strcmp ((const char *) level_str, __##uc)) \
Damjan Marion07a38572018-01-21 06:44:18 -0800523 { \
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200524 *level = VLIB_LOG_LEVEL_##uc; \
Damjan Marion07a38572018-01-21 06:44:18 -0800525 rv = 1; \
526 goto done; \
527 }
528 foreach_vlib_log_level;
529 rv = 0;
530#undef _
531 }
532done:
533 vec_free (level_str);
534 return rv;
535}
536
537static uword
538unformat_vlib_log_class (unformat_input_t * input, va_list * args)
539{
540 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
541 uword rv = 0;
542 u8 *class_str = NULL;
543 vlib_log_main_t *lm = &log_main;
544 if (unformat (input, "%v", &class_str))
545 {
546 vlib_log_class_data_t *cdata;
547 vec_foreach (cdata, lm->classes)
548 {
549 if (vec_is_equal (cdata->name, class_str))
550 {
551 *class = cdata;
552 rv = 1;
553 break;
554 }
555 }
556 }
557 vec_free (class_str);
558 return rv;
559}
560
561static clib_error_t *
562set_log_class (vlib_main_t * vm,
563 unformat_input_t * input, vlib_cli_command_t * cmd)
564{
565 unformat_input_t _line_input, *line_input = &_line_input;
566 clib_error_t *rv = NULL;
567 int rate_limit;
568 bool set_rate_limit = false;
569 bool set_level = false;
570 bool set_syslog_level = false;
571 vlib_log_level_t level;
572 vlib_log_level_t syslog_level;
573
574 /* Get a line of input. */
575 if (!unformat_user (input, unformat_line_input, line_input))
576 return 0;
577
578 vlib_log_class_data_t *class = NULL;
579 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
580 {
581 return clib_error_return (0, "unknown log class `%U'",
582 format_unformat_error, line_input);
583 }
584 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
585 {
586 if (unformat (line_input, "rate-limit %d", &rate_limit))
587 {
588 set_rate_limit = true;
589 }
590 else
591 if (unformat
592 (line_input, "level %U", unformat_vlib_log_level, &level))
593 {
594 set_level = true;
595 }
596 else
597 if (unformat
598 (line_input, "syslog-level %U", unformat_vlib_log_level,
599 &syslog_level))
600 {
601 set_syslog_level = true;
602 }
603 else
604 {
605 return clib_error_return (0, "unknown input `%U'",
606 format_unformat_error, line_input);
607 }
608 }
609
610 if (set_level)
611 {
612 vlib_log_subclass_data_t *subclass;
613 vec_foreach (subclass, class->subclasses)
614 {
615 subclass->level = level;
616 }
617 }
618 if (set_syslog_level)
619 {
620 vlib_log_subclass_data_t *subclass;
621 vec_foreach (subclass, class->subclasses)
622 {
623 subclass->syslog_level = syslog_level;
Damjan Marion07a38572018-01-21 06:44:18 -0800624 }
625 }
626 if (set_rate_limit)
627 {
628 vlib_log_subclass_data_t *subclass;
629 vec_foreach (subclass, class->subclasses)
630 {
631 subclass->rate_limit = rate_limit;
632 }
633 }
634
635 return rv;
636}
637
638/* *INDENT-OFF* */
639VLIB_CLI_COMMAND (cli_set_log, static) = {
640 .path = "set logging class",
Paul Vinciguerra43d8cf62019-10-30 11:14:58 -0400641 .short_help = "set logging class <class> [rate-limit <int>] "
Damjan Marion07a38572018-01-21 06:44:18 -0800642 "[level <level>] [syslog-level <level>]",
643 .function = set_log_class,
644};
645/* *INDENT-ON* */
646
647static clib_error_t *
648set_log_unth_time (vlib_main_t * vm,
649 unformat_input_t * input, vlib_cli_command_t * cmd)
650{
651 unformat_input_t _line_input, *line_input = &_line_input;
652 clib_error_t *rv = NULL;
653 int unthrottle_time;
654 vlib_log_main_t *lm = &log_main;
655
656 /* Get a line of input. */
657 if (!unformat_user (input, unformat_line_input, line_input))
658 return 0;
659
660 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
661 {
662 if (unformat (line_input, "%d", &unthrottle_time))
663 lm->unthrottle_time = unthrottle_time;
664 else
665 return clib_error_return (0, "unknown input `%U'",
666 format_unformat_error, line_input);
667 }
668
669 return rv;
670}
671
672/* *INDENT-OFF* */
673VLIB_CLI_COMMAND (cli_set_log_params, static) = {
674 .path = "set logging unthrottle-time",
675 .short_help = "set logging unthrottle-time <int>",
676 .function = set_log_unth_time,
677};
678/* *INDENT-ON* */
679
680static clib_error_t *
681set_log_size (vlib_main_t * vm,
682 unformat_input_t * input, vlib_cli_command_t * cmd)
683{
684 unformat_input_t _line_input, *line_input = &_line_input;
685 clib_error_t *rv = NULL;
686 int size;
687 vlib_log_main_t *lm = &log_main;
688
689 /* Get a line of input. */
690 if (!unformat_user (input, unformat_line_input, line_input))
691 return 0;
692
693 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
694 {
695 if (unformat (line_input, "%d", &size))
696 {
697 lm->size = size;
698 vec_validate (lm->entries, lm->size);
699 }
700 else
701 return clib_error_return (0, "unknown input `%U'",
702 format_unformat_error, line_input);
703 }
704
705 return rv;
706}
707
708/* *INDENT-OFF* */
709VLIB_CLI_COMMAND (cli_set_log_size, static) = {
710 .path = "set logging size",
711 .short_help = "set logging size <int>",
712 .function = set_log_size,
713};
714/* *INDENT-ON* */
715
716static uword
717unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
718{
719 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
720 vlib_log_subclass_data_t **subclass =
721 va_arg (*args, vlib_log_subclass_data_t **);
722 uword rv = 0;
723 u8 *subclass_str = NULL;
724 if (unformat (input, "%v", &subclass_str))
725 {
726 vlib_log_subclass_data_t *scdata;
727 vec_foreach (scdata, class->subclasses)
728 {
729 if (vec_is_equal (scdata->name, subclass_str))
730 {
731 rv = 1;
732 *subclass = scdata;
733 break;
734 }
735 }
736 }
737 vec_free (subclass_str);
738 return rv;
739}
740
741static clib_error_t *
742test_log_class_subclass (vlib_main_t * vm,
743 unformat_input_t * input, vlib_cli_command_t * cmd)
744{
745 unformat_input_t _line_input, *line_input = &_line_input;
746 /* Get a line of input. */
747 if (!unformat_user (input, unformat_line_input, line_input))
748 return 0;
749
750 vlib_log_class_data_t *class = NULL;
751 vlib_log_subclass_data_t *subclass = NULL;
752 vlib_log_level_t level;
753 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
754 {
755 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
756 {
757 if (unformat
758 (line_input, "%U", unformat_vlib_log_subclass, class,
759 &subclass))
760 {
761 vlib_log (level,
762 (class->index << 16) | (subclass->index), "%U",
763 format_unformat_input, line_input);
764 }
765 else
766 {
767 return clib_error_return (0,
768 "unknown log subclass near beginning of `%U'",
769 format_unformat_error, line_input);
770 }
771 }
772 else
773 {
774 return clib_error_return (0,
775 "unknown log class near beginning of `%U'",
776 format_unformat_error, line_input);
777 }
778 }
779 else
780 {
781 return clib_error_return (0, "unknown log level near beginning of `%U'",
782 format_unformat_error, line_input);
783 }
784 return 0;
785}
786
787/* *INDENT-OFF* */
788VLIB_CLI_COMMAND (cli_test_log, static) = {
789 .path = "test log",
Su Wang34321b32019-01-11 12:46:05 -0500790 .short_help = "test log <level> <class> <subclass> <message>",
Damjan Marion07a38572018-01-21 06:44:18 -0800791 .function = test_log_class_subclass,
792};
793/* *INDENT-ON* */
794
795static clib_error_t *
Damjan Marion055e6402020-10-21 19:43:36 +0200796log_config_class (vlib_main_t * vm, char *name, unformat_input_t * input)
797{
798 vlib_log_main_t *lm = &log_main;
799 vlib_log_class_config_t *cc, tmp;
800 uword *p;
801
802 if (lm->config_index_by_name == 0)
803 lm->config_index_by_name = hash_create_string (0, sizeof (uword));
804
805 p = hash_get_mem (lm->config_index_by_name, name);
806
807 if (p)
808 return clib_error_return (0, "logging class '%s' already configured",
809 name);
810
811 clib_memset_u8 (&tmp, 0xff, sizeof (vlib_log_class_config_t));
812
813 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
814 {
815 if (unformat (input, "level %U", unformat_vlib_log_level, &tmp.level))
816 ;
817 else if (unformat (input, "syslog-level %U", unformat_vlib_log_level,
818 &tmp.syslog_level))
819 ;
820 else if (unformat (input, "rate-limit %u", &tmp.rate_limit))
821 ;
822 else
823 return clib_error_return (0, "unknown input '%U'",
824 format_unformat_error, input);
825 }
826
827 vec_add2 (lm->configs, cc, 1);
828 clib_memcpy_fast (cc, &tmp, sizeof (vlib_log_class_config_t));
829 cc->name = name;
830 hash_set_mem (lm->config_index_by_name, name, cc - lm->configs);
831 return 0;
832}
833
834static clib_error_t *
Damjan Marion07a38572018-01-21 06:44:18 -0800835log_config (vlib_main_t * vm, unformat_input_t * input)
836{
837 vlib_log_main_t *lm = &log_main;
Damjan Marion055e6402020-10-21 19:43:36 +0200838 unformat_input_t sub_input;
839 u8 *class = 0;
Damjan Marion07a38572018-01-21 06:44:18 -0800840
841 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
842 {
Damjan Marione78fab82018-04-18 17:03:28 +0200843 if (unformat (input, "size %d", &lm->size))
844 vec_validate (lm->entries, lm->size);
Damjan Marion07a38572018-01-21 06:44:18 -0800845 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
Damjan Marione78fab82018-04-18 17:03:28 +0200846 ;
847 else if (unformat (input, "default-log-level %U",
848 unformat_vlib_log_level, &lm->default_log_level))
849 ;
850 else if (unformat (input, "default-syslog-log-level %U",
851 unformat_vlib_log_level,
852 &lm->default_syslog_log_level))
853 ;
Dave Barachbc867c32020-11-25 10:07:09 -0500854 else if (unformat (input, "add-to-elog"))
855 lm->add_to_elog = 1;
Damjan Marion055e6402020-10-21 19:43:36 +0200856 else if (unformat (input, "class %s %U", &class,
857 unformat_vlib_cli_sub_input, &sub_input))
858 {
859 clib_error_t *err;
860 err = log_config_class (vm, (char *) class, &sub_input);
861 class = 0;
862 unformat_free (&sub_input);
863 if (err)
864 return err;
865 }
Damjan Marion07a38572018-01-21 06:44:18 -0800866 else
867 {
868 return unformat_parse_error (input);
869 }
870 }
871
872 return 0;
873}
874
875VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
876
877/*
878 * fd.io coding-style-patch-verification: ON
879 *
880 * Local Variables:
881 * eval: (c-set-style "gnu")
882 * End:
883 */