Skip to content

Commit 6ae8d7d

Browse files
committed
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 71fa5d9 commit 6ae8d7d

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
@@ -4652,6 +4652,20 @@ static void json_output_perror(const char *msg)
46524652
json_output_object(r);
46534653
}
46544654

4655+
static void json_output_key_value(const char *key, const char *val, va_list ap)
4656+
{
4657+
struct json_object *r = json_r ? json_r : json_create_object();
4658+
4659+
_cleanup_free_ char *value = NULL;
4660+
4661+
if (vasprintf(&value, val, ap) < 0)
4662+
value = NULL;
4663+
4664+
obj_add_str(r, key, value ? value : "Could not allocate string");
4665+
4666+
obj_print(r);
4667+
}
4668+
46554669
void json_show_init(void)
46564670
{
46574671
json_r = json_create_object();
@@ -5016,6 +5030,7 @@ static struct print_ops json_print_ops = {
50165030
.show_perror = json_output_perror,
50175031
.show_status = json_output_status,
50185032
.show_error_status = json_output_error_status,
5033+
.show_key_value = json_output_key_value,
50195034
};
50205035

50215036
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
@@ -5609,6 +5609,16 @@ static void stdout_perror(const char *msg)
56095609
perror(msg);
56105610
}
56115611

5612+
static void stdout_key_value(const char *key, const char *val, va_list ap)
5613+
{
5614+
_cleanup_free_ char *value = NULL;
5615+
5616+
if (vasprintf(&value, val, ap) < 0)
5617+
value = NULL;
5618+
5619+
printf("%s: %s\n", key, value ? value : "Could not allocate string");
5620+
}
5621+
56125622
static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec)
56135623
{
56145624
int i;
@@ -5956,6 +5966,7 @@ static struct print_ops stdout_print_ops = {
59565966
.show_perror = stdout_perror,
59575967
.show_status = stdout_status,
59585968
.show_error_status = stdout_error_status,
5969+
.show_key_value = stdout_key_value,
59595970
};
59605971

59615972
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
@@ -1471,6 +1471,22 @@ void nvme_show_perror(const char *msg)
14711471
ops->show_perror(msg);
14721472
}
14731473

1474+
void nvme_show_key_value(const char *key, const char *val, ...)
1475+
{
1476+
struct print_ops *ops = nvme_print_ops(NORMAL);
1477+
va_list ap;
1478+
1479+
va_start(ap, val);
1480+
1481+
if (nvme_is_output_format_json())
1482+
ops = nvme_print_ops(JSON);
1483+
1484+
if (ops && ops->show_key_value)
1485+
ops->show_key_value(key, val, ap);
1486+
1487+
va_end(ap);
1488+
}
1489+
14741490
void nvme_show_discovery_log(struct nvmf_discovery_log *log, uint64_t numrec,
14751491
nvme_print_flags_t flags)
14761492
{

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);
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
};
@@ -330,6 +331,7 @@ void nvme_show_perror(const char *msg);
330331
void nvme_show_error_status(int status, const char *msg, ...);
331332
void nvme_show_init(void);
332333
void nvme_show_finish(void);
334+
void nvme_show_key_value(const char *key, const char *value, ...);
333335
bool nvme_is_fabrics_reg(int offset);
334336
bool nvme_is_fabrics_optional_reg(int offset);
335337
bool nvme_registers_cmbloc_support(__u32 cmbsz);

0 commit comments

Comments
 (0)