]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print: add nvme_show_perror() variable number arguments
authorTokunori Ikegami <ikegami.t@gmail.com>
Sun, 30 Mar 2025 10:58:44 +0000 (19:58 +0900)
committerDaniel Wagner <wagi@monom.org>
Wed, 2 Apr 2025 07:15:07 +0000 (07:15 +0000)
Then show_perror print function outputs the message with a va_list.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c
nvme-print-stdout.c
nvme-print.c
nvme-print.h

index b092f4377c0d564e5629363db430253bd33ae8c3..b7b198eb60daeb581717f5a626e6ad764b2af059 100644 (file)
@@ -4629,16 +4629,16 @@ static void json_output_message(bool error, const char *msg, va_list ap)
        obj_print(r);
 }
 
-static void json_output_perror(const char *msg)
+static void json_output_perror(const char *msg, va_list ap)
 {
        struct json_object *r = json_create_object();
 
        _cleanup_free_ char *error = NULL;
 
-       if (asprintf(&error, "%s: %s", msg, strerror(errno)) < 0)
+       if (vasprintf(&error, msg, ap) < 0)
                error = "Could not allocate string";
 
-       obj_add_str(r, "error", error);
+       obj_add_key(r, "error", "%s: %s", error, strerror(errno));
 
        json_output_object(r);
 }
index e87c605db41c5e4badf4482e3d385da98b3731bb..23d5ba9c87814af06c003ff46fe169cf525cc9bd 100644 (file)
@@ -5604,9 +5604,14 @@ static void stdout_message(bool error, const char *msg, va_list ap)
        fprintf(error ? stderr : stdout, "\n");
 }
 
-static void stdout_perror(const char *msg)
+static void stdout_perror(const char *msg, va_list ap)
 {
-       perror(msg);
+       _cleanup_free_ char *error = NULL;
+
+       if (vasprintf(&error, msg, ap) < 0)
+               error = "Could not allocate string";
+
+       perror(error);
 }
 
 static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec)
index 8492225e7fd6e65cafd85c6069438da744364b1e..824062a6f998295b0cf0f2faa8e60edece846d0d 100644 (file)
@@ -1460,15 +1460,20 @@ void nvme_show_message(bool error, const char *msg, ...)
        va_end(ap);
 }
 
-void nvme_show_perror(const char *msg)
+void nvme_show_perror(const char *msg, ...)
 {
        struct print_ops *ops = nvme_print_ops(NORMAL);
+       va_list ap;
+
+       va_start(ap, msg);
 
        if (nvme_is_output_format_json())
                ops = nvme_print_ops(JSON);
 
        if (ops && ops->show_perror)
-               ops->show_perror(msg);
+               ops->show_perror(msg, ap);
+
+       va_end(ap);
 }
 
 void nvme_show_discovery_log(struct nvmf_discovery_log *log, uint64_t numrec,
index ad5d4c829b3c27e424e72892f4d9679d341858cc..ac3f97856ea7be721a56a0020049a5017db4b996 100644 (file)
@@ -108,7 +108,7 @@ struct print_ops {
        /* status and error messages */
        void (*connect_msg)(nvme_ctrl_t c);
        void (*show_message)(bool error, const char *msg, va_list ap);
-       void (*show_perror)(const char *msg);
+       void (*show_perror)(const char *msg, va_list ap);
        void (*show_status)(int status);
        void (*show_error_status)(int status, const char *msg, va_list ap);
 
@@ -326,7 +326,7 @@ const char *nvme_pls_mode_to_string(__u8 mode);
 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_perror(const char *msg, ...);
 void nvme_show_error_status(int status, const char *msg, ...);
 void nvme_show_init(void);
 void nvme_show_finish(void);