Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 19 | #include <syslog.h> |
| 20 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 21 | typedef struct |
| 22 | { |
| 23 | vlib_log_level_t level; |
| 24 | vlib_log_class_t class; |
| 25 | f64 timestamp; |
| 26 | u8 *string; |
| 27 | } vlib_log_entry_t; |
| 28 | |
| 29 | typedef struct |
| 30 | { |
| 31 | u32 index; |
| 32 | u8 *name; |
| 33 | // level of log messages kept for this subclass |
| 34 | vlib_log_level_t level; |
| 35 | // level of log messages sent to syslog for this subclass |
| 36 | vlib_log_level_t syslog_level; |
| 37 | // flag saying whether this subclass is logged to syslog |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 38 | f64 last_event_timestamp; |
| 39 | int last_sec_count; |
| 40 | int is_throttling; |
| 41 | int rate_limit; |
| 42 | } vlib_log_subclass_data_t; |
| 43 | |
| 44 | typedef struct |
| 45 | { |
| 46 | u32 index; |
| 47 | u8 *name; |
| 48 | vlib_log_subclass_data_t *subclasses; |
| 49 | } vlib_log_class_data_t; |
| 50 | |
| 51 | typedef struct |
| 52 | { |
| 53 | vlib_log_entry_t *entries; |
| 54 | vlib_log_class_data_t *classes; |
| 55 | int size, next, count; |
| 56 | |
| 57 | /* our own log class */ |
| 58 | vlib_log_class_t log_class; |
| 59 | |
| 60 | int default_rate_limit; |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 61 | int default_log_level; |
| 62 | int default_syslog_log_level; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 63 | int unthrottle_time; |
| 64 | u32 indent; |
Dave Barach | 10a690b | 2018-09-27 08:46:15 -0400 | [diff] [blame] | 65 | |
| 66 | /* time zero */ |
| 67 | struct timeval time_zero_timeval; |
| 68 | f64 time_zero; |
| 69 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 70 | } vlib_log_main_t; |
| 71 | |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 72 | vlib_log_main_t log_main = { |
| 73 | .default_log_level = VLIB_LOG_LEVEL_NOTICE, |
| 74 | .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING, |
| 75 | .unthrottle_time = 3, |
| 76 | .size = 512, |
| 77 | .default_rate_limit = 50, |
| 78 | }; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 79 | |
| 80 | static int |
| 81 | last_log_entry () |
| 82 | { |
| 83 | vlib_log_main_t *lm = &log_main; |
| 84 | int i; |
| 85 | |
| 86 | i = lm->next - lm->count; |
| 87 | |
| 88 | if (i < 0) |
| 89 | i += lm->size; |
| 90 | return i; |
| 91 | } |
| 92 | |
| 93 | static vlib_log_class_data_t * |
| 94 | get_class_data (vlib_log_class_t ci) |
| 95 | { |
| 96 | vlib_log_main_t *lm = &log_main; |
| 97 | return vec_elt_at_index (lm->classes, (ci >> 16)); |
| 98 | } |
| 99 | |
| 100 | static vlib_log_subclass_data_t * |
| 101 | get_subclass_data (vlib_log_class_t ci) |
| 102 | { |
| 103 | vlib_log_class_data_t *c = get_class_data (ci); |
| 104 | return vec_elt_at_index (c->subclasses, (ci & 0xffff)); |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | vlib_log_level_to_syslog_priority (vlib_log_level_t level) |
| 109 | { |
| 110 | switch (level) |
| 111 | { |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 112 | #define LOG_DISABLED LOG_DEBUG |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 113 | #define _(n,uc,lc) \ |
| 114 | case VLIB_LOG_LEVEL_##uc:\ |
| 115 | return LOG_##uc; |
| 116 | foreach_vlib_log_level |
| 117 | #undef _ |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 118 | #undef LOG_DISABLED |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 119 | } |
| 120 | return LOG_DEBUG; |
| 121 | } |
| 122 | |
| 123 | u8 * |
| 124 | format_vlib_log_class (u8 * s, va_list * args) |
| 125 | { |
| 126 | vlib_log_class_t ci = va_arg (*args, vlib_log_class_t); |
| 127 | vlib_log_class_data_t *c = get_class_data (ci); |
| 128 | vlib_log_subclass_data_t *sc = get_subclass_data (ci); |
| 129 | |
| 130 | if (sc->name) |
| 131 | return format (s, "%v/%v", c->name, sc->name); |
| 132 | else |
| 133 | return format (s, "%v", c->name, 0); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void |
| 138 | vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...) |
| 139 | { |
| 140 | vlib_main_t *vm = vlib_get_main (); |
| 141 | vlib_log_main_t *lm = &log_main; |
| 142 | vlib_log_entry_t *e; |
| 143 | vlib_log_subclass_data_t *sc = get_subclass_data (class); |
| 144 | va_list va; |
| 145 | f64 t = vlib_time_now (vm); |
| 146 | f64 delta = t - sc->last_event_timestamp; |
| 147 | u8 *s = 0; |
| 148 | bool use_formatted_log_entry = true; |
| 149 | |
Mohsin Kazmi | 2f63bc9 | 2018-05-18 16:27:26 +0200 | [diff] [blame] | 150 | vec_validate (lm->entries, lm->size); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 151 | /* make sure we are running on the main thread to avoid use in dataplane |
| 152 | code, for dataplane logging consider use of event-logger */ |
| 153 | ASSERT (vlib_get_thread_index () == 0); |
| 154 | |
| 155 | if (level > sc->level) |
| 156 | { |
| 157 | use_formatted_log_entry = false; |
| 158 | goto syslog; |
| 159 | } |
| 160 | |
| 161 | if ((delta > lm->unthrottle_time) || |
| 162 | (sc->is_throttling == 0 && (delta > 1))) |
| 163 | { |
| 164 | sc->last_event_timestamp = t; |
| 165 | sc->last_sec_count = 0; |
| 166 | sc->is_throttling = 0; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | sc->last_sec_count++; |
| 171 | if (sc->last_sec_count > sc->rate_limit) |
| 172 | return; |
| 173 | else if (sc->last_sec_count == sc->rate_limit) |
| 174 | { |
| 175 | vec_reset_length (s); |
| 176 | s = format (0, "--- message(s) throttled ---"); |
| 177 | sc->is_throttling = 1; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (s == 0) |
| 182 | { |
| 183 | va_start (va, fmt); |
| 184 | s = va_format (s, fmt, &va); |
| 185 | va_end (va); |
| 186 | } |
| 187 | |
| 188 | e = vec_elt_at_index (lm->entries, lm->next); |
| 189 | vec_free (e->string); |
| 190 | e->level = level; |
| 191 | e->class = class; |
| 192 | e->string = s; |
| 193 | e->timestamp = t; |
| 194 | |
| 195 | lm->next = (lm->next + 1) % lm->size; |
| 196 | if (lm->size > lm->count) |
| 197 | lm->count++; |
| 198 | |
| 199 | syslog: |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 200 | if (sc->syslog_level != VLIB_LOG_LEVEL_DISABLED && |
| 201 | level <= sc->syslog_level) |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 202 | { |
| 203 | u8 *tmp = format (NULL, "%U", format_vlib_log_class, class); |
| 204 | if (use_formatted_log_entry) |
| 205 | { |
| 206 | syslog (vlib_log_level_to_syslog_priority (level), "%.*s: %.*s", |
Pierre Pfister | ffb3813 | 2018-08-20 14:21:07 +0200 | [diff] [blame] | 207 | (int) vec_len (tmp), tmp, |
| 208 | (int) (vec_len (s) - |
| 209 | (vec_c_string_is_terminated (s) ? 1 : 0)), s); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 210 | } |
| 211 | else |
| 212 | { |
| 213 | tmp = format (tmp, ": "); |
| 214 | va_start (va, fmt); |
| 215 | tmp = va_format (tmp, fmt, &va); |
| 216 | va_end (va); |
| 217 | syslog (vlib_log_level_to_syslog_priority (level), "%.*s", |
Pierre Pfister | ffb3813 | 2018-08-20 14:21:07 +0200 | [diff] [blame] | 218 | (int) (vec_len (tmp) - |
| 219 | (vec_c_string_is_terminated (tmp) ? 1 : 0)), tmp); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 220 | } |
| 221 | vec_free (tmp); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | vlib_log_class_t |
| 227 | vlib_log_register_class (char *class, char *subclass) |
| 228 | { |
| 229 | vlib_log_main_t *lm = &log_main; |
| 230 | vlib_log_class_data_t *c = NULL; |
| 231 | vlib_log_subclass_data_t *s; |
| 232 | vlib_log_class_data_t *tmp; |
| 233 | vec_foreach (tmp, lm->classes) |
| 234 | { |
| 235 | if (!memcmp (class, tmp->name, vec_len (tmp->name))) |
| 236 | { |
| 237 | c = tmp; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | if (!c) |
| 242 | { |
| 243 | vec_add2 (lm->classes, c, 1); |
| 244 | c->index = c - lm->classes; |
| 245 | c->name = format (0, "%s", class); |
| 246 | } |
| 247 | |
| 248 | vec_add2 (c->subclasses, s, 1); |
| 249 | s->index = s - c->subclasses; |
| 250 | s->name = subclass ? format (0, "%s", subclass) : 0; |
| 251 | s->rate_limit = lm->default_rate_limit; |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 252 | s->level = lm->default_log_level; |
| 253 | s->syslog_level = lm->default_syslog_log_level; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 254 | return (c->index << 16) | (s->index); |
| 255 | } |
| 256 | |
| 257 | u8 * |
| 258 | format_vlib_log_level (u8 * s, va_list * args) |
| 259 | { |
| 260 | vlib_log_level_t i = va_arg (*args, vlib_log_level_t); |
| 261 | char *t = 0; |
| 262 | |
| 263 | switch (i) |
| 264 | { |
| 265 | #define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break; |
| 266 | foreach_vlib_log_level |
| 267 | #undef _ |
| 268 | default: |
| 269 | return format (s, "unknown"); |
| 270 | } |
| 271 | return format (s, "%s", t); |
| 272 | } |
| 273 | |
| 274 | u32 |
| 275 | vlib_log_get_indent () |
| 276 | { |
| 277 | return log_main.indent; |
| 278 | } |
| 279 | |
| 280 | static clib_error_t * |
| 281 | vlib_log_init (vlib_main_t * vm) |
| 282 | { |
| 283 | vlib_log_main_t *lm = &log_main; |
Dave Barach | 10a690b | 2018-09-27 08:46:15 -0400 | [diff] [blame] | 284 | |
| 285 | gettimeofday (&lm->time_zero_timeval, 0); |
| 286 | lm->time_zero = vlib_time_now (vm); |
| 287 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 288 | vec_validate (lm->entries, lm->size); |
| 289 | lm->log_class = vlib_log_register_class ("log", 0); |
| 290 | u8 *tmp = format (NULL, "%U %-10U %-10U ", format_time_float, 0, (f64) 0, |
| 291 | format_white_space, 255, format_white_space, 255); |
| 292 | log_main.indent = vec_len (tmp); |
| 293 | vec_free (tmp); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | VLIB_INIT_FUNCTION (vlib_log_init); |
| 298 | |
| 299 | |
| 300 | static clib_error_t * |
| 301 | show_log (vlib_main_t * vm, |
| 302 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 303 | { |
| 304 | clib_error_t *error = 0; |
| 305 | vlib_log_main_t *lm = &log_main; |
| 306 | vlib_log_entry_t *e; |
| 307 | int i = last_log_entry (); |
| 308 | int count = lm->count; |
Dave Barach | 10a690b | 2018-09-27 08:46:15 -0400 | [diff] [blame] | 309 | f64 time_offset; |
| 310 | |
| 311 | time_offset = (f64) lm->time_zero_timeval.tv_sec |
| 312 | + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 313 | |
| 314 | while (count--) |
| 315 | { |
| 316 | e = vec_elt_at_index (lm->entries, i); |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 317 | vlib_cli_output (vm, "%U %-10U %-10U %v", |
Dave Barach | 10a690b | 2018-09-27 08:46:15 -0400 | [diff] [blame] | 318 | format_time_float, 0, e->timestamp + time_offset, |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 319 | format_vlib_log_level, e->level, |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 320 | format_vlib_log_class, e->class, e->string); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 321 | i = (i + 1) % lm->size; |
| 322 | } |
| 323 | |
| 324 | return error; |
| 325 | } |
| 326 | |
| 327 | /* *INDENT-OFF* */ |
| 328 | VLIB_CLI_COMMAND (cli_show_log, static) = { |
| 329 | .path = "show logging", |
| 330 | .short_help = "show logging", |
| 331 | .function = show_log, |
| 332 | }; |
| 333 | /* *INDENT-ON* */ |
| 334 | |
| 335 | static clib_error_t * |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 336 | show_log_config (vlib_main_t * vm, |
| 337 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 338 | { |
| 339 | clib_error_t *error = 0; |
| 340 | vlib_log_main_t *lm = &log_main; |
| 341 | vlib_log_class_data_t *c; |
| 342 | vlib_log_subclass_data_t *sc; |
| 343 | |
| 344 | vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size); |
| 345 | vlib_cli_output (vm, "Defaults:\n"); |
| 346 | vlib_cli_output (vm, "%-20s %U", " Log Level:", |
| 347 | format_vlib_log_level, lm->default_log_level); |
| 348 | vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:", |
| 349 | format_vlib_log_level, lm->default_syslog_log_level); |
| 350 | vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:", |
| 351 | lm->default_rate_limit); |
| 352 | vlib_cli_output (vm, "\n"); |
| 353 | vlib_cli_output (vm, "%-22s %-14s %-14s %s", |
| 354 | "Class/Subclass", "Level", "Syslog Level", "Rate Limit"); |
| 355 | |
| 356 | vec_foreach (c, lm->classes) |
| 357 | { |
| 358 | vlib_cli_output (vm, "%s", c->name); |
| 359 | vec_foreach (sc, c->subclasses) |
| 360 | { |
| 361 | vlib_cli_output (vm, " %-20s %-14U %-14U %d", |
| 362 | sc->name ? (char *) sc->name : "default", |
| 363 | format_vlib_log_level, sc->level, |
| 364 | format_vlib_log_level, sc->syslog_level, |
| 365 | sc->rate_limit); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return error; |
| 370 | } |
| 371 | |
| 372 | /* *INDENT-OFF* */ |
| 373 | VLIB_CLI_COMMAND (cli_show_log_config, static) = { |
| 374 | .path = "show logging configuration", |
| 375 | .short_help = "show logging configuration", |
| 376 | .function = show_log_config, |
| 377 | }; |
| 378 | /* *INDENT-ON* */ |
| 379 | |
| 380 | static clib_error_t * |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 381 | clear_log (vlib_main_t * vm, |
| 382 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 383 | { |
| 384 | clib_error_t *error = 0; |
| 385 | vlib_log_main_t *lm = &log_main; |
| 386 | vlib_log_entry_t *e; |
| 387 | int i = last_log_entry (); |
| 388 | int count = lm->count; |
| 389 | |
| 390 | while (count--) |
| 391 | { |
| 392 | e = vec_elt_at_index (lm->entries, i); |
| 393 | vec_free (e->string); |
| 394 | i = (i + 1) % lm->size; |
| 395 | } |
| 396 | |
| 397 | lm->count = 0; |
| 398 | lm->next = 0; |
| 399 | vlib_log_info (lm->log_class, "log cleared"); |
| 400 | return error; |
| 401 | } |
| 402 | |
| 403 | /* *INDENT-OFF* */ |
| 404 | VLIB_CLI_COMMAND (cli_clear_log, static) = { |
| 405 | .path = "clear logging", |
| 406 | .short_help = "clear logging", |
| 407 | .function = clear_log, |
| 408 | }; |
| 409 | /* *INDENT-ON* */ |
| 410 | |
| 411 | static uword |
| 412 | unformat_vlib_log_level (unformat_input_t * input, va_list * args) |
| 413 | { |
| 414 | vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *); |
| 415 | u8 *level_str = NULL; |
| 416 | uword rv = 1; |
Steven | 526ea3e | 2018-04-25 11:29:00 -0700 | [diff] [blame] | 417 | if (unformat (input, "%s", &level_str)) |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 418 | { |
| 419 | #define _(v, uc, lc) \ |
| 420 | const char __##uc[] = #lc; \ |
Steven | 526ea3e | 2018-04-25 11:29:00 -0700 | [diff] [blame] | 421 | if (!strcmp ((const char *) level_str, __##uc)) \ |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 422 | { \ |
| 423 | *level = VLIB_LOG_LEVEL_##uc; \ |
| 424 | rv = 1; \ |
| 425 | goto done; \ |
| 426 | } |
| 427 | foreach_vlib_log_level; |
| 428 | rv = 0; |
| 429 | #undef _ |
| 430 | } |
| 431 | done: |
| 432 | vec_free (level_str); |
| 433 | return rv; |
| 434 | } |
| 435 | |
| 436 | static uword |
| 437 | unformat_vlib_log_class (unformat_input_t * input, va_list * args) |
| 438 | { |
| 439 | vlib_log_class_data_t **class = va_arg (*args, vlib_log_class_data_t **); |
| 440 | uword rv = 0; |
| 441 | u8 *class_str = NULL; |
| 442 | vlib_log_main_t *lm = &log_main; |
| 443 | if (unformat (input, "%v", &class_str)) |
| 444 | { |
| 445 | vlib_log_class_data_t *cdata; |
| 446 | vec_foreach (cdata, lm->classes) |
| 447 | { |
| 448 | if (vec_is_equal (cdata->name, class_str)) |
| 449 | { |
| 450 | *class = cdata; |
| 451 | rv = 1; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | vec_free (class_str); |
| 457 | return rv; |
| 458 | } |
| 459 | |
| 460 | static clib_error_t * |
| 461 | set_log_class (vlib_main_t * vm, |
| 462 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 463 | { |
| 464 | unformat_input_t _line_input, *line_input = &_line_input; |
| 465 | clib_error_t *rv = NULL; |
| 466 | int rate_limit; |
| 467 | bool set_rate_limit = false; |
| 468 | bool set_level = false; |
| 469 | bool set_syslog_level = false; |
| 470 | vlib_log_level_t level; |
| 471 | vlib_log_level_t syslog_level; |
| 472 | |
| 473 | /* Get a line of input. */ |
| 474 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 475 | return 0; |
| 476 | |
| 477 | vlib_log_class_data_t *class = NULL; |
| 478 | if (!unformat (line_input, "%U", unformat_vlib_log_class, &class)) |
| 479 | { |
| 480 | return clib_error_return (0, "unknown log class `%U'", |
| 481 | format_unformat_error, line_input); |
| 482 | } |
| 483 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 484 | { |
| 485 | if (unformat (line_input, "rate-limit %d", &rate_limit)) |
| 486 | { |
| 487 | set_rate_limit = true; |
| 488 | } |
| 489 | else |
| 490 | if (unformat |
| 491 | (line_input, "level %U", unformat_vlib_log_level, &level)) |
| 492 | { |
| 493 | set_level = true; |
| 494 | } |
| 495 | else |
| 496 | if (unformat |
| 497 | (line_input, "syslog-level %U", unformat_vlib_log_level, |
| 498 | &syslog_level)) |
| 499 | { |
| 500 | set_syslog_level = true; |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | return clib_error_return (0, "unknown input `%U'", |
| 505 | format_unformat_error, line_input); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (set_level) |
| 510 | { |
| 511 | vlib_log_subclass_data_t *subclass; |
| 512 | vec_foreach (subclass, class->subclasses) |
| 513 | { |
| 514 | subclass->level = level; |
| 515 | } |
| 516 | } |
| 517 | if (set_syslog_level) |
| 518 | { |
| 519 | vlib_log_subclass_data_t *subclass; |
| 520 | vec_foreach (subclass, class->subclasses) |
| 521 | { |
| 522 | subclass->syslog_level = syslog_level; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | if (set_rate_limit) |
| 526 | { |
| 527 | vlib_log_subclass_data_t *subclass; |
| 528 | vec_foreach (subclass, class->subclasses) |
| 529 | { |
| 530 | subclass->rate_limit = rate_limit; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return rv; |
| 535 | } |
| 536 | |
| 537 | /* *INDENT-OFF* */ |
| 538 | VLIB_CLI_COMMAND (cli_set_log, static) = { |
| 539 | .path = "set logging class", |
| 540 | .short_help = "set loggging class <class> [rate-limit <int>] " |
| 541 | "[level <level>] [syslog-level <level>]", |
| 542 | .function = set_log_class, |
| 543 | }; |
| 544 | /* *INDENT-ON* */ |
| 545 | |
| 546 | static clib_error_t * |
| 547 | set_log_unth_time (vlib_main_t * vm, |
| 548 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 549 | { |
| 550 | unformat_input_t _line_input, *line_input = &_line_input; |
| 551 | clib_error_t *rv = NULL; |
| 552 | int unthrottle_time; |
| 553 | vlib_log_main_t *lm = &log_main; |
| 554 | |
| 555 | /* Get a line of input. */ |
| 556 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 557 | return 0; |
| 558 | |
| 559 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 560 | { |
| 561 | if (unformat (line_input, "%d", &unthrottle_time)) |
| 562 | lm->unthrottle_time = unthrottle_time; |
| 563 | else |
| 564 | return clib_error_return (0, "unknown input `%U'", |
| 565 | format_unformat_error, line_input); |
| 566 | } |
| 567 | |
| 568 | return rv; |
| 569 | } |
| 570 | |
| 571 | /* *INDENT-OFF* */ |
| 572 | VLIB_CLI_COMMAND (cli_set_log_params, static) = { |
| 573 | .path = "set logging unthrottle-time", |
| 574 | .short_help = "set logging unthrottle-time <int>", |
| 575 | .function = set_log_unth_time, |
| 576 | }; |
| 577 | /* *INDENT-ON* */ |
| 578 | |
| 579 | static clib_error_t * |
| 580 | set_log_size (vlib_main_t * vm, |
| 581 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 582 | { |
| 583 | unformat_input_t _line_input, *line_input = &_line_input; |
| 584 | clib_error_t *rv = NULL; |
| 585 | int size; |
| 586 | vlib_log_main_t *lm = &log_main; |
| 587 | |
| 588 | /* Get a line of input. */ |
| 589 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 590 | return 0; |
| 591 | |
| 592 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 593 | { |
| 594 | if (unformat (line_input, "%d", &size)) |
| 595 | { |
| 596 | lm->size = size; |
| 597 | vec_validate (lm->entries, lm->size); |
| 598 | } |
| 599 | else |
| 600 | return clib_error_return (0, "unknown input `%U'", |
| 601 | format_unformat_error, line_input); |
| 602 | } |
| 603 | |
| 604 | return rv; |
| 605 | } |
| 606 | |
| 607 | /* *INDENT-OFF* */ |
| 608 | VLIB_CLI_COMMAND (cli_set_log_size, static) = { |
| 609 | .path = "set logging size", |
| 610 | .short_help = "set logging size <int>", |
| 611 | .function = set_log_size, |
| 612 | }; |
| 613 | /* *INDENT-ON* */ |
| 614 | |
| 615 | static uword |
| 616 | unformat_vlib_log_subclass (unformat_input_t * input, va_list * args) |
| 617 | { |
| 618 | vlib_log_class_data_t *class = va_arg (*args, vlib_log_class_data_t *); |
| 619 | vlib_log_subclass_data_t **subclass = |
| 620 | va_arg (*args, vlib_log_subclass_data_t **); |
| 621 | uword rv = 0; |
| 622 | u8 *subclass_str = NULL; |
| 623 | if (unformat (input, "%v", &subclass_str)) |
| 624 | { |
| 625 | vlib_log_subclass_data_t *scdata; |
| 626 | vec_foreach (scdata, class->subclasses) |
| 627 | { |
| 628 | if (vec_is_equal (scdata->name, subclass_str)) |
| 629 | { |
| 630 | rv = 1; |
| 631 | *subclass = scdata; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | vec_free (subclass_str); |
| 637 | return rv; |
| 638 | } |
| 639 | |
| 640 | static clib_error_t * |
| 641 | test_log_class_subclass (vlib_main_t * vm, |
| 642 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 643 | { |
| 644 | unformat_input_t _line_input, *line_input = &_line_input; |
| 645 | /* Get a line of input. */ |
| 646 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 647 | return 0; |
| 648 | |
| 649 | vlib_log_class_data_t *class = NULL; |
| 650 | vlib_log_subclass_data_t *subclass = NULL; |
| 651 | vlib_log_level_t level; |
| 652 | if (unformat (line_input, "%U", unformat_vlib_log_level, &level)) |
| 653 | { |
| 654 | if (unformat (line_input, "%U", unformat_vlib_log_class, &class)) |
| 655 | { |
| 656 | if (unformat |
| 657 | (line_input, "%U", unformat_vlib_log_subclass, class, |
| 658 | &subclass)) |
| 659 | { |
| 660 | vlib_log (level, |
| 661 | (class->index << 16) | (subclass->index), "%U", |
| 662 | format_unformat_input, line_input); |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | return clib_error_return (0, |
| 667 | "unknown log subclass near beginning of `%U'", |
| 668 | format_unformat_error, line_input); |
| 669 | } |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | return clib_error_return (0, |
| 674 | "unknown log class near beginning of `%U'", |
| 675 | format_unformat_error, line_input); |
| 676 | } |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | return clib_error_return (0, "unknown log level near beginning of `%U'", |
| 681 | format_unformat_error, line_input); |
| 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | /* *INDENT-OFF* */ |
| 687 | VLIB_CLI_COMMAND (cli_test_log, static) = { |
| 688 | .path = "test log", |
| 689 | .short_help = "test log <class> <subclass> <level> <message", |
| 690 | .function = test_log_class_subclass, |
| 691 | }; |
| 692 | /* *INDENT-ON* */ |
| 693 | |
| 694 | static clib_error_t * |
| 695 | log_config (vlib_main_t * vm, unformat_input_t * input) |
| 696 | { |
| 697 | vlib_log_main_t *lm = &log_main; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 698 | |
| 699 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 700 | { |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 701 | if (unformat (input, "size %d", &lm->size)) |
| 702 | vec_validate (lm->entries, lm->size); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 703 | else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time)) |
Damjan Marion | e78fab8 | 2018-04-18 17:03:28 +0200 | [diff] [blame] | 704 | ; |
| 705 | else if (unformat (input, "default-log-level %U", |
| 706 | unformat_vlib_log_level, &lm->default_log_level)) |
| 707 | ; |
| 708 | else if (unformat (input, "default-syslog-log-level %U", |
| 709 | unformat_vlib_log_level, |
| 710 | &lm->default_syslog_log_level)) |
| 711 | ; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 712 | else |
| 713 | { |
| 714 | return unformat_parse_error (input); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | VLIB_EARLY_CONFIG_FUNCTION (log_config, "logging"); |
| 722 | |
| 723 | /* |
| 724 | * fd.io coding-style-patch-verification: ON |
| 725 | * |
| 726 | * Local Variables: |
| 727 | * eval: (c-set-style "gnu") |
| 728 | * End: |
| 729 | */ |