devlink: Replace json prints by common library functions

Substitute json prints to use json_print.c common library functions,
instead of directly calling jsonw_functions.

Signed-off-by: Ron Diskin <rondi@mellanox.com>
Reviewed-by: Moshe Shemesh <moshe@mellanox.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Ron Diskin 2020-01-23 12:32:28 +02:00 committed by Stephen Hemminger
parent 98e48e7dd0
commit b20555e402
1 changed files with 61 additions and 86 deletions

View File

@ -1715,19 +1715,17 @@ static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb,
if (dl->json_output) { if (dl->json_output) {
if (array) { if (array) {
if (should_arr_last_handle_end(dl, bus_name, dev_name)) if (should_arr_last_handle_end(dl, bus_name, dev_name))
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
if (should_arr_last_handle_start(dl, bus_name, if (should_arr_last_handle_start(dl, bus_name,
dev_name)) { dev_name)) {
jsonw_name(dl->jw, buf); open_json_array(PRINT_JSON, buf);
jsonw_start_array(dl->jw); open_json_object(NULL);
jsonw_start_object(dl->jw);
arr_last_handle_set(dl, bus_name, dev_name); arr_last_handle_set(dl, bus_name, dev_name);
} else { } else {
jsonw_start_object(dl->jw); open_json_object(NULL);
} }
} else { } else {
jsonw_name(dl->jw, buf); open_json_object(buf);
jsonw_start_object(dl->jw);
} }
} else { } else {
if (array) { if (array) {
@ -1754,7 +1752,7 @@ static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb)
static void pr_out_handle_end(struct dl *dl) static void pr_out_handle_end(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_object(dl->jw); close_json_object();
else else
__pr_out_newline(); __pr_out_newline();
} }
@ -1816,21 +1814,19 @@ static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name,
if (should_arr_last_port_handle_end(dl, bus_name, if (should_arr_last_port_handle_end(dl, bus_name,
dev_name, dev_name,
port_index)) port_index))
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
if (should_arr_last_port_handle_start(dl, bus_name, if (should_arr_last_port_handle_start(dl, bus_name,
dev_name, dev_name,
port_index)) { port_index)) {
jsonw_name(dl->jw, buf); open_json_array(PRINT_JSON, buf);
jsonw_start_array(dl->jw); open_json_object(NULL);
jsonw_start_object(dl->jw);
arr_last_port_handle_set(dl, bus_name, dev_name, arr_last_port_handle_set(dl, bus_name, dev_name,
port_index); port_index);
} else { } else {
jsonw_start_object(dl->jw); open_json_object(NULL);
} }
} else { } else {
jsonw_name(dl->jw, buf); open_json_object(buf);
jsonw_start_object(dl->jw);
} }
} else { } else {
pr_out("%s:", buf); pr_out("%s:", buf);
@ -1864,7 +1860,7 @@ static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool
static void pr_out_port_handle_end(struct dl *dl) static void pr_out_port_handle_end(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_object(dl->jw); close_json_object();
else else
pr_out("\n"); pr_out("\n");
} }
@ -1874,7 +1870,7 @@ static void pr_out_str(struct dl *dl, const char *name, const char *val)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_string_field(dl->jw, name, val); print_string(PRINT_JSON, name, NULL, val);
else else
pr_out("%s %s", name, val); pr_out("%s %s", name, val);
} }
@ -1882,7 +1878,7 @@ static void pr_out_str(struct dl *dl, const char *name, const char *val)
static void pr_out_bool(struct dl *dl, const char *name, bool val) static void pr_out_bool(struct dl *dl, const char *name, bool val)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_bool_field(dl->jw, name, val); print_bool(PRINT_JSON, name, NULL, val);
else else
pr_out_str(dl, name, val ? "true" : "false"); pr_out_str(dl, name, val ? "true" : "false");
} }
@ -1891,7 +1887,7 @@ static void pr_out_uint(struct dl *dl, const char *name, unsigned int val)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_uint_field(dl->jw, name, val); print_uint(PRINT_JSON, name, NULL, val);
else else
pr_out("%s %u", name, val); pr_out("%s %u", name, val);
} }
@ -1903,7 +1899,7 @@ static void pr_out_u64(struct dl *dl, const char *name, uint64_t val)
return pr_out_str(dl, name, "unlimited"); return pr_out_str(dl, name, "unlimited");
if (dl->json_output) if (dl->json_output)
jsonw_u64_field(dl->jw, name, val); print_u64(PRINT_JSON, name, NULL, val);
else else
pr_out("%s %"PRIu64, name, val); pr_out("%s %"PRIu64, name, val);
} }
@ -1912,7 +1908,7 @@ static void pr_out_bool_value(struct dl *dl, bool value)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_bool(dl->jw, value); print_bool(PRINT_JSON, NULL, NULL, value);
else else
pr_out("%s", value ? "true" : "false"); pr_out("%s", value ? "true" : "false");
} }
@ -1921,7 +1917,7 @@ static void pr_out_uint_value(struct dl *dl, unsigned int value)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_uint(dl->jw, value); print_uint(PRINT_JSON, NULL, NULL, value);
else else
pr_out("%u", value); pr_out("%u", value);
} }
@ -1930,7 +1926,7 @@ static void pr_out_uint64_value(struct dl *dl, uint64_t value)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_u64(dl->jw, value); print_u64(PRINT_JSON, NULL, NULL, value);
else else
pr_out("%"PRIu64, value); pr_out("%"PRIu64, value);
} }
@ -1946,7 +1942,7 @@ static void pr_out_binary_value(struct dl *dl, uint8_t *data, uint32_t len)
while (i < len) { while (i < len) {
if (dl->json_output) if (dl->json_output)
jsonw_printf(dl->jw, "%d", data[i]); print_int(PRINT_JSON, NULL, NULL, data[i]);
else else
pr_out("%02x ", data[i]); pr_out("%02x ", data[i]);
i++; i++;
@ -1961,7 +1957,7 @@ static void pr_out_str_value(struct dl *dl, const char *value)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_string(dl->jw, value); print_string(PRINT_JSON, NULL, NULL, value);
else else
pr_out("%s", value); pr_out("%s", value);
} }
@ -1970,7 +1966,7 @@ static void pr_out_name(struct dl *dl, const char *name)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) if (dl->json_output)
jsonw_name(dl->jw, name); print_string(PRINT_JSON, name, NULL, NULL);
else else
pr_out("%s:", name); pr_out("%s:", name);
} }
@ -1978,17 +1974,15 @@ static void pr_out_name(struct dl *dl, const char *name)
static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr) static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr)
{ {
if (dl->json_output) { if (dl->json_output) {
jsonw_name(dl->jw, "address"); print_uint(PRINT_JSON, "address", NULL, addr);
jsonw_uint(dl->jw, addr); open_json_array(PRINT_JSON, "data");
jsonw_name(dl->jw, "data");
jsonw_start_array(dl->jw);
} }
} }
static void pr_out_region_chunk_end(struct dl *dl) static void pr_out_region_chunk_end(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
} }
static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len, static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len,
@ -2008,7 +2002,7 @@ static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len,
align_val++; align_val++;
if (dl->json_output) if (dl->json_output)
jsonw_printf(dl->jw, "%d", data[i]); print_int(PRINT_JSON, NULL, NULL, data[i]);
else else
pr_out("%02x ", data[i]); pr_out("%02x ", data[i]);
@ -2021,9 +2015,8 @@ static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len,
static void pr_out_section_start(struct dl *dl, const char *name) static void pr_out_section_start(struct dl *dl, const char *name)
{ {
if (dl->json_output) { if (dl->json_output) {
jsonw_start_object(dl->jw); open_json_object(NULL);
jsonw_name(dl->jw, name); open_json_object(name);
jsonw_start_object(dl->jw);
} }
} }
@ -2031,17 +2024,16 @@ static void pr_out_section_end(struct dl *dl)
{ {
if (dl->json_output) { if (dl->json_output) {
if (dl->arr_last.present) if (dl->arr_last.present)
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
jsonw_end_object(dl->jw); close_json_object();
jsonw_end_object(dl->jw); close_json_object();
} }
} }
static void pr_out_array_start(struct dl *dl, const char *name) static void pr_out_array_start(struct dl *dl, const char *name)
{ {
if (dl->json_output) { if (dl->json_output) {
jsonw_name(dl->jw, name); open_json_array(PRINT_JSON, name);
jsonw_start_array(dl->jw);
} else { } else {
__pr_out_indent_inc(); __pr_out_indent_inc();
__pr_out_newline(); __pr_out_newline();
@ -2054,7 +2046,7 @@ static void pr_out_array_start(struct dl *dl, const char *name)
static void pr_out_array_end(struct dl *dl) static void pr_out_array_end(struct dl *dl)
{ {
if (dl->json_output) { if (dl->json_output) {
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
} else { } else {
__pr_out_indent_dec(); __pr_out_indent_dec();
__pr_out_indent_dec(); __pr_out_indent_dec();
@ -2064,8 +2056,7 @@ static void pr_out_array_end(struct dl *dl)
static void pr_out_object_start(struct dl *dl, const char *name) static void pr_out_object_start(struct dl *dl, const char *name)
{ {
if (dl->json_output) { if (dl->json_output) {
jsonw_name(dl->jw, name); open_json_object(name);
jsonw_start_object(dl->jw);
} else { } else {
__pr_out_indent_inc(); __pr_out_indent_inc();
__pr_out_newline(); __pr_out_newline();
@ -2078,7 +2069,7 @@ static void pr_out_object_start(struct dl *dl, const char *name)
static void pr_out_object_end(struct dl *dl) static void pr_out_object_end(struct dl *dl)
{ {
if (dl->json_output) { if (dl->json_output) {
jsonw_end_object(dl->jw); close_json_object();
} else { } else {
__pr_out_indent_dec(); __pr_out_indent_dec();
__pr_out_indent_dec(); __pr_out_indent_dec();
@ -2088,13 +2079,13 @@ static void pr_out_object_end(struct dl *dl)
static void pr_out_entry_start(struct dl *dl) static void pr_out_entry_start(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_start_object(dl->jw); open_json_object(NULL);
} }
static void pr_out_entry_end(struct dl *dl) static void pr_out_entry_end(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_object(dl->jw); close_json_object();
else else
__pr_out_newline(); __pr_out_newline();
} }
@ -3887,20 +3878,18 @@ static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label,
struct occ_item *occ_item; struct occ_item *occ_item;
char buf[32]; char buf[32];
jsonw_name(dl->jw, label); open_json_object(label);
jsonw_start_object(dl->jw);
list_for_each_entry(occ_item, list, list) { list_for_each_entry(occ_item, list, list) {
sprintf(buf, "%u", occ_item->index); sprintf(buf, "%u", occ_item->index);
jsonw_name(dl->jw, buf); open_json_object(buf);
jsonw_start_object(dl->jw);
if (bound_pool) if (bound_pool)
jsonw_uint_field(dl->jw, "bound_pool", print_uint(PRINT_JSON, "bound_pool", NULL,
occ_item->bound_pool_index); occ_item->bound_pool_index);
jsonw_uint_field(dl->jw, "current", occ_item->cur); print_uint(PRINT_JSON, "current", NULL, occ_item->cur);
jsonw_uint_field(dl->jw, "max", occ_item->max); print_uint(PRINT_JSON, "max", NULL, occ_item->max);
jsonw_end_object(dl->jw); close_json_object();
} }
jsonw_end_object(dl->jw); close_json_object();
} }
static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port) static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port)
@ -6140,18 +6129,16 @@ static void pr_out_region_handle_start(struct dl *dl, struct nlattr **tb)
char buf[256]; char buf[256];
sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name); sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name);
if (dl->json_output) { if (dl->json_output)
jsonw_name(dl->jw, buf); open_json_object(buf);
jsonw_start_object(dl->jw); else
} else {
pr_out("%s:", buf); pr_out("%s:", buf);
}
} }
static void pr_out_region_handle_end(struct dl *dl) static void pr_out_region_handle_end(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_object(dl->jw); close_json_object();
else else
pr_out("\n"); pr_out("\n");
} }
@ -6159,18 +6146,16 @@ static void pr_out_region_handle_end(struct dl *dl)
static void pr_out_region_snapshots_start(struct dl *dl, bool array) static void pr_out_region_snapshots_start(struct dl *dl, bool array)
{ {
__pr_out_indent_newline(dl); __pr_out_indent_newline(dl);
if (dl->json_output) { if (dl->json_output)
jsonw_name(dl->jw, "snapshot"); open_json_array(PRINT_JSON, "snapshot");
jsonw_start_array(dl->jw); else
} else {
pr_out("snapshot %s", array ? "[" : ""); pr_out("snapshot %s", array ? "[" : "");
}
} }
static void pr_out_region_snapshots_end(struct dl *dl, bool array) static void pr_out_region_snapshots_end(struct dl *dl, bool array)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
else if (array) else if (array)
pr_out("]"); pr_out("]");
} }
@ -6185,7 +6170,7 @@ static void pr_out_region_snapshots_id(struct dl *dl, struct nlattr **tb, int in
snapshot_id = mnl_attr_get_u32(tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); snapshot_id = mnl_attr_get_u32(tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
if (dl->json_output) if (dl->json_output)
jsonw_uint(dl->jw, snapshot_id); print_uint(PRINT_JSON, NULL, NULL, snapshot_id);
else else
pr_out("%s%u", index ? " " : "", snapshot_id); pr_out("%s%u", index ? " " : "", snapshot_id);
} }
@ -6527,7 +6512,7 @@ static void pr_out_fmsg_start_object(struct dl *dl, char **name)
{ {
if (dl->json_output) { if (dl->json_output) {
pr_out_fmsg_name(dl, name); pr_out_fmsg_name(dl, name);
jsonw_start_object(dl->jw); open_json_object(NULL);
} else { } else {
pr_out_fmsg_group_start(dl, name); pr_out_fmsg_group_start(dl, name);
} }
@ -6536,7 +6521,7 @@ static void pr_out_fmsg_start_object(struct dl *dl, char **name)
static void pr_out_fmsg_end_object(struct dl *dl) static void pr_out_fmsg_end_object(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_object(dl->jw); close_json_object();
else else
pr_out_fmsg_group_end(dl); pr_out_fmsg_group_end(dl);
} }
@ -6545,7 +6530,7 @@ static void pr_out_fmsg_start_array(struct dl *dl, char **name)
{ {
if (dl->json_output) { if (dl->json_output) {
pr_out_fmsg_name(dl, name); pr_out_fmsg_name(dl, name);
jsonw_start_array(dl->jw); open_json_array(PRINT_JSON, NULL);
} else { } else {
pr_out_fmsg_group_start(dl, name); pr_out_fmsg_group_start(dl, name);
} }
@ -6554,7 +6539,7 @@ static void pr_out_fmsg_start_array(struct dl *dl, char **name)
static void pr_out_fmsg_end_array(struct dl *dl) static void pr_out_fmsg_end_array(struct dl *dl)
{ {
if (dl->json_output) if (dl->json_output)
jsonw_end_array(dl->jw); close_json_array(PRINT_JSON, NULL);
else else
pr_out_fmsg_group_end(dl); pr_out_fmsg_group_end(dl);
} }
@ -7206,18 +7191,9 @@ static int dl_init(struct dl *dl)
pr_err("Failed to create index map\n"); pr_err("Failed to create index map\n");
goto err_ifname_map_create; goto err_ifname_map_create;
} }
if (dl->json_output) { new_json_obj_plain(dl->json_output);
dl->jw = jsonw_new(stdout);
if (!dl->jw) {
pr_err("Failed to create JSON writer\n");
goto err_json_new;
}
jsonw_pretty(dl->jw, dl->pretty_output);
}
return 0; return 0;
err_json_new:
ifname_map_fini(dl);
err_ifname_map_create: err_ifname_map_create:
mnlg_socket_close(dl->nlg); mnlg_socket_close(dl->nlg);
return err; return err;
@ -7225,8 +7201,7 @@ err_ifname_map_create:
static void dl_fini(struct dl *dl) static void dl_fini(struct dl *dl)
{ {
if (dl->json_output) delete_json_obj_plain();
jsonw_destroy(&dl->jw);
ifname_map_fini(dl); ifname_map_fini(dl);
mnlg_socket_close(dl->nlg); mnlg_socket_close(dl->nlg);
} }
@ -7333,7 +7308,7 @@ int main(int argc, char **argv)
dl->json_output = true; dl->json_output = true;
break; break;
case 'p': case 'p':
dl->pretty_output = true; pretty = true;
break; break;
case 'v': case 'v':
dl->verbose = true; dl->verbose = true;