From 6aab9e49e1f32e63efebe6e666da3cd868cc1d28 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Sat, 25 Mar 2023 11:36:10 +0900 Subject: [PATCH] nvme-print-json: Add json output status function For the json output add json argconfig option also. Signed-off-by: Tokunori Ikegami --- nvme-print-json.c | 41 +++++++++++++++++++++++++++++++++++++++++ nvme-print-json.h | 2 ++ nvme-print.c | 3 +++ util/argconfig.c | 24 ++++++++++++++++++++---- util/argconfig.h | 1 + 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/nvme-print-json.c b/nvme-print-json.c index 40dbb2f8..eb7e484f 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include +#include #include "nvme-print-json.h" @@ -2813,3 +2814,43 @@ void json_connect_msg(nvme_ctrl_t c) printf("\n"); json_free_object(root); } + +static void json_output_object(struct json_object *root) +{ + json_print_object(root, NULL); + printf("\n"); + json_free_object(root); +} + +void json_output_status(int status) +{ + struct json_object *root = json_create_object(); + int val; + int type; + + if (status < 0) { + json_object_add_value_string(root, "error", nvme_strerror(errno)); + return json_output_object(root); + } + + val = nvme_status_get_value(status); + type = nvme_status_get_type(status); + + switch (type) { + case NVME_STATUS_TYPE_NVME: + json_object_add_value_string(root, "error", nvme_status_to_string(val, false)); + json_object_add_value_string(root, "type", "nvme"); + break; + case NVME_STATUS_TYPE_MI: + json_object_add_value_string(root, "error", nvme_mi_status_to_string(val)); + json_object_add_value_string(root, "type", "nvme-mi"); + break; + default: + json_object_add_value_string(root, "type", "unknow"); + break; + } + + json_object_add_value_int(root, "value", val); + + json_output_object(root); +} diff --git a/nvme-print-json.h b/nvme-print-json.h index 6720d454..3641c80b 100644 --- a/nvme-print-json.h +++ b/nvme-print-json.h @@ -78,6 +78,7 @@ void json_predictable_latency_event_agg_log( void json_predictable_latency_per_nvmset( struct nvme_nvmset_predictable_lat_log *plpns_log, __u16 nvmset_id); +void json_output_status(int status); /* fabrics.c */ void json_discovery_log(struct nvmf_discovery_log *log, int numrec); @@ -136,6 +137,7 @@ void json_connect_msg(nvme_ctrl_t c); #define json_persistent_event_log(pevent_log_info, size) #define json_predictable_latency_event_agg_log(pea_log, log_entries) #define json_predictable_latency_per_nvmset(plpns_log, nvmset_id) +#define json_output_status(status) /* fabrics.c */ #define json_discovery_log(log, numrec) diff --git a/nvme-print.c b/nvme-print.c index ce4abe07..356fb440 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -1744,6 +1744,9 @@ void nvme_show_status(int status) int val; int type; + if (argconfig_output_format_json(false)) + return json_output_status(status); + /* * Callers should be checking for negative values first, but provide a * sensible fallback anyway diff --git a/util/argconfig.c b/util/argconfig.c index a891dbbe..3eb885fa 100644 --- a/util/argconfig.c +++ b/util/argconfig.c @@ -285,6 +285,16 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct return ret; } +bool argconfig_output_format_json(bool set) +{ + static bool output_format_json = false; + + if (set) + output_format_json = true; + + return output_format_json; +} + int argconfig_parse(int argc, char *argv[], const char *program_desc, struct argconfig_commandline_options *options) { @@ -298,8 +308,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc, for (s = options; s->option; s++) options_count++; - long_opts = calloc(1, sizeof(struct option) * (options_count + 2)); - short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 4)); + long_opts = calloc(1, sizeof(struct option) * (options_count + 3)); + short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 5)); if (!long_opts || !short_opts) { fprintf(stderr, "failed to allocate memory for opts: %s\n", strerror(errno)); @@ -325,10 +335,14 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc, } long_opts[option_index].name = "help"; - long_opts[option_index].val = 'h'; + long_opts[option_index++].val = 'h'; + + long_opts[option_index].name = "json"; + long_opts[option_index].val = 'j'; short_opts[short_index++] = '?'; - short_opts[short_index] = 'h'; + short_opts[short_index++] = 'h'; + short_opts[short_index] = 'j'; optind = 0; while ((c = getopt_long_only(argc, argv, short_opts, long_opts, &option_index)) != -1) { @@ -338,6 +352,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc, ret = -EINVAL; break; } + if (c == 'j') + argconfig_output_format_json(true); for (option_index = 0; option_index < options_count; option_index++) { if (c == options[option_index].short_option) break; diff --git a/util/argconfig.h b/util/argconfig.h index b691159f..81eaaf40 100644 --- a/util/argconfig.h +++ b/util/argconfig.h @@ -137,4 +137,5 @@ void argconfig_register_help_func(argconfig_help_func * f); void print_word_wrapped(const char *s, int indent, int start, FILE *stream); bool argconfig_parse_seen(struct argconfig_commandline_options *options, const char *option); +bool argconfig_output_format_json(bool set); #endif -- 2.49.0