From: Tokunori Ikegami Date: Fri, 3 Nov 2023 05:18:48 +0000 (+0900) Subject: nvme-print: Add nvme_show_error_status() to merge error message and status X-Git-Tag: v2.7~58 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=c9c1dc968c0fdd7e031b0a23227df1a10a9ea383;p=users%2Fsagi%2Fnvme-cli.git nvme-print: Add nvme_show_error_status() to merge error message and status Signed-off-by: Tokunori Ikegami --- diff --git a/nvme-print-json.c b/nvme-print-json.c index 044f07db..1525a1ec 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -4423,6 +4423,51 @@ static void json_output_status(int status) json_output_object(r); } +static void json_output_error_status(int status, const char *msg, va_list ap) +{ + struct json_object *r = json_create_object(); + char *value; + const char *key = "Error"; + int val; + int type; + + if (vasprintf(&value, msg, ap) < 0) + value = NULL; + + if (value) + obj_add_str(r, key, value); + else + obj_add_str(r, key, "Could not allocate string"); + + free(value); + + if (status < 0) { + obj_add_str(r, "Error", nvme_strerror(errno)); + return json_output_object(r); + } + + val = nvme_status_get_value(status); + type = nvme_status_get_type(status); + + switch (type) { + case NVME_STATUS_TYPE_NVME: + obj_add_str(r, "Status", nvme_status_to_string(val, false)); + obj_add_str(r, "Type", "nvme"); + break; + case NVME_STATUS_TYPE_MI: + obj_add_str(r, "Status", nvme_mi_status_to_string(val)); + obj_add_str(r, "Type", "nvme-mi"); + break; + default: + obj_add_str(r, "Type", "Unknown"); + break; + } + + obj_add_int(r, "Value", val); + + json_output_object(r); +} + static void json_output_message(bool error, const char *msg, va_list ap) { struct json_object *r = json_create_object(); @@ -4537,6 +4582,7 @@ static struct print_ops json_print_ops = { .show_message = json_output_message, .show_perror = json_output_perror, .show_status = json_output_status, + .show_error_status = json_output_error_status, }; struct print_ops *nvme_get_json_print_ops(enum nvme_print_flags flags) diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index c5df574a..40d17cbf 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -1665,6 +1665,13 @@ static void stdout_status(int status) } } +static void stdout_error_status(int status, const char *msg, va_list ap) +{ + vfprintf(stderr, msg, ap); + + stdout_status(status); +} + static void stdout_id_ctrl_cmic(__u8 cmic) { __u8 rsvd = (cmic & 0xF0) >> 4; @@ -5022,7 +5029,7 @@ static void stdout_message(bool error, const char *msg, va_list ap) { vfprintf(error ? stderr : stdout, msg, ap); - printf("\n"); + fprintf(error ? stderr : stdout, "\n"); } static void stdout_perror(const char *msg) @@ -5155,6 +5162,7 @@ static struct print_ops stdout_print_ops = { .show_message = stdout_message, .show_perror = stdout_perror, .show_status = stdout_status, + .show_error_status = stdout_error_status, }; struct print_ops *nvme_get_stdout_print_ops(enum nvme_print_flags flags) diff --git a/nvme-print.c b/nvme-print.c index 362ad6ff..920929eb 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -394,22 +394,33 @@ void d_raw(unsigned char *buf, unsigned len) void nvme_show_status(int status) { - struct print_ops *ops; + struct print_ops *ops = nvme_print_ops(0); if (nvme_is_output_format_json()) ops = nvme_print_ops(JSON); - else - ops =nvme_print_ops(0); - if (!ops) - return; - - if (!ops->show_status) + if (!ops || !ops->show_status) return; ops->show_status(status); } +void nvme_show_error_status(int status, const char *msg, ...) +{ + struct print_ops *ops = nvme_print_ops(0); + va_list ap; + + va_start(ap, msg); + + if (nvme_is_output_format_json()) + ops = nvme_print_ops(JSON); + + if (ops && ops->show_status) + ops->show_error_status(status, msg, ap); + + va_end(ap); +} + void nvme_show_id_ctrl_rpmbs(__le32 ctrl_rpmbs, enum nvme_print_flags flags) { nvme_print(id_ctrl_rpmbs, flags, ctrl_rpmbs); @@ -1025,21 +1036,16 @@ void nvme_show_topology(nvme_root_t r, void nvme_show_message(bool error, const char *msg, ...) { - struct print_ops *ops; + struct print_ops *ops = nvme_print_ops(0); va_list ap; + va_start(ap, msg); if (nvme_is_output_format_json()) ops = nvme_print_ops(JSON); - else - ops = nvme_print_ops(0); - - if (!ops) - return; - if (!ops->show_message) - return; - ops->show_message(error, msg, ap); + if (ops && ops->show_message) + ops->show_message(error, msg, ap); va_end(ap); } diff --git a/nvme-print.h b/nvme-print.h index 1eee46b2..6961a31f 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -96,6 +96,7 @@ struct print_ops { void (*show_message)(bool error, const char *msg, va_list ap); void (*show_perror)(const char *msg); void (*show_status)(int status); + void (*show_error_status)(int status, const char *msg, va_list ap); enum nvme_print_flags flags; }; @@ -304,5 +305,6 @@ void nvme_dev_full_path(nvme_ns_t n, char *path, size_t len); void nvme_generic_full_path(nvme_ns_t n, char *path, size_t len); void nvme_show_message(bool error, const char *msg, ...); void nvme_show_perror(const char *msg); +void nvme_show_error_status(int status, const char *msg, ...); #endif diff --git a/nvme.c b/nvme.c index f203119c..c270fcd0 100644 --- a/nvme.c +++ b/nvme.c @@ -4649,13 +4649,11 @@ static int get_feature_ids(struct nvme_dev *dev, struct feat_cfg cfg) continue; if (!nvme_status_equals(status, type, NVME_SC_INVALID_NS)) break; - nvme_show_error("get-feature:%#0*x (%s): ", cfg.feature_id ? 4 : 2, cfg.feature_id, - nvme_feature_to_string(cfg.feature_id)); - nvme_show_status(err); + nvme_show_error_status(err, "get-feature:%#0*x (%s): ", cfg.feature_id ? 4 : 2, + cfg.feature_id, nvme_feature_to_string(cfg.feature_id)); } - if (feat_num == 1 && - nvme_status_equals(status, type, NVME_SC_INVALID_FIELD)) + if (feat_num == 1 && nvme_status_equals(status, type, NVME_SC_INVALID_FIELD)) nvme_show_status(err); return err;