]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print: Add nvme_show_error_status() to merge error message and status
authorTokunori Ikegami <ikegami.t@gmail.com>
Fri, 3 Nov 2023 05:18:48 +0000 (14:18 +0900)
committerDaniel Wagner <wagi@monom.org>
Thu, 16 Nov 2023 10:05:54 +0000 (11:05 +0100)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c
nvme-print-stdout.c
nvme-print.c
nvme-print.h
nvme.c

index 044f07dbe1ed2242406e828b01f3cbee81a4588e..1525a1ecd10805cd64323b541fd59777dc21ebd5 100644 (file)
@@ -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)
index c5df574ad5c8085b02d4750c73dd5d0eec0de738..40d17cbffab2e97b83274a5faa6262795ada5dc5 100644 (file)
@@ -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)
index 362ad6ffe293ca063cc951472449d9d7291be3ca..920929eb5bcdbca26ac9e7f26e49adb5cc32a106 100644 (file)
@@ -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);
 }
index 1eee46b2cad78b1e0d0bbf76b6ae9153286a4d8a..6961a31f7e33997a090c1179abcf88b32d23f81a 100644 (file)
@@ -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 f203119cd1c90c436d4086c82b1b24d3ee23c406..c270fcd0aee0412f6a2ea2e2ddc5ed0fa017d4ed 100644 (file)
--- 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;