Skip to content

Commit a7db1f2

Browse files
ikegami-tigaw
authored andcommitted
nvme-print: add show_key_value print_ops
This is to output a command result as json key and value. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 27b6d48 commit a7db1f2

5 files changed

Lines changed: 45 additions & 0 deletions

File tree

nvme-print-binary.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ static struct print_ops binary_print_ops = {
435435
.show_perror = NULL,
436436
.show_status = NULL,
437437
.show_error_status = NULL,
438+
.show_key_value = NULL,
438439
};
439440

440441
struct print_ops *nvme_get_binary_print_ops(nvme_print_flags_t flags)

nvme-print-json.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4900,6 +4900,20 @@ static void json_output_perror(const char *msg, va_list ap)
49004900
json_output_object(r);
49014901
}
49024902

4903+
static void json_output_key_value(const char *key, const char *val, va_list ap)
4904+
{
4905+
struct json_object *r = json_r ? json_r : json_create_object();
4906+
4907+
_cleanup_free_ char *value = NULL;
4908+
4909+
if (vasprintf(&value, val, ap) < 0)
4910+
value = NULL;
4911+
4912+
obj_add_str(r, key, value ? value : "Could not allocate string");
4913+
4914+
obj_print(r);
4915+
}
4916+
49034917
void json_show_init(void)
49044918
{
49054919
json_r = json_create_object();
@@ -5264,6 +5278,7 @@ static struct print_ops json_print_ops = {
52645278
.show_perror = json_output_perror,
52655279
.show_status = json_output_status,
52665280
.show_error_status = json_output_error_status,
5281+
.show_key_value = json_output_key_value,
52675282
};
52685283

52695284
struct print_ops *nvme_get_json_print_ops(nvme_print_flags_t flags)

nvme-print-stdout.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5705,6 +5705,16 @@ static void stdout_perror(const char *msg, va_list ap)
57055705
perror(error);
57065706
}
57075707

5708+
static void stdout_key_value(const char *key, const char *val, va_list ap)
5709+
{
5710+
_cleanup_free_ char *value = NULL;
5711+
5712+
if (vasprintf(&value, val, ap) < 0)
5713+
value = NULL;
5714+
5715+
printf("%s: %s\n", key, value ? value : "Could not allocate string");
5716+
}
5717+
57085718
static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec)
57095719
{
57105720
int i;
@@ -6052,6 +6062,7 @@ static struct print_ops stdout_print_ops = {
60526062
.show_perror = stdout_perror,
60536063
.show_status = stdout_status,
60546064
.show_error_status = stdout_error_status,
6065+
.show_key_value = stdout_key_value,
60556066
};
60566067

60576068
struct print_ops *nvme_get_stdout_print_ops(nvme_print_flags_t flags)

nvme-print.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,22 @@ void nvme_show_perror(const char *msg, ...)
15861586
va_end(ap);
15871587
}
15881588

1589+
void nvme_show_key_value(const char *key, const char *val, ...)
1590+
{
1591+
struct print_ops *ops = nvme_print_ops(NORMAL);
1592+
va_list ap;
1593+
1594+
va_start(ap, val);
1595+
1596+
if (nvme_is_output_format_json())
1597+
ops = nvme_print_ops(JSON);
1598+
1599+
if (ops && ops->show_key_value)
1600+
ops->show_key_value(key, val, ap);
1601+
1602+
va_end(ap);
1603+
}
1604+
15891605
void nvme_show_discovery_log(struct nvmf_discovery_log *log, uint64_t numrec,
15901606
nvme_print_flags_t flags)
15911607
{

nvme-print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct print_ops {
111111
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);
114+
void (*show_key_value)(const char *key, const char *val, va_list ap);
114115

115116
nvme_print_flags_t flags;
116117
};
@@ -334,6 +335,7 @@ void nvme_show_perror(const char *msg, ...);
334335
void nvme_show_error_status(int status, const char *msg, ...);
335336
void nvme_show_init(void);
336337
void nvme_show_finish(void);
338+
void nvme_show_key_value(const char *key, const char *value, ...);
337339
bool nvme_is_fabrics_reg(int offset);
338340
bool nvme_is_fabrics_optional_reg(int offset);
339341
bool nvme_registers_cmbloc_support(__u32 cmbsz);

0 commit comments

Comments
 (0)