api: replace print functions wth format
Type: improvement
Change-Id: I7f7050c19453a69a7fb6c5e62f8f57db847d9144
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlibapi/CMakeLists.txt b/src/vlibapi/CMakeLists.txt
index 95bb4cf..6476b5a 100644
--- a/src/vlibapi/CMakeLists.txt
+++ b/src/vlibapi/CMakeLists.txt
@@ -14,6 +14,7 @@
add_vpp_library (vlibapi
SOURCES
api_shared.c
+ api_format.c
node_serialize.c
memory_shared.c
diff --git a/src/vlibapi/api.h b/src/vlibapi/api.h
index f838a80..705678b 100644
--- a/src/vlibapi/api.h
+++ b/src/vlibapi/api.h
@@ -148,6 +148,9 @@
am->msg_data[msg_id].replay_allowed = v != 0;
}
+format_function_t format_vl_api_msg_text;
+format_function_t format_vl_api_msg_json;
+
#endif /* included_api_h */
/*
* fd.io coding-style-patch-verification: ON
diff --git a/src/vlibapi/api_common.h b/src/vlibapi/api_common.h
index 66a547f..37ffb58 100644
--- a/src/vlibapi/api_common.h
+++ b/src/vlibapi/api_common.h
@@ -122,8 +122,7 @@
void *handler; /**< the message handler */
void *cleanup; /**< non-default message cleanup handler */
void *endian; /**< message endian function */
- void *print; /**< message print function */
- void *print_json; /**< message print function (JSON format) */
+ void *format_fn; /**< message format function */
void *tojson; /**< binary to JSON convert function */
void *fromjson; /**< JSON to binary convert function */
void *calc_size; /**< message size calculation */
@@ -173,10 +172,9 @@
void vl_msg_api_replay_handler (void *the_msg);
void vl_msg_api_socket_handler (void *the_msg, uword msg_len);
void vl_msg_api_set_handlers (int msg_id, char *msg_name, void *handler,
- void *cleanup, void *endian, void *print,
- int msg_size, int traced, void *print_json,
- void *tojson, void *fromjson,
- void *validate_size);
+ void *endian, format_function_t *format,
+ int msg_size, int traced, void *tojson,
+ void *fromjson, void *validate_size);
void vl_msg_api_clean_handlers (int msg_id);
void vl_msg_api_config (vl_msg_api_msg_config_t *);
void vl_msg_api_set_cleanup_handler (int msg_id, void *fp);
@@ -191,7 +189,6 @@
#define vl_msg_api_barrier_trace_context(X)
#endif
void vl_msg_api_free (void *);
-void vl_noop_handler (void *mp);
void vl_msg_api_increment_missing_client_counter (void);
void vl_msg_api_post_mortem_dump (void);
void vl_msg_api_post_mortem_dump_enable_disable (int enable);
@@ -234,6 +231,9 @@
/** Message name vector */
const char *name;
+ /** Message format function */
+ format_function_t *format_fn;
+
/** Message convert function vector */
cJSON *(*tojson_handler) (void *);
@@ -243,12 +243,6 @@
/** Message endian handler vector */
void (*endian_handler) (void *);
- /** Message print function vector */
- void (*print_handler) (void *, void *);
-
- /** Message print function vector in JSON */
- void (*print_json_handler) (void *, void *);
-
/** Message calc size function vector */
uword (*calc_size_func) (void *);
diff --git a/src/vlibapi/api_doc.rst b/src/vlibapi/api_doc.rst
index 7d2b80a..2131cc1 100644
--- a/src/vlibapi/api_doc.rst
+++ b/src/vlibapi/api_doc.rst
@@ -300,7 +300,6 @@
#undef vl_endianfun
/* instantiate all the print functions we know about */
- #define vl_print(handle, ...)
#define vl_printfun
#include <vpp/api/vpe_all_api_h.h>
#undef vl_printfun
diff --git a/src/vlibapi/api_format.c b/src/vlibapi/api_format.c
new file mode 100644
index 0000000..f7bb9d3
--- /dev/null
+++ b/src/vlibapi/api_format.c
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: Apache-2.0
+ * Copyright(c) 2022 Cisco Systems, Inc.
+ */
+
+#include <vppinfra/format.h>
+#include <vlibapi/api.h>
+
+u8 *
+format_vl_api_msg_text (u8 *s, va_list *args)
+{
+ api_main_t *am = va_arg (*args, api_main_t *);
+ u32 msg_id = va_arg (*args, u32);
+ void *msg = va_arg (*args, void *);
+ vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
+
+ if (m->format_fn)
+ s = format (s, "%U", m->format_fn, msg);
+ else
+ s = format (s, "[format handler missing for `%s`]", m->name);
+ return s;
+}
+
+u8 *
+format_vl_api_msg_json (u8 *s, va_list *args)
+{
+ api_main_t *am = va_arg (*args, api_main_t *);
+ u32 msg_id = va_arg (*args, u32);
+ void *msg = va_arg (*args, void *);
+ vl_api_msg_data_t *m = vl_api_get_msg_data (am, msg_id);
+
+ cJSON *o = m->tojson_handler (msg);
+ char *out = cJSON_Print (o);
+
+ s = format (s, "%s", out);
+
+ cJSON_Delete (o);
+ cJSON_free (out);
+ return s;
+}
diff --git a/src/vlibapi/api_shared.c b/src/vlibapi/api_shared.c
index 73ef3ce..0a98348 100644
--- a/src/vlibapi/api_shared.c
+++ b/src/vlibapi/api_shared.c
@@ -531,10 +531,7 @@
if (am->msg_print_flag)
{
fformat (stdout, "[%d]: %s\n", id, m->name);
- if (m->print_handler)
- m->print_handler (the_msg, stdout);
- else
- fformat (stdout, " [no registered print fn]\n");
+ fformat (stdout, "%U", format_vl_api_msg_text, am, id, the_msg);
}
uword calc_size = 0;
@@ -769,8 +766,7 @@
m->handler = c->handler;
m->cleanup_handler = c->cleanup;
m->endian_handler = c->endian;
- m->print_handler = c->print;
- m->print_json_handler = c->print_json;
+ m->format_fn = c->format_fn;
m->tojson_handler = c->tojson;
m->fromjson_handler = c->fromjson;
m->calc_size_func = c->calc_size;
@@ -793,10 +789,9 @@
* preserve the old API for a while
*/
void
-vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup,
- void *endian, void *print, int size, int traced,
- void *print_json, void *tojson, void *fromjson,
- void *calc_size)
+vl_msg_api_set_handlers (int id, char *name, void *handler, void *endian,
+ format_function_t *format, int size, int traced,
+ void *tojson, void *fromjson, void *calc_size)
{
vl_msg_api_msg_config_t cfg;
vl_msg_api_msg_config_t *c = &cfg;
@@ -806,9 +801,8 @@
c->id = id;
c->name = name;
c->handler = handler;
- c->cleanup = cleanup;
c->endian = endian;
- c->print = print;
+ c->format_fn = format;
c->traced = traced;
c->replay = 1;
c->message_bounce = 0;
@@ -816,7 +810,6 @@
c->is_autoendian = 0;
c->tojson = tojson;
c->fromjson = fromjson;
- c->print_json = print_json;
c->calc_size = calc_size;
vl_msg_api_config (c);
}
@@ -884,12 +877,6 @@
}
}
-void
-vl_noop_handler (void *mp)
-{
-}
-
-
static u8 post_mortem_dump_enabled;
void