papi: Use CMSG_SPACE for sizing ancillary buffer space
[vpp.git] / src / vlib / log.c
index 27d2e16..60fb9fb 100644 (file)
 #include <vlib/log.h>
 #include <vlib/unix/unix.h>
 #include <syslog.h>
+#include <vppinfra/elog.h>
 
 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,
+  .add_to_elog = 0,
   .default_rate_limit = 50,
 };
 
+VLIB_REGISTER_LOG_CLASS (log_log, static) = {
+  .class_name = "log",
+};
+
 static const int colors[] = {
   [VLIB_LOG_LEVEL_EMERG] = 1,  /* red */
   [VLIB_LOG_LEVEL_ALERT] = 1,  /* red */
@@ -38,6 +44,18 @@ static const int colors[] = {
   [VLIB_LOG_LEVEL_DEBUG] = 6,  /* cyan */
 };
 
+static const int log_level_to_syslog_priority[] = {
+  [VLIB_LOG_LEVEL_EMERG] = LOG_EMERG,
+  [VLIB_LOG_LEVEL_ALERT] = LOG_ALERT,
+  [VLIB_LOG_LEVEL_CRIT] = LOG_CRIT,
+  [VLIB_LOG_LEVEL_ERR] = LOG_ERR,
+  [VLIB_LOG_LEVEL_WARNING] = LOG_WARNING,
+  [VLIB_LOG_LEVEL_NOTICE] = LOG_NOTICE,
+  [VLIB_LOG_LEVEL_INFO] = LOG_INFO,
+  [VLIB_LOG_LEVEL_DEBUG] = LOG_DEBUG,
+  [VLIB_LOG_LEVEL_DISABLED] = LOG_DEBUG,
+};
+
 int
 last_log_entry ()
 {
@@ -50,43 +68,12 @@ last_log_entry ()
     i += lm->size;
   return i;
 }
-
-static vlib_log_class_data_t *
-get_class_data (vlib_log_class_t ci)
-{
-  vlib_log_main_t *lm = &log_main;
-  return vec_elt_at_index (lm->classes, (ci >> 16));
-}
-
-static vlib_log_subclass_data_t *
-get_subclass_data (vlib_log_class_t ci)
-{
-  vlib_log_class_data_t *c = get_class_data (ci);
-  return vec_elt_at_index (c->subclasses, (ci & 0xffff));
-}
-
-static int
-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;
-}
-
 u8 *
 format_vlib_log_class (u8 * s, va_list * args)
 {
   vlib_log_class_t ci = va_arg (*args, vlib_log_class_t);
-  vlib_log_class_data_t *c = get_class_data (ci);
-  vlib_log_subclass_data_t *sc = get_subclass_data (ci);
+  vlib_log_class_data_t *c = vlib_log_get_class_data (ci);
+  vlib_log_subclass_data_t *sc = vlib_log_get_subclass_data (ci);
 
   if (sc->name)
     return format (s, "%v/%v", c->name, sc->name);
@@ -101,7 +88,6 @@ format_indent (u8 * s, va_list * args)
   u32 indent = va_arg (*args, u32);
   u8 *c;
 
-  /* *INDENT-OFF* */
   vec_foreach (c, v)
     {
       vec_add (s, c, 1);
@@ -109,7 +95,6 @@ format_indent (u8 * s, va_list * args)
        for (u32 i = 0; i < indent; i++)
          vec_add1 (s, (u8) ' ');
     }
-  /* *INDENT-ON* */
   return s;
 }
 
@@ -129,7 +114,7 @@ vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
   vlib_main_t *vm = vlib_get_main ();
   vlib_log_main_t *lm = &log_main;
   vlib_log_entry_t *e;
-  vlib_log_subclass_data_t *sc = get_subclass_data (class);
+  vlib_log_subclass_data_t *sc = vlib_log_get_subclass_data (class);
   va_list va;
   f64 t = vlib_time_now (vm);
   f64 delta = t - sc->last_event_timestamp;
@@ -187,6 +172,7 @@ vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
              indent = vec_len (l);
            }
          fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length);
+         vec_terminate_c_string (fmt);
          l = format (l, (char *) fmt, format_vlib_log_class, class,
                      format_vlib_log_level, level);
          vec_free (fmt);
@@ -200,7 +186,7 @@ vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
       else
        {
          l = format (l, "%U", format_vlib_log_class, class);
-         int prio = vlib_log_level_to_syslog_priority (level);
+         int prio = log_level_to_syslog_priority[level];
          int is_term = vec_c_string_is_terminated (l) ? 1 : 0;
 
          syslog (prio, "%.*s: %.*s", (int) vec_len (l), l,
@@ -219,6 +205,37 @@ vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...)
       e->timestamp = t;
       s = 0;
 
+      if (lm->add_to_elog)
+       {
+         ELOG_TYPE_DECLARE(ee) =
+            {
+             .format = "log-%s: %s",
+             .format_args = "t4T4",
+             .n_enum_strings = VLIB_LOG_N_LEVELS,
+             .enum_strings = {
+                "unknown",
+                "emerg",
+                "alert",
+                "crit",
+                "err",
+                "warn",
+                "notice",
+                "info",
+                "debug",
+                "disabled",
+                },
+            };
+         struct
+         {
+           u32 log_level;
+           u32 string_index;
+         } * ed;
+         ed = ELOG_DATA (&vlib_global_main.elog_main, ee);
+         ed->log_level = level;
+         ed->string_index =
+           elog_string (&vlib_global_main.elog_main, "%v%c", e->string, 0);
+       }
+
       lm->next = (lm->next + 1) % lm->size;
       if (lm->size > lm->count)
        lm->count++;
@@ -262,8 +279,8 @@ vlib_log_register_class_internal (char *class, char *subclass, u32 limit)
       vec_add2 (lm->classes, c, 1);
       c->index = c - lm->classes;
       c->name = format (0, "%s", class);
-      length = vec_len (c->name);
     }
