Skip to content

Commit cb542cb

Browse files
committed
nvme-print: add nvme_show_perror() variable number arguments
Then show_perror print function outputs the message with a va_list. Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
1 parent d8a47ea commit cb542cb

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

nvme-print-json.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,16 +4629,16 @@ static void json_output_message(bool error, const char *msg, va_list ap)
46294629
obj_print(r);
46304630
}
46314631

4632-
static void json_output_perror(const char *msg)
4632+
static void json_output_perror(const char *msg, va_list ap)
46334633
{
46344634
struct json_object *r = json_create_object();
46354635

46364636
_cleanup_free_ char *error = NULL;
46374637

4638-
if (asprintf(&error, "%s: %s", msg, strerror(errno)) < 0)
4638+
if (vasprintf(&error, msg, ap) < 0)
46394639
error = "Could not allocate string";
46404640

4641-
obj_add_str(r, "error", error);
4641+
obj_add_key(r, "error", "%s: %s", error, strerror(errno));
46424642

46434643
json_output_object(r);
46444644
}

nvme-print-stdout.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,9 +5604,14 @@ static void stdout_message(bool error, const char *msg, va_list ap)
56045604
fprintf(error ? stderr : stdout, "\n");
56055605
}
56065606

5607-
static void stdout_perror(const char *msg)
5607+
static void stdout_perror(const char *msg, va_list ap)
56085608
{
5609-
perror(msg);
5609+
_cleanup_free_ char *error = NULL;
5610+
5611+
if (vasprintf(&error, msg, ap) < 0)
5612+
error = "Could not allocate string";
5613+
5614+
perror(error);
56105615
}
56115616

56125617
static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec)

nvme-print.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,15 +1460,20 @@ void nvme_show_message(bool error, const char *msg, ...)
14601460
va_end(ap);
14611461
}
14621462

1463-
void nvme_show_perror(const char *msg)
1463+
void nvme_show_perror(const char *msg, ...)
14641464
{
14651465
struct print_ops *ops = nvme_print_ops(NORMAL);
1466+
va_list ap;
1467+
1468+
va_start(ap, msg);
14661469

14671470
if (nvme_is_output_format_json())
14681471
ops = nvme_print_ops(JSON);
14691472

14701473
if (ops && ops->show_perror)
1471-
ops->show_perror(msg);
1474+
ops->show_perror(msg, ap);
1475+
1476+
va_end(ap);
14721477
}
14731478

14741479
void nvme_show_discovery_log(struct nvmf_discovery_log *log, uint64_t numrec,

nvme-print.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct print_ops {
108108
/* status and error messages */
109109
void (*connect_msg)(nvme_ctrl_t c);
110110
void (*show_message)(bool error, const char *msg, va_list ap);
111-
void (*show_perror)(const char *msg);
111+
void (*show_perror)(const char *msg, va_list ap);
112112
void (*show_status)(int status);
113113
void (*show_error_status)(int status, const char *msg, va_list ap);
114114

@@ -326,7 +326,7 @@ const char *nvme_pls_mode_to_string(__u8 mode);
326326
void nvme_dev_full_path(nvme_ns_t n, char *path, size_t len);
327327
void nvme_generic_full_path(nvme_ns_t n, char *path, size_t len);
328328
void nvme_show_message(bool error, const char *msg, ...);
329-
void nvme_show_perror(const char *msg);
329+
void nvme_show_perror(const char *msg, ...);
330330
void nvme_show_error_status(int status, const char *msg, ...);
331331
void nvme_show_init(void);
332332
void nvme_show_finish(void);

0 commit comments

Comments
 (0)