X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvlib%2Flog.c;h=342c0d25cd936b5e625546414e5f081891d4240d;hb=9372374f0de1d4b2767bd8c9dbaa868a9975dfaa;hp=dd2ecf2c110438d50d8b6ee6145472dfb87abd98;hpb=07a38572caa2c2d2d8658420a7c3df8e7f9d0e74;p=vpp.git diff --git a/src/vlib/log.c b/src/vlib/log.c index dd2ecf2c110..342c0d25cd9 100644 --- a/src/vlib/log.c +++ b/src/vlib/log.c @@ -18,60 +18,15 @@ #include #include -#define VLIB_LOG_DEFAULT_SIZE 50 -#define VLIB_LOG_DEFAULT_RATE_LIMIT 10 -#define VLIB_LOG_DEFAULT_UNTHROTTLE_TIME 3 -#define VLIB_LOG_DEFAULT_LOG_LEVEL VLIB_LOG_LEVEL_CRIT - -typedef struct -{ - vlib_log_level_t level; - vlib_log_class_t class; - f64 timestamp; - u8 *string; -} vlib_log_entry_t; - -typedef struct -{ - u32 index; - u8 *name; - // level of log messages kept for this subclass - vlib_log_level_t level; - // level of log messages sent to syslog for this subclass - vlib_log_level_t syslog_level; - // flag saying whether this subclass is logged to syslog - bool syslog_enabled; - f64 last_event_timestamp; - int last_sec_count; - int is_throttling; - int rate_limit; -} vlib_log_subclass_data_t; - -typedef struct -{ - u32 index; - u8 *name; - vlib_log_subclass_data_t *subclasses; -} vlib_log_class_data_t; - -typedef struct -{ - vlib_log_entry_t *entries; - vlib_log_class_data_t *classes; - int size, next, count; - - /* our own log class */ - vlib_log_class_t log_class; - - int default_rate_limit; - int unthrottle_time; - u32 indent; -} vlib_log_main_t; - -vlib_log_main_t log_main; - +vlib_log_main_t log_main = { + .default_log_level = VLIB_LOG_LEVEL_NOTICE, + .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING, + .unthrottle_time = 3, + .size = 512, + .default_rate_limit = 50, +}; -static int +int last_log_entry () { vlib_log_main_t *lm = &log_main; @@ -103,11 +58,13 @@ vlib_log_level_to_syslog_priority (vlib_log_level_t level) { switch (level) { +#define LOG_DISABLED LOG_DEBUG #define _(n,uc,lc) \ case VLIB_LOG_LEVEL_##uc:\ return LOG_##uc; foreach_vlib_log_level #undef _ +#undef LOG_DISABLED } return LOG_DEBUG; } @@ -139,6 +96,7 @@ vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...) u8 *s = 0; bool use_formatted_log_entry = true; + vec_validate (lm->entries, lm->size); /* make sure we are running on the main thread to avoid use in dataplane code, for dataplane logging consider use of event-logger */ ASSERT (vlib_get_thread_index () == 0); @@ -188,14 +146,16 @@ vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...) lm->count++; syslog: - if (sc->syslog_enabled && level <= sc->syslog_level) + if (sc->syslog_level != VLIB_LOG_LEVEL_DISABLED && + level <= sc->syslog_level) { u8 *tmp = format (NULL, "%U", format_vlib_log_class, class); if (use_formatted_log_entry) { syslog (vlib_log_level_to_syslog_priority (level), "%.*s: %.*s", - vec_len (tmp), tmp, - vec_len (s) - (vec_c_string_is_terminated (s) ? 1 : 0), s); + (int) vec_len (tmp), tmp, + (int) (vec_len (s) - + (vec_c_string_is_terminated (s) ? 1 : 0)), s); } else { @@ -204,16 +164,16 @@ syslog: tmp = va_format (tmp, fmt, &va); va_end (va); syslog (vlib_log_level_to_syslog_priority (level), "%.*s", - vec_len (tmp) - (vec_c_string_is_terminated (tmp) ? 1 : 0), - tmp); + (int) (vec_len (tmp) - + (vec_c_string_is_terminated (tmp) ? 1 : 0)), tmp); } vec_free (tmp); } } -vlib_log_class_t -vlib_log_register_class (char *class, char *subclass) +static vlib_log_class_t +vlib_log_register_class_internal (char *class, char *subclass, u32 limit) { vlib_log_main_t *lm = &log_main; vlib_log_class_data_t *c = NULL; @@ -221,6 +181,8 @@ vlib_log_register_class (char *class, char *subclass) vlib_log_class_data_t *tmp; vec_foreach (tmp, lm->classes) { + if (vec_len (tmp->name) != strlen (class)) + continue; if (!memcmp (class, tmp->name, vec_len (tmp->name))) { c = tmp; @@ -237,11 +199,26 @@ vlib_log_register_class (char *class, char *subclass) vec_add2 (c->subclasses, s, 1); s->index = s - c->subclasses; s->name = subclass ? format (0, "%s", subclass) : 0; - s->rate_limit = lm->default_rate_limit; - s->level = VLIB_LOG_DEFAULT_LOG_LEVEL; + s->rate_limit = (limit == 0) ? lm->default_rate_limit : limit; + s->level = lm->default_log_level; + s->syslog_level = lm->default_syslog_log_level; return (c->index << 16) | (s->index); } +vlib_log_class_t +vlib_log_register_class (char *class, char *subclass) +{ + return vlib_log_register_class_internal (class, subclass, + 0 /* default rate limit */ ); +} + +vlib_log_class_t +vlib_log_register_class_rate_limit (char *class, char *subclass, u32 limit) +{ + return vlib_log_register_class_internal (class, subclass, limit); +} + + u8 * format_vlib_log_level (u8 * s, va_list * args) { @@ -269,7 +246,10 @@ static clib_error_t * vlib_log_init (vlib_main_t * vm) { vlib_log_main_t *lm = &log_main; - lm->size = VLIB_LOG_DEFAULT_SIZE; + + gettimeofday (&lm->time_zero_timeval, 0); + lm->time_zero = vlib_time_now (vm); + vec_validate (lm->entries, lm->size); lm->log_class = vlib_log_register_class ("log", 0); u8 *tmp = format (NULL, "%U %-10U %-10U ", format_time_float, 0, (f64) 0, @@ -291,14 +271,18 @@ show_log (vlib_main_t * vm, vlib_log_entry_t *e; int i = last_log_entry (); int count = lm->count; + f64 time_offset; + + time_offset = (f64) lm->time_zero_timeval.tv_sec + + (((f64) lm->time_zero_timeval.tv_usec) * 1e-6) - lm->time_zero; while (count--) { e = vec_elt_at_index (lm->entries, i); - vlib_cli_output (vm, "%U %-10U %-10U %v (%d)", - format_time_float, 0, e->timestamp, + vlib_cli_output (vm, "%U %-10U %-14U %v", + format_time_float, 0, e->timestamp + time_offset, format_vlib_log_level, e->level, - format_vlib_log_class, e->class, e->string, i); + format_vlib_log_class, e->class, e->string); i = (i + 1) % lm->size; } @@ -313,6 +297,54 @@ VLIB_CLI_COMMAND (cli_show_log, static) = { }; /* *INDENT-ON* */ +static clib_error_t * +show_log_config (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + clib_error_t *error = 0; + vlib_log_main_t *lm = &log_main; + vlib_log_class_data_t *c; + vlib_log_subclass_data_t *sc; + + vlib_cli_output (vm, "%-20s %u entries", "Buffer Size:", lm->size); + vlib_cli_output (vm, "Defaults:\n"); + vlib_cli_output (vm, "%-20s %U", " Log Level:", + format_vlib_log_level, lm->default_log_level); + vlib_cli_output (vm, "%-20s %U", " Syslog Log Level:", + format_vlib_log_level, lm->default_syslog_log_level); + vlib_cli_output (vm, "%-20s %u msgs/sec", " Rate Limit:", + lm->default_rate_limit); + vlib_cli_output (vm, "\n"); + vlib_cli_output (vm, "%-22s %-14s %-14s %s", + "Class/Subclass", "Level", "Syslog Level", "Rate Limit"); + + + u8 *defstr = format (0, "default"); + vec_foreach (c, lm->classes) + { + vlib_cli_output (vm, "%v", c->name); + vec_foreach (sc, c->subclasses) + { + vlib_cli_output (vm, " %-20v %-14U %-14U %d", + sc->name ? sc->name : defstr, + format_vlib_log_level, sc->level, + format_vlib_log_level, sc->syslog_level, + sc->rate_limit); + } + } + vec_free (defstr); + + return error; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (cli_show_log_config, static) = { + .path = "show logging configuration", + .short_help = "show logging configuration", + .function = show_log_config, +}; +/* *INDENT-ON* */ + static clib_error_t * clear_log (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) @@ -350,11 +382,11 @@ unformat_vlib_log_level (unformat_input_t * input, va_list * args) vlib_log_level_t *level = va_arg (*args, vlib_log_level_t *); u8 *level_str = NULL; uword rv = 1; - if (unformat (input, "%v", &level_str)) + if (unformat (input, "%s", &level_str)) { #define _(v, uc, lc) \ const char __##uc[] = #lc; \ - if (!memcmp (level_str, __##uc, sizeof (__##uc))) \ + if (!strcmp ((const char *) level_str, __##uc)) \ { \ *level = VLIB_LOG_LEVEL_##uc; \ rv = 1; \ @@ -456,15 +488,6 @@ set_log_class (vlib_main_t * vm, vec_foreach (subclass, class->subclasses) { subclass->syslog_level = syslog_level; - subclass->syslog_enabled = true; - } - } - else - { - vlib_log_subclass_data_t *subclass; - vec_foreach (subclass, class->subclasses) - { - subclass->syslog_enabled = false; } } if (set_rate_limit) @@ -482,7 +505,7 @@ set_log_class (vlib_main_t * vm, /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_set_log, static) = { .path = "set logging class", - .short_help = "set loggging class [rate-limit ] " + .short_help = "set logging class [rate-limit ] " "[level ] [syslog-level ]", .function = set_log_class, }; @@ -631,7 +654,7 @@ test_log_class_subclass (vlib_main_t * vm, /* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_test_log, static) = { .path = "test log", - .short_help = "test log size = size; - vec_validate (lm->entries, lm->size); - } + if (unformat (input, "size %d", &lm->size)) + vec_validate (lm->entries, lm->size); else if (unformat (input, "unthrottle-time %d", &lm->unthrottle_time)) - { - // nothing to do here - } + ; + else if (unformat (input, "default-log-level %U", + unformat_vlib_log_level, &lm->default_log_level)) + ; + else if (unformat (input, "default-syslog-log-level %U", + unformat_vlib_log_level, + &lm->default_syslog_log_level)) + ; else { return unformat_parse_error (input);