api: replace print functions wth format

Type: improvement
Change-Id: I7f7050c19453a69a7fb6c5e62f8f57db847d9144
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlibmemory/memclnt_api.c b/src/vlibmemory/memclnt_api.c
index 9e2971a..0717d81 100644
--- a/src/vlibmemory/memclnt_api.c
+++ b/src/vlibmemory/memclnt_api.c
@@ -48,7 +48,6 @@
 #undef vl_typedefs
 
 /* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
 #define vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_printfun
@@ -175,10 +174,8 @@
       c->id = VL_API_##N;                                                     \
       c->name = #n;                                                           \
       c->handler = vl_api_##n##_t_handler;                                    \
-      c->cleanup = vl_noop_handler;                                           \
       c->endian = vl_api_##n##_t_endian;                                      \
-      c->print = vl_api_##n##_t_print;                                        \
-      c->print_json = vl_api_##n##_t_print_json;                              \
+      c->format_fn = vl_api_##n##_t_format;                                   \
       c->tojson = vl_api_##n##_t_tojson;                                      \
       c->fromjson = vl_api_##n##_t_fromjson;                                  \
       c->calc_size = vl_api_##n##_t_calc_size;                                \
@@ -711,18 +708,16 @@
   api_main_t *am = vlibapi_get_main ();
 #define _(N, n)                                                               \
   vl_msg_api_set_handlers (                                                   \
-    VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler, vl_noop_handler, \
-    vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0 /* do not trace */,      \
-    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    VL_API_##N, #n, vl_api_##n##_t_handler, 0, vl_api_##n##_t_format,         \
+    sizeof (vl_api_##n##_t), 0 /* do not trace */, vl_api_##n##_t_tojson,     \
     vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
   foreach_rpc_api_msg;
 #undef _
 
 #define _(N, n)                                                               \
   vl_msg_api_set_handlers (                                                   \
-    VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler, vl_noop_handler, \
-    vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1 /* do trace */,          \
-    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    VL_API_##N, #n, vl_api_##n##_t_handler, 0, vl_api_##n##_t_format,         \
+    sizeof (vl_api_##n##_t), 1 /* do trace */, vl_api_##n##_t_tojson,         \
     vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
   foreach_plugin_trace_msg;
 #undef _
diff --git a/src/vlibmemory/memory_api.c b/src/vlibmemory/memory_api.c
index 80d8628..01fe49b 100644
--- a/src/vlibmemory/memory_api.c
+++ b/src/vlibmemory/memory_api.c
@@ -28,7 +28,6 @@
 #undef vl_typedefs
 
 /* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
 #define vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_printfun
@@ -504,18 +503,21 @@
   if ((rv = vl_map_shmem (region_name, 1 /* is_vlib */ )) < 0)
     return rv;
 
-#define _(N,n,t) do {                                            \
-    c->id = VL_API_##N;                                         \
-    c->name = #n;                                               \
-    c->handler = vl_api_##n##_t_handler;                        \
-    c->cleanup = vl_noop_handler;                               \
-    c->endian = vl_api_##n##_t_endian;                          \
-    c->print = vl_api_##n##_t_print;                            \
-    c->size = sizeof(vl_api_##n##_t);                           \
-    c->traced = t; /* trace, so these msgs print */             \
-    c->replay = 0; /* don't replay client create/delete msgs */ \
-    c->message_bounce = 0; /* don't bounce this message */	\
-    vl_msg_api_config(c);} while (0);
+#define _(N, n, t)                                                            \
+  do                                                                          \
+    {                                                                         \
+      c->id = VL_API_##N;                                                     \
+      c->name = #n;                                                           \
+      c->handler = vl_api_##n##_t_handler;                                    \
+      c->endian = vl_api_##n##_t_endian;                                      \
+      c->format_fn = vl_api_##n##_t_format;                                   \
+      c->size = sizeof (vl_api_##n##_t);                                      \
+      c->traced = t;	     /* trace, so these msgs print */                 \
+      c->replay = 0;	     /* don't replay client create/delete msgs */     \
+      c->message_bounce = 0; /* don't bounce this message */                  \
+      vl_msg_api_config (c);                                                  \
+    }                                                                         \
+  while (0);
 
   foreach_vlib_api_msg;
 #undef _
@@ -770,7 +772,6 @@
   u16 id = clib_net_to_host_u16 (*((u16 *) the_msg));
   vl_api_msg_data_t *m = vl_api_get_msg_data (am, id);
   u8 *(*handler) (void *, void *, void *);
-  u8 *(*print_fp) (void *, void *);
   svm_region_t *old_vlib_rp;
   void *save_shmem_hdr;
   int is_mp_safe = 1;
@@ -802,15 +803,7 @@
       if (PREDICT_FALSE (am->msg_print_flag))
 	{
 	  fformat (stdout, "[%d]: %s\n", id, m->name);
-	  print_fp = (void *) am->msg_data[id].print_handler;
-	  if (print_fp == 0)
-	    {
-	      fformat (stdout, "  [no registered print fn for msg %d]\n", id);
-	    }
-	  else
-	    {
-	      (*print_fp) (the_msg, vm);
-	    }
+	  fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
 	}
       is_mp_safe = am->msg_data[id].is_mp_safe;
 
diff --git a/src/vlibmemory/memory_client.c b/src/vlibmemory/memory_client.c
index 330390e..738c4e5 100644
--- a/src/vlibmemory/memory_client.c
+++ b/src/vlibmemory/memory_client.c
@@ -44,7 +44,6 @@
 #undef vl_calcsizefun
 
 /* instantiate all the print functions we know about */
-#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
 #define vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_printfun
@@ -155,11 +154,6 @@
     }
 }
 