+  length = vec_len (c->name);
 
   vec_add2 (c->subclasses, s, 1);
   s->index = s - c->subclasses;
@@ -321,7 +338,7 @@ format_vlib_log_level (u8 * s, va_list * args)
 
   switch (i)
     {
-#define _(v,uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
+#define _(uc,lc) case VLIB_LOG_LEVEL_##uc: t = #lc; break;
       foreach_vlib_log_level
 #undef _
     default:
@@ -330,21 +347,36 @@ format_vlib_log_level (u8 * s, va_list * args)
   return format (s, "%s", t);
 }
 
-static clib_error_t *
-vlib_log_init (vlib_main_t * vm)
+clib_error_t *
+vlib_log_init (vlib_main_t *vm)
 {
   vlib_log_main_t *lm = &log_main;
+  vlib_log_class_registration_t *r = lm->registrations;
 
   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);
-  return 0;
-}
 
-VLIB_INIT_FUNCTION (vlib_log_init);
+  while (r)
+    {
+      r->class = vlib_log_register_class (r->class_name, r->subclass_name);
+      if (r->default_level)
+       vlib_log_get_subclass_data (r->class)->level = r->default_level;
+      if (r->default_syslog_level)
+       vlib_log_get_subclass_data (r->class)->syslog_level =
+         r->default_syslog_level;
+      r = r->next;
+    }
 
+  r = lm->registrations;
+  while (r)
+    {
+      vlib_log_debug (r->class, "initialized");
+      r = r->next;
+    }
+  return 0;
+}
 
 static clib_error_t *
 show_log (vlib_main_t * vm,
@@ -363,23 +395,20 @@ show_log (vlib_main_t * vm,
   while (count--)
     {
       e = vec_elt_at_index (lm->entries, i);
-      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);
+      vlib_cli_output (vm, "%U %-10U %-14U %v", format_time_float, NULL,
+                      e->timestamp + time_offset, format_vlib_log_level,
+                      e->level, format_vlib_log_class, e->class, e->string);
       i = (i + 1) % lm->size;
     }
 
   return error;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cli_show_log, static) = {
   .path = "show logging",
   .short_help = "show logging",
   .function = show_log,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 show_log_config (vlib_main_t * vm,
@@ -421,13 +450,11 @@ show_log_config (vlib_main_t * vm,
   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,
@@ -448,17 +475,15 @@ clear_log (vlib_main_t * vm,
 
   lm->count = 0;
   lm->next = 0;
-  vlib_log_info (lm->log_class, "log cleared");
+  vlib_log_info (log_log.class, "log cleared");
   return error;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cli_clear_log, static) = {
   .path = "clear logging",
   .short_help = "clear logging",
   .function = clear_log,
 };
-/* *INDENT-ON* */
 
 static uword
 unformat_vlib_log_level (unformat_input_t * input, va_list * args)
@@ -468,11 +493,11 @@ unformat_vlib_log_level (unformat_input_t * input, va_list * args)
   uword rv = 1;
   if (unformat (input, "%s", &level_str))
     {
-#define _(v, uc, lc)                                   \
+#define _(uc, lc)                                      \
   const char __##uc[] = #lc;                           \
-  if (!strcmp ((const char *) level_str, __##uc))      \
+  if (!strcmp ((const char *) level_str, __##uc))      \
     {                                                  \
-      *level = VLIB_LOG_LEVEL_##uc;                 \
+      *level = VLIB_LOG_LEVEL_##uc;                    \
       rv = 1;                                          \
       goto done;                                       \
     }
@@ -586,14 +611,12 @@ set_log_class (vlib_main_t * vm,
   return rv;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cli_set_log, static) = {
   .path = "set logging class",
   .short_help = "set logging class <class> [rate-limit <int>] "
     "[level <level>] [syslog-level <level>]",
   .function = set_log_class,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 set_log_unth_time (vlib_main_t * vm,
@@ -620,13 +643,11 @@ set_log_unth_time (vlib_main_t * vm,
   return rv;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cli_set_log_params, static) = {
   .path = "set logging unthrottle-time",
   .short_help = "set logging unthrottle-time <int>",
   .function = set_log_unth_time,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 set_log_size (vlib_main_t * vm,
@@ -656,13 +677,11 @@ set_log_size (vlib_main_t * vm,
   return rv;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cli_set_log_size, static) = {
   .path = "set logging size",
   .short_help = "set logging size <int>",
   .function = set_log_size,
 };
-/* *INDENT-ON* */
 
 static uword
 unformat_vlib_log_subclass (unformat_input_t * input, va_list * args)
@@ -735,13 +754,11 @@ test_log_class_subclass (vlib_main_t * vm,
   return 0;
 }
 
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (cli_test_log, static) = {
   .path = "test log",
   .short_help = "test log <level> <class> <subclass> <message>",
   .function = test_log_class_subclass,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 log_config_class (vlib_main_t * vm, char *name, unformat_input_t * input)
@@ -802,6 +819,8 @@ log_config (vlib_main_t * vm, unformat_input_t * input)
                         unformat_vlib_log_level,
                         &lm->default_syslog_log_level))
        ;
+      else if (unformat (input, "add-to-elog"))
+       lm->add_to_elog = 1;
       else if (unformat (input, "class %s %U", &class,
                         unformat_vlib_cli_sub_input, &sub_input))
        {