From: Petr Machata Date: Thu, 12 Nov 2020 22:24:40 +0000 (+0100) Subject: lib: json_print: Add print_on_off() X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=9091ff0251f8da256d55ca4e994dbd14e32d08ad;hp=82604d28525aae8d9c5206d5c1d70aee88a7d960;p=mirror_iproute2.git lib: json_print: Add print_on_off() The value of a number of booleans is shown as "on" and "off" in the plain output, and as an actual boolean in JSON mode. Add a function that does that. RDMA tool already uses a function named print_on_off(). This function always shows "on" and "off", even in JSON mode. Since there are probably very few if any consumers of this interface at this point, migrate it to the new central print_on_off() as well. Signed-off-by: Petr Machata Reviewed-by: Leon Romanovsky Signed-off-by: David Ahern --- diff --git a/include/json_print.h b/include/json_print.h index 50e71de4..096a999a 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -65,6 +65,7 @@ void print_nl(void); _PRINT_FUNC(int, int) _PRINT_FUNC(s64, int64_t) _PRINT_FUNC(bool, bool) +_PRINT_FUNC(on_off, bool) _PRINT_FUNC(null, const char*) _PRINT_FUNC(string, const char*) _PRINT_FUNC(uint, unsigned int) diff --git a/lib/json_print.c b/lib/json_print.c index fe0705bf..62eeb1f1 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -191,11 +191,12 @@ int print_color_string(enum output_type type, * a value to it, you will need to use "is_json_context()" to have different * branch for json and regular output. grep -r "print_bool" for example */ -int print_color_bool(enum output_type type, - enum color_attr color, - const char *key, - const char *fmt, - bool value) +static int __print_color_bool(enum output_type type, + enum color_attr color, + const char *key, + const char *fmt, + bool value, + const char *str) { int ret = 0; @@ -205,13 +206,32 @@ int print_color_bool(enum output_type type, else jsonw_bool(_jw, value); } else if (_IS_FP_CONTEXT(type)) { - ret = color_fprintf(stdout, color, fmt, - value ? "true" : "false"); + ret = color_fprintf(stdout, color, fmt, str); } return ret; } +int print_color_bool(enum output_type type, + enum color_attr color, + const char *key, + const char *fmt, + bool value) +{ + return __print_color_bool(type, color, key, fmt, value, + value ? "true" : "false"); +} + +int print_color_on_off(enum output_type type, + enum color_attr color, + const char *key, + const char *fmt, + bool value) +{ + return __print_color_bool(type, color, key, fmt, value, + value ? "on" : "off"); +} + /* * In JSON context uses hardcode %#x format: 42 -> 0x2a */ diff --git a/rdma/dev.c b/rdma/dev.c index a11081b8..c684dde4 100644 --- a/rdma/dev.c +++ b/rdma/dev.c @@ -159,7 +159,7 @@ static void dev_print_dim_setting(struct rd *rd, struct nlattr **tb) if (dim_setting > 1) return; - print_on_off(rd, "adaptive-moderation", dim_setting); + print_on_off(PRINT_ANY, "adaptive-moderation", "adaptive-moderation %s ", dim_setting); } diff --git a/rdma/rdma.h b/rdma/rdma.h index a6c6bdea..8638b2a3 100644 --- a/rdma/rdma.h +++ b/rdma/rdma.h @@ -138,7 +138,6 @@ void print_driver_table(struct rd *rd, struct nlattr *tb); void print_raw_data(struct rd *rd, struct nlattr **nla_line); void newline(struct rd *rd); void newline_indent(struct rd *rd); -void print_on_off(struct rd *rd, const char *key_str, bool on); void print_raw_data(struct rd *rd, struct nlattr **nla_line); #define MAX_LINE_LENGTH 80 diff --git a/rdma/res-cq.c b/rdma/res-cq.c index 313f929a..9e7c4f51 100644 --- a/rdma/res-cq.c +++ b/rdma/res-cq.c @@ -36,7 +36,7 @@ static void print_cq_dim_setting(struct rd *rd, struct nlattr *attr) if (dim_setting > 1) return; - print_on_off(rd, "adaptive-moderation", dim_setting); + print_on_off(PRINT_ANY, "adaptive-moderation", "adaptive-moderation %s ", dim_setting); } static int res_cq_line_raw(struct rd *rd, const char *name, int idx, diff --git a/rdma/utils.c b/rdma/utils.c index 4d3de4fa..2a201aa4 100644 --- a/rdma/utils.c +++ b/rdma/utils.c @@ -781,11 +781,6 @@ static int print_driver_string(struct rd *rd, const char *key_str, return 0; } -void print_on_off(struct rd *rd, const char *key_str, bool on) -{ - print_driver_string(rd, key_str, (on) ? "on":"off"); -} - static int print_driver_s32(struct rd *rd, const char *key_str, int32_t val, enum rdma_nldev_print_type print_type) {