From fe45f8f5afbf34d68cf992cc32b12432a82cdb38 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Fri, 20 May 2022 16:01:22 +0200 Subject: api: replace print functions wth format Type: improvement Change-Id: I7f7050c19453a69a7fb6c5e62f8f57db847d9144 Signed-off-by: Damjan Marion --- src/vlibapi/CMakeLists.txt | 1 + src/vlibapi/api.h | 3 +++ src/vlibapi/api_common.h | 20 +++++++------------- src/vlibapi/api_doc.rst | 1 - src/vlibapi/api_format.c | 39 +++++++++++++++++++++++++++++++++++++++ src/vlibapi/api_shared.c | 25 ++++++------------------- 6 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 src/vlibapi/api_format.c (limited to 'src/vlibapi') diff --git a/src/vlibapi/CMakeLists.txt b/src/vlibapi/CMakeLists.txt index 95bb4cf7469..6476b5a2f33 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 f838a80cf71..705678b7965 100644 --- a/src/vlibapi/api.h +++ b/src/vlibapi/api.h @@ -148,6 +148,9 @@ vl_api_allow_msg_replay (api_main_t *am, u32 msg_id, int v) 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 66a547fb93d..37ffb587732 100644 --- a/src/vlibapi/api_common.h +++ b/src/vlibapi/api_common.h @@ -122,8 +122,7 @@ typedef struct 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_cleanup_handler (void *the_msg); 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 @@ void vl_msg_api_barrier_trace_context (const char *context) #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 @@ typedef struct /** 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 @@ typedef struct /** 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 7d2b80a2e06..2131cc1919c 100644 --- a/src/vlibapi/api_doc.rst +++ b/src/vlibapi/api_doc.rst @@ -300,7 +300,6 @@ Set up message handlers about as follows: #undef vl_endianfun /* instantiate all the print functions we know about */ - #define vl_print(handle, ...) #define vl_printfun #include #undef vl_printfun diff --git a/src/vlibapi/api_format.c b/src/vlibapi/api_format.c new file mode 100644 index 00000000000..f7bb9d3970f --- /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 +#include + +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 73ef3ce54d8..0a983488bec 100644 --- a/src/vlibapi/api_shared.c +++ b/src/vlibapi/api_shared.c @@ -531,10 +531,7 @@ msg_handler_internal (api_main_t *am, void *the_msg, uword msg_len, 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 @@ vl_msg_api_config (vl_msg_api_msg_config_t * c) 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 @@ vl_msg_api_config (vl_msg_api_msg_config_t * c) * 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 @@ vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup, 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 @@ vl_msg_api_set_handlers (int id, char *name, void *handler, void *cleanup, 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 @@ vl_msg_api_trace_get (api_main_t * am, vl_api_trace_which_t which) } } -void -vl_noop_handler (void *mp) -{ -} - - static u8 post_mortem_dump_enabled; void -- cgit 1.2.3-korg