For the json output add json argconfig option also.
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include <assert.h>
+#include <errno.h>
#include "nvme-print-json.h"
printf("\n");
json_free_object(root);
}
+
+static void json_output_object(struct json_object *root)
+{
+ json_print_object(root, NULL);
+ printf("\n");
+ json_free_object(root);
+}
+
+void json_output_status(int status)
+{
+ struct json_object *root = json_create_object();
+ int val;
+ int type;
+
+ if (status < 0) {
+ json_object_add_value_string(root, "error", nvme_strerror(errno));
+ return json_output_object(root);
+ }
+
+ val = nvme_status_get_value(status);
+ type = nvme_status_get_type(status);
+
+ switch (type) {
+ case NVME_STATUS_TYPE_NVME:
+ json_object_add_value_string(root, "error", nvme_status_to_string(val, false));
+ json_object_add_value_string(root, "type", "nvme");
+ break;
+ case NVME_STATUS_TYPE_MI:
+ json_object_add_value_string(root, "error", nvme_mi_status_to_string(val));
+ json_object_add_value_string(root, "type", "nvme-mi");
+ break;
+ default:
+ json_object_add_value_string(root, "type", "unknow");
+ break;
+ }
+
+ json_object_add_value_int(root, "value", val);
+
+ json_output_object(root);
+}
void json_predictable_latency_per_nvmset(
struct nvme_nvmset_predictable_lat_log *plpns_log,
__u16 nvmset_id);
+void json_output_status(int status);
/* fabrics.c */
void json_discovery_log(struct nvmf_discovery_log *log, int numrec);
#define json_persistent_event_log(pevent_log_info, size)
#define json_predictable_latency_event_agg_log(pea_log, log_entries)
#define json_predictable_latency_per_nvmset(plpns_log, nvmset_id)
+#define json_output_status(status)
/* fabrics.c */
#define json_discovery_log(log, numrec)
int val;
int type;
+ if (argconfig_output_format_json(false))
+ return json_output_status(status);
+
/*
* Callers should be checking for negative values first, but provide a
* sensible fallback anyway
return ret;
}
+bool argconfig_output_format_json(bool set)
+{
+ static bool output_format_json = false;
+
+ if (set)
+ output_format_json = true;
+
+ return output_format_json;
+}
+
int argconfig_parse(int argc, char *argv[], const char *program_desc,
struct argconfig_commandline_options *options)
{
for (s = options; s->option; s++)
options_count++;
- long_opts = calloc(1, sizeof(struct option) * (options_count + 2));
- short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 4));
+ long_opts = calloc(1, sizeof(struct option) * (options_count + 3));
+ short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 5));
if (!long_opts || !short_opts) {
fprintf(stderr, "failed to allocate memory for opts: %s\n", strerror(errno));
}
long_opts[option_index].name = "help";
- long_opts[option_index].val = 'h';
+ long_opts[option_index++].val = 'h';
+
+ long_opts[option_index].name = "json";
+ long_opts[option_index].val = 'j';
short_opts[short_index++] = '?';
- short_opts[short_index] = 'h';
+ short_opts[short_index++] = 'h';
+ short_opts[short_index] = 'j';
optind = 0;
while ((c = getopt_long_only(argc, argv, short_opts, long_opts, &option_index)) != -1) {
ret = -EINVAL;
break;
}
+ if (c == 'j')
+ argconfig_output_format_json(true);
for (option_index = 0; option_index < options_count; option_index++) {
if (c == options[option_index].short_option)
break;
void print_word_wrapped(const char *s, int indent, int start, FILE *stream);
bool argconfig_parse_seen(struct argconfig_commandline_options *options,
const char *option);
+bool argconfig_output_format_json(bool set);
#endif