-static void
-noop_handler (void *notused)
-{
-}
-
 void vl_msg_api_send_shmem (svm_queue_t * q, u8 * elem);
 int
 vl_client_connect (const char *name, int ctx_quota, int input_queue_size)
@@ -373,9 +367,8 @@
   api_main_t *am = vlibapi_get_main ();
 #define _(N, n)                                                               \
   vl_msg_api_set_handlers (                                                   \
-    VL_API_##N, #n, vl_api_##n##_t_handler, noop_handler,                     \
-    vl_api_##n##_t_endian, vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0,  \
-    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    VL_API_##N, #n, vl_api_##n##_t_handler, vl_api_##n##_t_endian,            \
+    vl_api_##n##_t_format, sizeof (vl_api_##n##_t), 0, vl_api_##n##_t_tojson, \
     vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);                       \
   am->msg_data[VL_API_##N].replay_allowed = 0;
   foreach_api_msg;
diff --git a/src/vlibmemory/socket_api.c b/src/vlibmemory/socket_api.c
index e451d62..106fcb2 100644
--- a/src/vlibmemory/socket_api.c
+++ b/src/vlibmemory/socket_api.c
@@ -35,7 +35,6 @@
 #undef vl_typedefs
 
 /* instantiate all the print functions we know about */
-#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
 #define vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_printfun
@@ -798,9 +797,8 @@
 
 #define _(N, n, t)                                                            \
   vl_msg_api_set_handlers (                                                   \
-    VL_API_##N, #n, vl_api_##n##_t_handler, vl_noop_handler,                  \
-    vl_api_##n##_t_endian, vl_api_##n##_t_print, sizeof (vl_api_##n##_t), t,  \
-    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    VL_API_##N, #n, vl_api_##n##_t_handler, vl_api_##n##_t_endian,            \
+    vl_api_##n##_t_format, sizeof (vl_api_##n##_t), t, vl_api_##n##_t_tojson, \
     vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);                       \
   am->msg_data[VL_API_##N].replay_allowed = 0;
   foreach_vlib_api_msg;
diff --git a/src/vlibmemory/socket_client.c b/src/vlibmemory/socket_client.c
index 237d222..1eccb73 100644
--- a/src/vlibmemory/socket_client.c
+++ b/src/vlibmemory/socket_client.c
@@ -41,7 +41,6 @@
 #undef vl_calcsizefun
 
 /* instantiate all the print functions we know about */
-#define vl_print(handle, ...) clib_warning (__VA_ARGS__)
 #define vl_printfun
 #include <vlibmemory/vl_memory_api_h.h>
 #undef vl_printfun
@@ -427,20 +426,14 @@
 _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply)			\
 _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply)     		\
 
