blob: 60fb9fb5178dae41f16ad5214af18d155108c5fb [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,
Florin Coras6bec05b2023-04-10 20:44:26 -070028 .add_to_elog = 0,
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 +020032VLIB_REGISTER_LOG_CLASS (log_log, static) = {
33 .class_name = "log",
34};
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020035
Damjan Marion06d82262020-10-21 12:43:40 +020036static const int colors[] = {
37 [VLIB_LOG_LEVEL_EMERG] = 1, /* red */
38 [VLIB_LOG_LEVEL_ALERT] = 1, /* red */
39 [VLIB_LOG_LEVEL_CRIT] = 1, /* red */
40 [VLIB_LOG_LEVEL_ERR] = 1, /* red */
41 [VLIB_LOG_LEVEL_WARNING] = 3, /* yellow */
42 [VLIB_LOG_LEVEL_NOTICE] = 2, /* green */
43 [VLIB_LOG_LEVEL_INFO] = 4, /* blue */
44 [VLIB_LOG_LEVEL_DEBUG] = 6, /* cyan */
45};
46
Damjan Marion4d7ad4a2020-10-23 21:52:50 +020047static const int log_level_to_syslog_priority[] = {
48 [VLIB_LOG_LEVEL_EMERG] = LOG_EMERG,
49 [VLIB_LOG_LEVEL_ALERT] = LOG_ALERT,
50 [VLIB_LOG_LEVEL_CRIT] = LOG_CRIT,
51 [VLIB_LOG_LEVEL_ERR] = LOG_ERR,
52 [VLIB_LOG_LEVEL_WARNING] = LOG_WARNING,
53 [VLIB_LOG_LEVEL_NOTICE] = LOG_NOTICE,
54 [VLIB_LOG_LEVEL_INFO] = LOG_INFO,
55 [VLIB_LOG_LEVEL_DEBUG] = LOG_DEBUG,
56 [VLIB_LOG_LEVEL_DISABLED] = LOG_DEBUG,
57};
58
Paul Vinciguerra03f1af22019-06-25 22:30:19 -040059int
Damjan Marion07a38572018-01-21 06:44:18 -080060last_log_entry ()
61{
62 vlib_log_main_t *lm = &log_main;
63 int i;
64
65 i = lm->next - lm->count;
66
67 if (i < 0)
68 i += lm->size;
69 return i;
70}
Damjan Marion07a38572018-01-21 06:44:18 -080071u8 *
72format_vlib_log_class (u8 * s, va_list * args)
73{
74 vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
Vratko Polake9ea7d52023-07-26 13:48:20 +020075 vlib_log_class_data_t *c = vlib_log_get_class_data (ci);
Damjan Marionbf179a92023-05-26 19:06:10 +000076 vlib_log_subclass_data_t *sc = vlib_log_get_subclass_data (ci);
Damjan Marion07a38572018-01-21 06:44:18 -080077
78 if (sc->name)
79 return format (s, "%v/%v", c->name, sc->name);
80 else
81 return format (s, "%v", c->name, 0);
82}
83
Damjan Marion06d82262020-10-21 12:43:40 +020084u8 *
85format_indent (u8 * s, va_list * args)
86{
87 u8 *v = va_arg (*args, u8 *);
88 u32 indent = va_arg (*args, u32);
89 u8 *c;
90
Damjan Marion06d82262020-10-21 12:43:40 +020091 vec_foreach (c, v)
92 {
93 vec_add (s, c, 1);
94 if (c[0] == '\n')
95 for (u32 i = 0; i < indent; i++)
96 vec_add1 (s, (u8) ' ');
97 }
Damjan Marion06d82262020-10-21 12:43:40 +020098 return s;
99}
100
101static int
102log_level_is_enabled (vlib_log_level_t level, vlib_log_level_t configured)
103{
104 if (configured == VLIB_LOG_LEVEL_DISABLED)
105 return 0;
106 if (level > configured)
107 return 0;
108 return 1;
109}
Damjan Marion07a38572018-01-21 06:44:18 -0800110
111void
112vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
113{
114 vlib_main_t *vm = vlib_get_main ();
115 vlib_log_main_t *lm = &log_main;
116 vlib_log_entry_t *e;
Damjan Marionbf179a92023-05-26 19:06:10 +0000117 vlib_log_subclass_data_t *sc = vlib_log_get_subclass_data (class);
Damjan Marion07a38572018-01-21 06:44:18 -0800118 va_list va;
119 f64 t = vlib_time_now (vm);
120 f64 delta = t - sc->last_event_timestamp;
Damjan Marion06d82262020-10-21 12:43:40 +0200121 int log_enabled = log_level_is_enabled (level, sc->level);
122 int syslog_enabled = log_level_is_enabled (level, sc->syslog_level);
Damjan Marion07a38572018-01-21 06:44:18 -0800123 u8 *s = 0;
Damjan Marion07a38572018-01-21 06:44:18 -0800124
125 /* make sure we are running on the main thread to avoid use in dataplane
126 code, for dataplane logging consider use of event-logger */
127 ASSERT (vlib_get_thread_index () == 0);
128
Damjan Marion06d82262020-10-21 12:43:40 +0200129 if ((log_enabled || syslog_enabled) == 0)
130 return;
131
132 vec_validate (lm->entries, lm->size);
Damjan Marion07a38572018-01-21 06:44:18 -0800133
134 if ((delta > lm->unthrottle_time) ||
135 (sc->is_throttling == 0 && (delta > 1)))
136 {
137 sc->last_event_timestamp = t;
138 sc->last_sec_count = 0;
139 sc->is_throttling = 0;
140 }
141 else
142 {
143 sc->last_sec_count++;
144 if (sc->last_sec_count > sc->rate_limit)
145 return;
146 else if (sc->last_sec_count == sc->rate_limit)
147 {
148 vec_reset_length (s);
Damjan Marion06d82262020-10-21 12:43:40 +0200149 s = format (s, "--- message(s) throttled ---");
Damjan Marion07a38572018-01-21 06:44:18 -0800150 sc->is_throttling = 1;
151 }
152 }
153
154 if (s == 0)
155 {
156 va_start (va, fmt);
157 s = va_format (s, fmt, &va);
158 va_end (va);
159 }
160
Damjan Marion06d82262020-10-21 12:43:40 +0200161 if (syslog_enabled)
Damjan Marion07a38572018-01-21 06:44:18 -0800162 {
Damjan Marion06d82262020-10-21 12:43:40 +0200163 u8 *l = 0;
164 if (unix_main.flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG))
Damjan Marion07a38572018-01-21 06:44:18 -0800165 {
Damjan Marion06d82262020-10-21 12:43:40 +0200166 int indent = 0;
167 int with_colors = (unix_main.flags & UNIX_FLAG_NOCOLOR) == 0;
168 u8 *fmt;
169 if (with_colors)
170 {
171 l = format (l, "\x1b[%um", 90 + colors[level]);
172 indent = vec_len (l);
173 }
174 fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length);
jiangxiaoming94a92aa2020-10-22 10:08:36 +0800175 vec_terminate_c_string (fmt);
Damjan Marion06d82262020-10-21 12:43:40 +0200176 l = format (l, (char *) fmt, format_vlib_log_class, class,
177 format_vlib_log_level, level);
178 vec_free (fmt);
179 indent = vec_len (l) - indent;
180 if (with_colors)
181 l = format (l, "\x1b[0m");
182 l = format (l, "%U", format_indent, s, indent);
183 fformat (stderr, "%v\n", l);
184 fflush (stderr);
Damjan Marion07a38572018-01-21 06:44:18 -0800185 }
186 else
187 {
Damjan Marion06d82262020-10-21 12:43:40 +0200188 l = format (l, "%U", format_vlib_log_class, class);
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200189 int prio = log_level_to_syslog_priority[level];
Damjan Marion06d82262020-10-21 12:43:40 +0200190 int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
191
192 syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
193 (int) vec_len (s) - is_term, s);
Damjan Marion07a38572018-01-21 06:44:18 -0800194 }
Damjan Marion06d82262020-10-21 12:43:40 +0200195 vec_free (l);
Damjan Marion07a38572018-01-21 06:44:18 -0800196 }
197
Damjan Marion06d82262020-10-21 12:43:40 +0200198 if (log_enabled)
199 {
200 e = vec_elt_at_index (lm->entries, lm->next);
201 vec_free (e->string);
202 e->level = level;
203 e->class = class;
204 e->string = s;
205 e->timestamp = t;
206 s = 0;
207
Dave Barachbc867c32020-11-25 10:07:09 -0500208 if (lm->add_to_elog)
209 {
luoyaozu2d3da802022-11-23 15:59:17 +0800210 ELOG_TYPE_DECLARE(ee) =
Dave Barachbc867c32020-11-25 10:07:09 -0500211 {
212 .format = "log-%s: %s",
213 .format_args = "t4T4",
luoyaozu2d3da802022-11-23 15:59:17 +0800214 .n_enum_strings = VLIB_LOG_N_LEVELS,
Dave Barachbc867c32020-11-25 10:07:09 -0500215 .enum_strings = {
luoyaozu2d3da802022-11-23 15:59:17 +0800216 "unknown",
Dave Barachbc867c32020-11-25 10:07:09 -0500217 "emerg",
218 "alert",
219 "crit",
220 "err",
221 "warn",
222 "notice",
223 "info",
224 "debug",
225 "disabled",
226 },
227 };
luoyaozu2d3da802022-11-23 15:59:17 +0800228 struct
229 {
230 u32 log_level;
231 u32 string_index;
232 } * ed;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100233 ed = ELOG_DATA (&vlib_global_main.elog_main, ee);
Dave Barachbc867c32020-11-25 10:07:09 -0500234 ed->log_level = level;
Damjan Marionf553a2c2021-03-26 13:45:37 +0100235 ed->string_index =
Damjan Marion4926cdd2021-12-22 12:25:49 +0100236 elog_string (&vlib_global_main.elog_main, "%v%c", e->string, 0);
Dave Barachbc867c32020-11-25 10:07:09 -0500237 }
238
Damjan Marion06d82262020-10-21 12:43:40 +0200239 lm->next = (lm->next + 1) % lm->size;
240 if (lm->size > lm->count)
241 lm->count++;
242 }
243
244 vec_free (s);
Damjan Marion07a38572018-01-21 06:44:18 -0800245}
246
Dave Barach8dc954a2020-02-05 17:31:09 -0500247static vlib_log_class_t
248vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
Damjan Marion07a38572018-01-21 06:44:18 -0800249{
250 vlib_log_main_t *lm = &log_main;
251 vlib_log_class_data_t *c = NULL;
252 vlib_log_subclass_data_t *s;
253 vlib_log_class_data_t *tmp;
Damjan Marion055e6402020-10-21 19:43:36 +0200254 vlib_log_class_config_t *cc = 0, *scc = 0;
255 uword *p;
256 u8 *str;
Damjan Marion06d82262020-10-21 12:43:40 +0200257 u32 length = 0;
258
Damjan Marion055e6402020-10-21 19:43:36 +0200259 if ((p = hash_get_mem (lm->config_index_by_name, class)))
260 cc = vec_elt_at_index (lm->configs, p[0]);
261
262 str = format (0, "%s/%s%c", class, subclass, 0);
263 if ((p = hash_get_mem (lm->config_index_by_name, (char *) str)))
264 scc = vec_elt_at_index (lm->configs, p[0]);
265 vec_free (str);
266
Damjan Marion07a38572018-01-21 06:44:18 -0800267 vec_foreach (tmp, lm->classes)
268 {
Su Wang34321b32019-01-11 12:46:05 -0500269 if (vec_len (tmp->name) != strlen (class))
270 continue;
Damjan Marion07a38572018-01-21 06:44:18 -0800271 if (!memcmp (class, tmp->name, vec_len (tmp->name)))
272 {
273 c = tmp;
274 break;
275 }
276 }
277 if (!c)
278 {
279 vec_add2 (lm->classes, c, 1);
280 c->index = c - lm->classes;
281 c->name = format (0, "%s", class);
282 }
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200283 length = vec_len (c->name);
Damjan Marion07a38572018-01-21 06:44:18 -0800284
285 vec_add2 (c->subclasses, s, 1);
286 s->index = s - c->subclasses;
287 s->name = subclass ? format (0, "%s", subclass) : 0;
Damjan Marion055e6402020-10-21 19:43:36 +0200288
289 if (scc && scc->rate_limit != ~0)
290 s->rate_limit = scc->rate_limit;
291 else if (cc && cc->rate_limit != ~0)
292 s->rate_limit = cc->rate_limit;
293 else if (limit)
294 s->rate_limit = limit;
295 else
296 s->rate_limit = lm->default_rate_limit;
297
298 if (scc && scc->level != ~0)
299 s->level = scc->level;
300 else if (cc && cc->level != ~0)
301 s->level = cc->level;
302 else
303 s->level = lm->default_log_level;
304
305 if (scc && scc->syslog_level != ~0)
306 s->syslog_level = scc->syslog_level;
307 else if (cc && cc->syslog_level != ~0)
308 s->syslog_level = cc->syslog_level;
309 else
310 s->syslog_level = lm->default_syslog_log_level;
311
Damjan Marion06d82262020-10-21 12:43:40 +0200312 if (subclass)
313 length += 1 + vec_len (s->name);
314 if (length > lm->max_class_name_length)
315 lm->max_class_name_length = length;
Damjan Marion07a38572018-01-21 06:44:18 -0800316 return (c->index << 16) | (s->index);
317}
318
Dave Barach8dc954a2020-02-05 17:31:09 -0500319vlib_log_class_t
320vlib_log_register_class (char *class, char *subclass)
321{
322 return vlib_log_register_class_internal (class, subclass,
323 0 /* default rate limit */ );
324}
325
326vlib_log_class_t
327vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit)
328{
329 return vlib_log_register_class_internal (class, subclass, limit);
330}
331
332
Damjan Marion07a38572018-01-21 06:44:18 -0800333u8 *
334format_vlib_log_level (u8 * s, va_list * args)
335{
336 vlib_log_level_t i = va_arg (*args, vlib_log_level_t);
337 char *t = 0;
338
339 switch (i)
340 {
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200341#define _(uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
Damjan Marion07a38572018-01-21 06:44:18 -0800342 foreach_vlib_log_level
343#undef _
344 default:
345 return format (s, "unknown");
346 }
347 return format (s, "%s", t);
348}
349
Damjan Marion62d656a2022-03-09 16:10:54 +0100350clib_error_t *
351vlib_log_init (vlib_main_t *vm)
Damjan Marion07a38572018-01-21 06:44:18 -0800352{
353 vlib_log_main_t *lm = &log_main;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200354 vlib_log_class_registration_t *r = lm->registrations;
Dave Barach10a690b2018-09-27 08:46:15 -0400355
356 gettimeofday (&lm->time_zero_timeval, 0);
357 lm->time_zero = vlib_time_now (vm);
358
Damjan Marion07a38572018-01-21 06:44:18 -0800359 vec_validate (lm->entries, lm->size);
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200360
361 while (r)
362 {
363 r->class = vlib_log_register_class (r->class_name, r->subclass_name);
364 if (r->default_level)
Damjan Marionbf179a92023-05-26 19:06:10 +0000365 vlib_log_get_subclass_data (r->class)->level = r->default_level;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200366 if (r->default_syslog_level)
Damjan Marionbf179a92023-05-26 19:06:10 +0000367 vlib_log_get_subclass_data (r->class)->syslog_level =
368 r->default_syslog_level;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200369 r = r->next;
370 }
371
372 r = lm->registrations;
373 while (r)
374 {
375 vlib_log_debug (r->class, "initialized");
376 r = r->next;
377 }
Damjan Marion07a38572018-01-21 06:44:18 -0800378 return 0;
379}
380
Damjan Marion07a38572018-01-21 06:44:18 -0800381static clib_error_t *
382show_log (vlib_main_t * vm,
383 unformat_input_t * input, vlib_cli_command_t * cmd)
384{
385 clib_error_t *error = 0;
386 vlib_log_main_t *lm = &log_main;
387 vlib_log_entry_t *e;
388 int i = last_log_entry ();
389 int count = lm->count;
Dave Barach10a690b2018-09-27 08:46:15 -0400390 f64 time_offset;
391
392 time_offset = (f64) lm->time_zero_timeval.tv_sec
393 + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero;
Damjan Marion07a38572018-01-21 06:44:18 -0800394
395 while (count--)
396 {
397 e = vec_elt_at_index (lm->entries, i);
Andreas Schultz972dc172020-05-15 11:50:07 +0200398 vlib_cli_output (vm, "%U %-10U %-14U %v", format_time_float, NULL,
399 e->timestamp + time_offset, format_vlib_log_level,
400 e->level, format_vlib_log_class, e->class, e->string);
Damjan Marion07a38572018-01-21 06:44:18 -0800401 i = (i + 1) % lm->size;
402 }
403
404 return error;
405}
406
Damjan Marion07a38572018-01-21 06:44:18 -0800407VLIB_CLI_COMMAND (cli_show_log, static) = {
408 .path = "show logging",
409 .short_help = "show logging",
410 .function = show_log,
411};
Damjan Marion07a38572018-01-21 06:44:18 -0800412
413static clib_error_t *
Damjan Marione78fab82018-04-18 17:03:28 +0200414show_log_config (vlib_main_t * vm,
415 unformat_input_t * input, vlib_cli_command_t * cmd)
416{
417 clib_error_t *error = 0;
418 vlib_log_main_t *lm = &log_main;
419 vlib_log_class_data_t *c;
420 vlib_log_subclass_data_t *sc;
421
422 vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size);
423 vlib_cli_output (vm, "Defaults:\n");
424 vlib_cli_output (vm, "%-20s %U", " Log Level:",
425 format_vlib_log_level, lm->default_log_level);
426 vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:",
427 format_vlib_log_level, lm->default_syslog_log_level);
428 vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:",
429 lm->default_rate_limit);
430 vlib_cli_output (vm, "\n");
431 vlib_cli_output (vm, "%-22s %-14s %-14s %s",
432 "Class/Subclass", "Level", "Syslog Level", "Rate Limit");
433
Jerome Tollete4db8032018-10-02 22:54:30 +0200434
435 u8 *defstr = format (0, "default");
Damjan Marione78fab82018-04-18 17:03:28 +0200436 vec_foreach (c, lm->classes)
437 {
Jerome Tollete4db8032018-10-02 22:54:30 +0200438 vlib_cli_output (vm, "%v", c->name);
Damjan Marione78fab82018-04-18 17:03:28 +0200439 vec_foreach (sc, c->subclasses)
440 {
Jerome Tollete4db8032018-10-02 22:54:30 +0200441 vlib_cli_output (vm, " %-20v %-14U %-14U %d",
442 sc->name ? sc->name : defstr,
Damjan Marione78fab82018-04-18 17:03:28 +0200443 format_vlib_log_level, sc->level,
444 format_vlib_log_level, sc->syslog_level,
445 sc->rate_limit);
446 }
447 }
Jerome Tollete4db8032018-10-02 22:54:30 +0200448 vec_free (defstr);
Damjan Marione78fab82018-04-18 17:03:28 +0200449
450 return error;
451}
452
Damjan Marione78fab82018-04-18 17:03:28 +0200453VLIB_CLI_COMMAND (cli_show_log_config, static) = {
454 .path = "show logging configuration",
455 .short_help = "show logging configuration",
456 .function = show_log_config,
457};
Damjan Marione78fab82018-04-18 17:03:28 +0200458
459static clib_error_t *
Damjan Marion07a38572018-01-21 06:44:18 -0800460clear_log (vlib_main_t * vm,
461 unformat_input_t * input, vlib_cli_command_t * cmd)
462{
463 clib_error_t *error = 0;
464 vlib_log_main_t *lm = &log_main;
465 vlib_log_entry_t *e;
466 int i = last_log_entry ();
467 int count = lm->count;
468
469 while (count--)
470 {
471 e = vec_elt_at_index (lm->entries, i);
472 vec_free (e->string);
473 i = (i + 1) % lm->size;
474 }
475
476 lm->count = 0;
477 lm->next = 0;
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200478 vlib_log_info (log_log.class, "log cleared");
Damjan Marion07a38572018-01-21 06:44:18 -0800479 return error;
480}
481
Damjan Marion07a38572018-01-21 06:44:18 -0800482VLIB_CLI_COMMAND (cli_clear_log, static) = {
483 .path = "clear logging",
484 .short_help = "clear logging",
485 .function = clear_log,
486};
Damjan Marion07a38572018-01-21 06:44:18 -0800487
488static uword
489unformat_vlib_log_level (unformat_input_t * input, va_list * args)
490{
491 vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *);
492 u8 *level_str = NULL;
493 uword rv = 1;
Steven526ea3e2018-04-25 11:29:00 -0700494 if (unformat (input, "%s", &level_str))
Damjan Marion07a38572018-01-21 06:44:18 -0800495 {
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200496#define _(uc, lc) \
Damjan Marion07a38572018-01-21 06:44:18 -0800497 const char __##uc[] = #lc; \
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200498 if (!strcmp ((const char *) level_str, __##uc)) \
Damjan Marion07a38572018-01-21 06:44:18 -0800499 { \
Damjan Marion4d7ad4a2020-10-23 21:52:50 +0200500 *level = VLIB_LOG_LEVEL_##uc; \
Damjan Marion07a38572018-01-21 06:44:18 -0800501 rv = 1; \
502 goto done; \
503 }
504 foreach_vlib_log_level;
505 rv = 0;
506#undef _
507 }
508done:
509 vec_free (level_str);
510 return rv;
511}
512
513static uword
514unformat_vlib_log_class (unformat_input_t * input, va_list * args)
515{
516 vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **);
517 uword rv = 0;
518 u8 *class_str = NULL;
519 vlib_log_main_t *lm = &log_main;
520 if (unformat (input, "%v", &class_str))
521 {
522 vlib_log_class_data_t *cdata;
523 vec_foreach (cdata, lm->classes)
524 {
525 if (vec_is_equal (cdata->name, class_str))
526 {
527 *class = cdata;
528 rv = 1;
529 break;
530 }
531 }
532 }
533 vec_free (class_str);
534 return rv;
535}
536
537static clib_error_t *
538set_log_class (vlib_main_t * vm,
539 unformat_input_t * input, vlib_cli_command_t * cmd)
540{
541 unformat_input_t _line_input, *line_input = &_line_input;
542 clib_error_t *rv = NULL;
543 int rate_limit;
544 bool set_rate_limit = false;
545 bool set_level = false;
546 bool set_syslog_level = false;
547 vlib_log_level_t level;
548 vlib_log_level_t syslog_level;
549
550 /* Get a line of input. */
551 if (!unformat_user (input, unformat_line_input, line_input))
552 return 0;
553
554 vlib_log_class_data_t *class = NULL;
555 if (!unformat (line_input, "%U", unformat_vlib_log_class, &class))
556 {
557 return clib_error_return (0, "unknown log class `%U'",
558 format_unformat_error, line_input);
559 }
560 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
561 {
562 if (unformat (line_input, "rate-limit %d", &rate_limit))
563 {
564 set_rate_limit = true;
565 }
566 else
567 if (unformat
568 (line_input, "level %U", unformat_vlib_log_level, &level))
569 {
570 set_level = true;
571 }
572 else
573 if (unformat
574 (line_input, "syslog-level %U", unformat_vlib_log_level,
575 &syslog_level))
576 {
577 set_syslog_level = true;
578 }
579 else
580 {
581 return clib_error_return (0, "unknown input `%U'",
582 format_unformat_error, line_input);
583 }
584 }
585
586 if (set_level)
587 {
588 vlib_log_subclass_data_t *subclass;
589 vec_foreach (subclass, class->subclasses)
590 {
591 subclass->level = level;
592 }
593 }
594 if (set_syslog_level)
595 {
596 vlib_log_subclass_data_t *subclass;
597 vec_foreach (subclass, class->subclasses)
598 {
599 subclass->syslog_level = syslog_level;
Damjan Marion07a38572018-01-21 06:44:18 -0800600 }
601 }
602 if (set_rate_limit)
603 {
604 vlib_log_subclass_data_t *subclass;
605 vec_foreach (subclass, class->subclasses)
606 {
607 subclass->rate_limit = rate_limit;
608 }
609 }
610
611 return rv;
612}
613
Damjan Marion07a38572018-01-21 06:44:18 -0800614VLIB_CLI_COMMAND (cli_set_log, static) = {
615 .path = "set logging class",
Paul Vinciguerra43d8cf62019-10-30 11:14:58 -0400616 .short_help = "set logging class <class> [rate-limit <int>] "
Damjan Marion07a38572018-01-21 06:44:18 -0800617 "[level <level>] [syslog-level <level>]",
618 .function = set_log_class,
619};
Damjan Marion07a38572018-01-21 06:44:18 -0800620
621static clib_error_t *
622set_log_unth_time (vlib_main_t * vm,
623 unformat_input_t * input, vlib_cli_command_t * cmd)
624{
625 unformat_input_t _line_input, *line_input = &_line_input;
626 clib_error_t *rv = NULL;
627 int unthrottle_time;
628 vlib_log_main_t *lm = &log_main;
629
630 /* Get a line of input. */
631 if (!unformat_user (input, unformat_line_input, line_input))
632 return 0;
633
634 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
635 {
636 if (unformat (line_input, "%d", &unthrottle_time))
637 lm->unthrottle_time = unthrottle_time;
638 else
639 return clib_error_return (0, "unknown input `%U'",
640 format_unformat_error, line_input);
641 }
642
643 return rv;
644}
645
Damjan Marion07a38572018-01-21 06:44:18 -0800646VLIB_CLI_COMMAND (cli_set_log_params, static) = {
647 .path = "set logging unthrottle-time",
648 .short_help = "set logging unthrottle-time <int>",
649 .function = set_log_unth_time,
650};
Damjan Marion07a38572018-01-21 06:44:18 -0800651
652static clib_error_t *
653set_log_size (vlib_main_t * vm,
654 unformat_input_t * input, vlib_cli_command_t * cmd)
655{
656 unformat_input_t _line_input, *line_input = &_line_input;
657 clib_error_t *rv = NULL;
658 int size;
659 vlib_log_main_t *lm = &log_main;
660
661 /* Get a line of input. */
662 if (!unformat_user (input, unformat_line_input, line_input))
663 return 0;
664
665 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
666 {
667 if (unformat (line_input, "%d", &size))
668 {
669 lm->size = size;
670 vec_validate (lm->entries, lm->size);
671 }
672 else
673 return clib_error_return (0, "unknown input `%U'",
674 format_unformat_error, line_input);
675 }
676
677 return rv;
678}
679
Damjan Marion07a38572018-01-21 06:44:18 -0800680VLIB_CLI_COMMAND (cli_set_log_size, static) = {
681 .path = "set logging size",
682 .short_help = "set logging size <int>",
683 .function = set_log_size,
684};
Damjan Marion07a38572018-01-21 06:44:18 -0800685
686static uword
687unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
688{
689 vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *);
690 vlib_log_subclass_data_t **subclass =
691 va_arg (*args, vlib_log_subclass_data_t **);
692 uword rv = 0;
693 u8 *subclass_str = NULL;
694 if (unformat (input, "%v", &subclass_str))
695 {
696 vlib_log_subclass_data_t *scdata;
697 vec_foreach (scdata, class->subclasses)
698 {
699 if (vec_is_equal (scdata->name, subclass_str))
700 {
701 rv = 1;
702 *subclass = scdata;
703 break;
704 }
705 }
706 }
707 vec_free (subclass_str);
708 return rv;
709}
710
711static clib_error_t *
712test_log_class_subclass (vlib_main_t * vm,
713 unformat_input_t * input, vlib_cli_command_t * cmd)
714{
715 unformat_input_t _line_input, *line_input = &_line_input;
716 /* Get a line of input. */
717 if (!unformat_user (input, unformat_line_input, line_input))
718 return 0;
719
720 vlib_log_class_data_t *class = NULL;
721 vlib_log_subclass_data_t *subclass = NULL;
722 vlib_log_level_t level;
723 if (unformat (line_input, "%U", unformat_vlib_log_level, &level))
724 {
725 if (unformat (line_input, "%U", unformat_vlib_log_class, &class))
726 {
727 if (unformat
728 (line_input, "%U", unformat_vlib_log_subclass, class,
729 &subclass))
730 {
731 vlib_log (level,
732 (class->index << 16) | (subclass->index), "%U",
733 format_unformat_input, line_input);
734 }
735 else
736 {
737 return clib_error_return (0,
738 "unknown log subclass near beginning of `%U'",
739 format_unformat_error, line_input);
740 }
741 }
742 else
743 {
744 return clib_error_return (0,
745 "unknown log class near beginning of `%U'",
746 format_unformat_error, line_input);
747 }
748 }
749 else
750 {
751 return clib_error_return (0, "unknown log level near beginning of `%U'",
752 format_unformat_error, line_input);
753 }
754 return 0;
755}
756
Damjan Marion07a38572018-01-21 06:44:18 -0800757VLIB_CLI_COMMAND (cli_test_log, static) = {
758 .path = "test log",
Su Wang34321b32019-01-11 12:46:05 -0500759 .short_help = "test log <level> <class> <subclass> <message>",
Damjan Marion07a38572018-01-21 06:44:18 -0800760 .function = test_log_class_subclass,
761};
Damjan Marion07a38572018-01-21 06:44:18 -0800762
763static clib_error_t *
Damjan Marion055e6402020-10-21 19:43:36 +0200764log_config_class (vlib_main_t * vm, char *name, unformat_input_t * input)
765{
766 vlib_log_main_t *lm = &log_main;
767 vlib_log_class_config_t *cc, tmp;
768 uword *p;
769
770 if (lm->config_index_by_name == 0)
771 lm->config_index_by_name = hash_create_string (0, sizeof (uword));
772
773 p = hash_get_mem (lm->config_index_by_name, name);
774
775 if (p)
776 return clib_error_return (0, "logging class '%s' already configured",
777 name);
778
779 clib_memset_u8 (&tmp, 0xff, sizeof (vlib_log_class_config_t));
780
781 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
782 {
783 if (unformat (input, "level %U", unformat_vlib_log_level, &tmp.level))
784 ;
785 else if (unformat (input, "syslog-level %U", unformat_vlib_log_level,
786 &tmp.syslog_level))
787 ;
788 else if (unformat (input, "rate-limit %u", &tmp.rate_limit))
789 ;
790 else
791 return clib_error_return (0, "unknown input '%U'",
792 format_unformat_error, input);
793 }
794
795 vec_add2 (lm->configs, cc, 1);
796 clib_memcpy_fast (cc, &tmp, sizeof (vlib_log_class_config_t));
797 cc->name = name;
798 hash_set_mem (lm->config_index_by_name, name, cc - lm->configs);
799 return 0;
800}
801
802static clib_error_t *
Damjan Marion07a38572018-01-21 06:44:18 -0800803log_config (vlib_main_t * vm, unformat_input_t * input)
804{
805 vlib_log_main_t *lm = &log_main;
Damjan Marion055e6402020-10-21 19:43:36 +0200806 unformat_input_t sub_input;
807 u8 *class = 0;
Damjan Marion07a38572018-01-21 06:44:18 -0800808
809 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
810 {
Damjan Marione78fab82018-04-18 17:03:28 +0200811 if (unformat (input, "size %d", &lm->size))
812 vec_validate (lm->entries, lm->size);
Damjan Marion07a38572018-01-21 06:44:18 -0800813 else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time))
Damjan Marione78fab82018-04-18 17:03:28 +0200814 ;
815 else if (unformat (input, "default-log-level %U",
816 unformat_vlib_log_level, &lm->default_log_level))
817 ;
818 else if (unformat (input, "default-syslog-log-level %U",
819 unformat_vlib_log_level,
820 &lm->default_syslog_log_level))
821 ;
Dave Barachbc867c32020-11-25 10:07:09 -0500822 else if (unformat (input, "add-to-elog"))
823 lm->add_to_elog = 1;
Damjan Marion055e6402020-10-21 19:43:36 +0200824 else if (unformat (input, "class %s %U", &class,
825 unformat_vlib_cli_sub_input, &sub_input))
826 {
827 clib_error_t *err;
828 err = log_config_class (vm, (char *) class, &sub_input);
829 class = 0;
830 unformat_free (&sub_input);
831 if (err)
832 return err;
833 }
Damjan Marion07a38572018-01-21 06:44:18 -0800834 else
835 {
836 return unformat_parse_error (input);
837 }
838 }
839
840 return 0;
841}
842
843VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging");
844
845/*
846 * fd.io coding-style-patch-verification: ON
847 *
848 * Local Variables:
849 * eval: (c-set-style "gnu")
850 * End:
851 */