vlib: add VLIB_REGISTER_LOG_CLASS macro 17/29617/2
authorDamjan Marion <damarion@cisco.com>
Fri, 23 Oct 2020 19:52:50 +0000 (21:52 +0200)
committerOle Tr�an <otroan@employees.org>
Mon, 26 Oct 2020 10:45:49 +0000 (10:45 +0000)
Type: improvement
Change-Id: Iafcaeb88fa3569e042fb9cb270cf062b145416e2
Signed-off-by: Damjan Marion <damarion@cisco.com>
src/plugins/avf/avf.h
src/plugins/avf/device.c
src/vlib/log.c
src/vlib/log.h

index 135b5f9..57e7126 100644 (file)
 #define AVF_TXD_CMD_RS                 AVF_TXD_CMD(1)
 #define AVF_TXD_CMD_RSV                        AVF_TXD_CMD(2)
 
+extern vlib_log_class_registration_t avf_log;
+
 #define avf_log_err(dev, f, ...)                        \
-  vlib_log (VLIB_LOG_LEVEL_ERR, avf_main.log_class, "%U: " f, \
+  vlib_log (VLIB_LOG_LEVEL_ERR, avf_log.class, "%U: " f, \
             format_vlib_pci_addr, &dev->pci_addr, \
             ## __VA_ARGS__)
 
 #define avf_log_warn(dev, f, ...)                        \
-  vlib_log (VLIB_LOG_LEVEL_WARNING, avf_main.log_class, "%U: " f, \
+  vlib_log (VLIB_LOG_LEVEL_WARNING, avf_log.class, "%U: " f, \
             format_vlib_pci_addr, &dev->pci_addr, \
             ## __VA_ARGS__)
 
 #define avf_log_debug(dev, f, ...)                        \
-  vlib_log (VLIB_LOG_LEVEL_DEBUG, avf_main.log_class, "%U: " f, \
+  vlib_log (VLIB_LOG_LEVEL_DEBUG, avf_log.class, "%U: " f, \
             format_vlib_pci_addr, &dev->pci_addr, \
             ## __VA_ARGS__)
 
@@ -245,8 +247,6 @@ typedef struct
 
   avf_device_t **devices;
   avf_per_thread_data_t *per_thread_data;
-
-  vlib_log_class_t log_class;
 } avf_main_t;
 
 extern avf_main_t avf_main;
index f391563..b6cf32f 100644 (file)
 #define PCI_DEVICE_ID_INTEL_X710_VF            0x154c
 #define PCI_DEVICE_ID_INTEL_X722_VF            0x37cd
 
+/* *INDENT-OFF* */
+VLIB_REGISTER_LOG_CLASS (avf_log) = {
+  .class_name = "avf",
+};
+/* *INDENT-ON* */
+
 avf_main_t avf_main;
 void avf_delete_if (vlib_main_t * vm, avf_device_t * ad, int with_barrier);
 
@@ -1017,7 +1023,6 @@ avf_device_init (vlib_main_t * vm, avf_main_t * am, avf_device_t * ad,
 void
 avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq)
 {
-  avf_main_t *am = &avf_main;
   vnet_main_t *vnm = vnet_get_main ();
   virtchnl_pf_event_t *e;
   u32 r;
@@ -1157,7 +1162,7 @@ avf_process_one_device (vlib_main_t * vm, avf_device_t * ad, int is_irq)
 error:
   ad->flags |= AVF_DEVICE_F_ERROR;
   ASSERT (ad->error != 0);
-  vlib_log_err (am->log_class, "%U", format_clib_error, ad->error);
+  vlib_log_err (avf_log.class, "%U", format_clib_error, ad->error);
 }
 
 static clib_error_t *
@@ -1780,9 +1785,6 @@ avf_init (vlib_main_t * vm)
   vec_validate_aligned (am->per_thread_data, tm->n_vlib_mains - 1,
                        CLIB_CACHE_LINE_BYTES);
 
-  am->log_class = vlib_log_register_class ("avf", 0);
-  vlib_log_debug (am->log_class, "initialized");
-
   return 0;
 }
 
index 1bf19b0..e1ab036 100644 (file)
@@ -27,6 +27,12 @@ vlib_log_main_t log_main = {
   .default_rate_limit = 50,
 };
 
+/* *INDENT-OFF* */
+VLIB_REGISTER_LOG_CLASS (log_log, static) = {
+  .class_name = "log",
+};
+/* *INDENT-ON* */
+
 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 ()
 {
@@ -65,22 +83,6 @@ get_subclass_data (vlib_log_class_t 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)
 {
@@ -201,7 +203,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,
@@ -263,8 +265,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;
@@ -322,7 +324,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:
@@ -335,12 +337,29 @@ static 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);
+
+  while (r)
+    {
+      r->class = vlib_log_register_class (r->class_name, r->subclass_name);
+      if (r->default_level)
+       get_subclass_data (r->class)->level = r->default_level;
+      if (r->default_syslog_level)
+       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;
 }
 
@@ -449,7 +468,7 @@ 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;
 }
 
@@ -469,11 +488,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;                                       \
     }
index 4cceb4b..75bdceb 100644 (file)
 
 #include <vppinfra/types.h>
 
-#define foreach_vlib_log_level \
-  _(0, EMERG, emerg) \
-  _(1, ALERT, alert) \
-  _(2, CRIT, crit) \
-  _(3, ERR, error) \
-  _(4, WARNING, warn) \
-  _(5, NOTICE, notice) \
-  _(6, INFO, info) \
-  _(7, DEBUG, debug) \
-  _(8, DISABLED, disabled)
+#define foreach_vlib_log_level \
+  _(EMERG, emerg)              \
+  _(ALERT, alert)              \
+  _(CRIT, crit)                        \
+  _(ERR, error)                        \
+  _(WARNING, warn)             \
+  _(NOTICE, notice)            \
+  _(INFO, info)                        \
+  _(DEBUG, debug)              \
+  _(DISABLED, disabled)
 
 typedef enum
 {
-#define _(n,uc,lc) VLIB_LOG_LEVEL_##uc = n,
+  VLIB_LOG_LEVEL_UNKNOWN = 0,
+#define _(uc,lc) VLIB_LOG_LEVEL_##uc,
   foreach_vlib_log_level
 #undef _
+    VLIB_LOG_N_LEVELS,
 } vlib_log_level_t;
 
 typedef struct
@@ -74,15 +76,25 @@ typedef struct
   char *name;
 } vlib_log_class_config_t;
 
+
+typedef struct vlib_log_registration
+{
+  char *class_name;
+  char *subclass_name;
+  vlib_log_class_t class;
+  vlib_log_level_t default_level;
+  vlib_log_level_t default_syslog_level;
+
+  /* next */
+  struct vlib_log_registration *next;
+} vlib_log_class_registration_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 default_log_level;
   int default_syslog_log_level;
@@ -96,6 +108,9 @@ typedef struct
   /* config */
   vlib_log_class_config_t *configs;
   uword *config_index_by_name;
+
+  /* registrations */
+  vlib_log_class_registration_t *registrations;
 } vlib_log_main_t;
 
 extern vlib_log_main_t log_main;
@@ -119,6 +134,18 @@ u8 *format_vlib_log_level (u8 * s, va_list * args);
 #define vlib_log_info(...) vlib_log(VLIB_LOG_LEVEL_INFO, __VA_ARGS__)
 #define vlib_log_debug(...) vlib_log(VLIB_LOG_LEVEL_DEBUG, __VA_ARGS__)
 
+#define VLIB_REGISTER_LOG_CLASS(x,...) \
+__VA_ARGS__ vlib_log_class_registration_t x; \
+static void __clib_constructor                 \
+__vlib_add_log_registration_##x (void)         \
+  {                                            \
+    vlib_log_main_t * lm = &log_main;          \
+    x.next = lm->registrations;                        \
+    x.class = ~0;                              \
+    lm->registrations = &x;                    \
+  }                                            \
+__VA_ARGS__  vlib_log_class_registration_t x
+
 #endif /* included_vlib_log_h */
 
 /*