-static void
-noop_handler (void *notused)
-{
-}
-
 void
 vl_sock_client_install_message_handlers (void)
 {
 
 #define _(N, n)                                                               \
   vl_msg_api_set_handlers (                                                   \
-    VL_API_##N, #n, vl_api_##n##_t_handler, noop_handler,                     \
-    vl_api_##n##_t_endian, vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 0,  \
-    vl_api_##n##_t_print_json, vl_api_##n##_t_tojson,                         \
+    VL_API_##N, #n, vl_api_##n##_t_handler, vl_api_##n##_t_endian,            \
+    vl_api_##n##_t_format, sizeof (vl_api_##n##_t), 0, vl_api_##n##_t_tojson, \
     vl_api_##n##_t_fromjson, vl_api_##n##_t_calc_size);
   foreach_sock_client_api_msg;
 #undef _
diff --git a/src/vlibmemory/vlib_api_cli.c b/src/vlibmemory/vlib_api_cli.c
index e53ea95..5b62e61 100644
--- a/src/vlibmemory/vlib_api_cli.c
+++ b/src/vlibmemory/vlib_api_cli.c
@@ -584,40 +584,24 @@
       switch (which)
 	{
 	case DUMP_JSON:
-	  if (m && m->print_json_handler)
-	    {
-	      m->print_json_handler (tmpbuf + sizeof (uword), vm);
-	    }
-	  else
-	    {
-	      vlib_cli_output (vm, "Skipping msg id %d: no JSON print fcn\n",
-			       msg_id);
-	      break;
-	    }
+	  vlib_cli_output (vm, "%U", format_vl_api_msg_json, am, msg_id,
+			   tmpbuf + sizeof (uword));
 	  break;
 
 	case DUMP:
-	  if (m && m->print_handler)
-	    {
-	      m->print_handler (tmpbuf + sizeof (uword), vm);
-	    }
-	  else
-	    {
-	      vlib_cli_output (vm, "Skipping msg id %d: no print fcn\n",
-			       msg_id);
-	      break;
-	    }
+	  vlib_cli_output (vm, "%U", format_vl_api_msg_text, am, msg_id,
+			   tmpbuf + sizeof (uword));
 	  break;
 
 	case INITIALIZERS:
-	  if (m && m->print_handler)
+	  if (m)
 	    {
 	      u8 *s;
 	      int j;
 
-	      vlib_cli_output (vm, "/*");
+	      vlib_cli_output (vm, "/*%U*/", format_vl_api_msg_text, am,
+			       msg_id, tmpbuf + sizeof (uword));
 
-	      m->print_handler (tmpbuf + sizeof (uword), vm);
 	      vlib_cli_output (vm, "*/\n");
 
 	      s = format (0, "static u8 * vl_api_%s_%d[%d] = {", m->name, i,
@@ -636,7 +620,7 @@
 	  break;
 
 	case REPLAY:
-	  if (m && m->print_handler && m->replay_allowed)
+	  if (m && m->handler && m->replay_allowed)
 	    {
 	      if (!m->is_mp_safe)
 		vl_msg_api_barrier_sync ();
@@ -690,7 +674,6 @@
   api_main_t *am = vlibapi_get_main ();
   u16 msg_id = ntohs (*((u16 *) msg));
   vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
-  void (*handler) (void *, void *) = 0;
   u8 is_json = a->is_json;
   u8 *tmpbuf = 0;
 
@@ -710,12 +693,9 @@
       m->endian_handler (tmpbuf);
     }
 
-  handler = is_json ? m->print_json_handler : m->print_handler;
-
-  if (handler)
-    handler (msg, a->vm);
-  else
-    vlib_cli_output (a->vm, "Skipping msg id %d: no print fcn\n", msg_id);
+  vlib_cli_output (a->vm, "%U\n",
+		   is_json ? format_vl_api_msg_json : format_vl_api_msg_text,
+		   am, msg_id, msg);
 
   vec_free (tmpbuf);
   return 0;
@@ -868,11 +848,14 @@
 	  goto end;
 	}
 
-      if (!m->is_mp_safe)
-	vl_msg_api_barrier_sync ();
-      m->handler (msg);
-      if (!m->is_mp_safe)
-	vl_msg_api_barrier_release ();
+      if (m->handler)
+	{
+	  if (!m->is_mp_safe)
+	    vl_msg_api_barrier_sync ();
+	  m->handler (msg);
+	  if (!m->is_mp_safe)
+	    vl_msg_api_barrier_release ();
+	}
     }
 
   rv = 0;