json_output_object(r);
}
+static void json_output_error_status(int status, const char *msg, va_list ap)
+{
+ struct json_object *r = json_create_object();
+ char *value;
+ const char *key = "Error";
+ int val;
+ int type;
+
+ if (vasprintf(&value, msg, ap) < 0)
+ value = NULL;
+
+ if (value)
+ obj_add_str(r, key, value);
+ else
+ obj_add_str(r, key, "Could not allocate string");
+
+ free(value);
+
+ if (status < 0) {
+ obj_add_str(r, "Error", nvme_strerror(errno));
+ return json_output_object(r);
+ }
+
+ val = nvme_status_get_value(status);
+ type = nvme_status_get_type(status);
+
+ switch (type) {
+ case NVME_STATUS_TYPE_NVME:
+ obj_add_str(r, "Status", nvme_status_to_string(val, false));
+ obj_add_str(r, "Type", "nvme");
+ break;
+ case NVME_STATUS_TYPE_MI:
+ obj_add_str(r, "Status", nvme_mi_status_to_string(val));
+ obj_add_str(r, "Type", "nvme-mi");
+ break;
+ default:
+ obj_add_str(r, "Type", "Unknown");
+ break;
+ }
+
+ obj_add_int(r, "Value", val);
+
+ json_output_object(r);
+}
+
static void json_output_message(bool error, const char *msg, va_list ap)
{
struct json_object *r = json_create_object();
.show_message = json_output_message,
.show_perror = json_output_perror,
.show_status = json_output_status,
+ .show_error_status = json_output_error_status,
};
struct print_ops *nvme_get_json_print_ops(enum nvme_print_flags flags)
}
}
+static void stdout_error_status(int status, const char *msg, va_list ap)
+{
+ vfprintf(stderr, msg, ap);
+
+ stdout_status(status);
+}
+
static void stdout_id_ctrl_cmic(__u8 cmic)
{
__u8 rsvd = (cmic & 0xF0) >> 4;
{
vfprintf(error ? stderr : stdout, msg, ap);
- printf("\n");
+ fprintf(error ? stderr : stdout, "\n");
}
static void stdout_perror(const char *msg)
.show_message = stdout_message,
.show_perror = stdout_perror,
.show_status = stdout_status,
+ .show_error_status = stdout_error_status,
};
struct print_ops *nvme_get_stdout_print_ops(enum nvme_print_flags flags)
void nvme_show_status(int status)
{
- struct print_ops *ops;
+ struct print_ops *ops = nvme_print_ops(0);
if (nvme_is_output_format_json())
ops = nvme_print_ops(JSON);
- else
- ops =nvme_print_ops(0);
- if (!ops)
- return;
-
- if (!ops->show_status)
+ if (!ops || !ops->show_status)
return;
ops->show_status(status);
}
+void nvme_show_error_status(int status, const char *msg, ...)
+{
+ struct print_ops *ops = nvme_print_ops(0);
+ va_list ap;
+
+ va_start(ap, msg);
+
+ if (nvme_is_output_format_json())
+ ops = nvme_print_ops(JSON);
+
+ if (ops && ops->show_status)
+ ops->show_error_status(status, msg, ap);
+
+ va_end(ap);
+}
+
void nvme_show_id_ctrl_rpmbs(__le32 ctrl_rpmbs, enum nvme_print_flags flags)
{
nvme_print(id_ctrl_rpmbs, flags, ctrl_rpmbs);
void nvme_show_message(bool error, const char *msg, ...)
{
- struct print_ops *ops;
+ struct print_ops *ops = nvme_print_ops(0);
va_list ap;
+
va_start(ap, msg);
if (nvme_is_output_format_json())
ops = nvme_print_ops(JSON);
- else
- ops = nvme_print_ops(0);
-
- if (!ops)
- return;
- if (!ops->show_message)
- return;
- ops->show_message(error, msg, ap);
+ if (ops && ops->show_message)
+ ops->show_message(error, msg, ap);
va_end(ap);
}
void (*show_message)(bool error, const char *msg, va_list ap);
void (*show_perror)(const char *msg);
void (*show_status)(int status);
+ void (*show_error_status)(int status, const char *msg, va_list ap);
enum nvme_print_flags flags;
};
void nvme_generic_full_path(nvme_ns_t n, char *path, size_t len);
void nvme_show_message(bool error, const char *msg, ...);
void nvme_show_perror(const char *msg);
+void nvme_show_error_status(int status, const char *msg, ...);
#endif
continue;
if (!nvme_status_equals(status, type, NVME_SC_INVALID_NS))
break;
- nvme_show_error("get-feature:%#0*x (%s): ", cfg.feature_id ? 4 : 2, cfg.feature_id,
- nvme_feature_to_string(cfg.feature_id));
- nvme_show_status(err);
+ nvme_show_error_status(err, "get-feature:%#0*x (%s): ", cfg.feature_id ? 4 : 2,
+ cfg.feature_id, nvme_feature_to_string(cfg.feature_id));
}
- if (feat_num == 1 &&
- nvme_status_equals(status, type, NVME_SC_INVALID_FIELD))
+ if (feat_num == 1 && nvme_status_equals(status, type, NVME_SC_INVALID_FIELD))
nvme_show_status(err);
return err;