diff --git a/fabrics.c b/fabrics.c index 86b0e15a10..9eb6a09e96 100644 --- a/fabrics.c +++ b/fabrics.c @@ -46,7 +46,7 @@ #include "nvme-print.h" #include "fabrics.h" #include "util/cleanup.h" -#include "util/logging.h" +#include "logging.h" #include "util/sighdl.h" #define PATH_NVMF_DISC SYSCONFDIR "/nvme/discovery.conf" diff --git a/util/logging.c b/logging.c similarity index 74% rename from util/logging.c rename to logging.c index 7d68b8781a..f09e4ed003 100644 --- a/util/logging.c +++ b/logging.c @@ -16,7 +16,8 @@ #include #include "logging.h" -#include "sighdl.h" +#include "util/sighdl.h" +#include "nvme-print.h" struct submit_data { struct timeval start; @@ -24,7 +25,6 @@ struct submit_data { }; int log_level; -static bool dry_run; static struct submit_data sb; int map_log_level(int verbose, bool quiet) @@ -57,51 +57,46 @@ int map_log_level(int verbose, bool quiet) return log_level; } -void set_dry_run(bool enable) -{ - dry_run = enable; -} - static void nvme_show_common(struct nvme_passthru_cmd *cmd) { - printf("opcode : %02x\n", cmd->opcode); - printf("flags : %02x\n", cmd->flags); - printf("rsvd1 : %04x\n", cmd->rsvd1); - printf("nsid : %08x\n", cmd->nsid); - printf("cdw2 : %08x\n", cmd->cdw2); - printf("cdw3 : %08x\n", cmd->cdw3); - printf("data_len : %08x\n", cmd->data_len); - printf("metadata_len : %08x\n", cmd->metadata_len); - printf("addr : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->addr); - printf("metadata : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->metadata); - printf("cdw10 : %08x\n", cmd->cdw10); - printf("cdw11 : %08x\n", cmd->cdw11); - printf("cdw12 : %08x\n", cmd->cdw12); - printf("cdw13 : %08x\n", cmd->cdw13); - printf("cdw14 : %08x\n", cmd->cdw14); - printf("cdw15 : %08x\n", cmd->cdw15); - printf("timeout_ms : %08x\n", cmd->timeout_ms); + nvme_show_key_value("opcode ", "%02x", cmd->opcode); + nvme_show_key_value("flags ", "%02x", cmd->flags); + nvme_show_key_value("rsvd1 ", "%04x", cmd->rsvd1); + nvme_show_key_value("nsid ", "%08x", cmd->nsid); + nvme_show_key_value("cdw2 ", "%08x", cmd->cdw2); + nvme_show_key_value("cdw3 ", "%08x", cmd->cdw3); + nvme_show_key_value("data_len ", "%08x", cmd->data_len); + nvme_show_key_value("metadata_len ", "%08x", cmd->metadata_len); + nvme_show_key_value("addr ", "%"PRIx64"", (uint64_t)(uintptr_t)cmd->addr); + nvme_show_key_value("metadata ", "%"PRIx64"", (uint64_t)(uintptr_t)cmd->metadata); + nvme_show_key_value("cdw10 ", "%08x", cmd->cdw10); + nvme_show_key_value("cdw11 ", "%08x", cmd->cdw11); + nvme_show_key_value("cdw12 ", "%08x", cmd->cdw12); + nvme_show_key_value("cdw13 ", "%08x", cmd->cdw13); + nvme_show_key_value("cdw14 ", "%08x", cmd->cdw14); + nvme_show_key_value("cdw15 ", "%08x", cmd->cdw15); + nvme_show_key_value("timeout_ms ", "%08x", cmd->timeout_ms); } static void nvme_show_command(struct nvme_passthru_cmd *cmd, int err) { nvme_show_common(cmd); - printf("result : %08x\n", cmd->result); - printf("err : %d\n", err); + nvme_show_key_value("result ", "%08x", cmd->result); + nvme_show_key_value("err ", "%d", err); } static void nvme_show_command64(struct nvme_passthru_cmd64 *cmd, int err) { nvme_show_common((struct nvme_passthru_cmd *)cmd); - printf("result : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->result); - printf("err : %d\n", err); + nvme_show_key_value("result ", "%"PRIx64"", (uint64_t)(uintptr_t)cmd->result); + nvme_show_key_value("err ", "%d", err); } static void nvme_show_latency(struct timeval start, struct timeval end) { - printf("latency : %llu us\n", - (unsigned long long)((end.tv_sec - start.tv_sec) * 1000000 + - (end.tv_usec - start.tv_usec))); + nvme_show_key_value("latency ", "%llu us", + (unsigned long long)((end.tv_sec - start.tv_sec) * 1000000 + + (end.tv_usec - start.tv_usec))); } static void nvme_log_retry(int errnum) @@ -122,7 +117,7 @@ int nvme_submit_passthru(int fd, unsigned long ioctl_cmd, if (log_level >= LOG_DEBUG) gettimeofday(&start, NULL); - if (!dry_run) { + if (!nvme_cfg.dry_run) { retry: err = ioctl(fd, ioctl_cmd, cmd); if (err && (errno == EAGAIN || @@ -155,7 +150,7 @@ int nvme_submit_passthru64(int fd, unsigned long ioctl_cmd, if (log_level >= LOG_DEBUG) gettimeofday(&start, NULL); - if (!dry_run) { + if (!nvme_cfg.dry_run) { retry: err = ioctl(fd, ioctl_cmd, cmd); if (err && (errno == EAGAIN || @@ -197,8 +192,8 @@ static void nvme_show_req_admin(const struct nvme_mi_admin_req_hdr *hdr, size_t }; nvme_show_common(&cmd); - printf("doff : %08x\n", le32_to_cpu(hdr->doff)); - printf("dlen : %08x\n", le32_to_cpu(hdr->dlen)); + nvme_show_key_value("doff ", "%08x", le32_to_cpu(hdr->doff)); + nvme_show_key_value("dlen ", "%08x", le32_to_cpu(hdr->dlen)); } static void nvme_show_req(__u8 type, const struct nvme_mi_msg_hdr *hdr, size_t hdr_len, diff --git a/util/logging.h b/logging.h similarity index 93% rename from util/logging.h rename to logging.h index 5828189079..6899a4f298 100644 --- a/util/logging.h +++ b/logging.h @@ -20,6 +20,5 @@ extern int log_level; int map_log_level(int verbose, bool quiet); -void set_dry_run(bool enable); #endif // DEBUG_H_ diff --git a/meson.build b/meson.build index 47e0c1b5f0..c312d4cb7c 100644 --- a/meson.build +++ b/meson.build @@ -283,6 +283,7 @@ sources = [ 'nvme-wrap.c', 'plugin.c', 'libnvme-wrap.c', + 'logging.c', ] if json_c_dep.found() sources += [ diff --git a/nbft.c b/nbft.c index 5c852454fa..b5c82b07ca 100644 --- a/nbft.c +++ b/nbft.c @@ -14,7 +14,7 @@ #include "nvme-print.h" #include "util/types.h" -#include "util/logging.h" +#include "logging.h" #define NBFT_SYSFS_FILENAME "NBFT*" diff --git a/nvme-print-binary.c b/nvme-print-binary.c index b4c34ad50b..ec86e38624 100644 --- a/nvme-print-binary.c +++ b/nvme-print-binary.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #include "nvme-print.h" -#include "util/logging.h" +#include "logging.h" #include "common.h" static struct print_ops binary_print_ops; diff --git a/nvme-print-json.c b/nvme-print-json.c index 858c6f7327..87d56d6225 100644 --- a/nvme-print-json.c +++ b/nvme-print-json.c @@ -11,7 +11,7 @@ #include "nvme-print.h" #include "util/json.h" -#include "util/logging.h" +#include "logging.h" #include "nvme.h" #include "common.h" @@ -44,6 +44,7 @@ static const uint8_t zero_uuid[16] = { 0 }; static struct print_ops json_print_ops; static struct json_object *json_r; +static int json_init; static void json_feature_show_fields(enum nvme_features_id fid, unsigned int result, unsigned char *buf); @@ -4900,27 +4901,60 @@ static void json_output_perror(const char *msg, va_list ap) json_output_object(r); } +static char *trim_white_space(char *str) +{ + char *end; + + if (!str) + return NULL; + + /* Trim leading space */ + while (isspace((unsigned char)*str)) + str++; + + /* All spaces */ + if (!*str) + return str; + + /* Trim trailing space */ + end = str + strlen(str) - 1; + while (end > str && isspace((unsigned char)*end)) + end--; + + /* Write new null terminator character */ + end[1] = '\0'; + + return str; +} + static void json_output_key_value(const char *key, const char *val, va_list ap) { struct json_object *r = json_r ? json_r : json_create_object(); _cleanup_free_ char *value = NULL; + _cleanup_free_ char *key_trim = trim_white_space(strdup(key)); if (vasprintf(&value, val, ap) < 0) value = NULL; - obj_add_str(r, key, value ? value : "Could not allocate string"); + obj_add_str(r, key_trim ? key_trim : key, value ? value : "Could not allocate string"); obj_print(r); } void json_show_init(void) { - json_r = json_create_object(); + json_init++; + + if (!json_r) + json_r = json_create_object(); } void json_show_finish(void) { + if (--json_init) + return; + if (json_r) json_output_object(json_r); diff --git a/nvme-print-stdout.c b/nvme-print-stdout.c index 4ff3408330..1852beaf0c 100644 --- a/nvme-print-stdout.c +++ b/nvme-print-stdout.c @@ -20,7 +20,7 @@ #include "nvme-models.h" #include "util/suffix.h" #include "util/types.h" -#include "util/logging.h" +#include "logging.h" #include "common.h" static const uint8_t zero_uuid[16] = { 0 }; diff --git a/nvme.c b/nvme.c index 5d2af37b07..2781904c83 100644 --- a/nvme.c +++ b/nvme.c @@ -64,7 +64,7 @@ #include "nvme-wrap.h" #include "util/argconfig.h" #include "util/suffix.h" -#include "util/logging.h" +#include "logging.h" #include "util/sighdl.h" #include "fabrics.h" #define CREATE_CMD @@ -432,6 +432,9 @@ static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags) else ret = open_dev_direct(dev, devname, flags); + if (!ret && log_level >= LOG_DEBUG) + nvme_show_init(); + return ret ? -errno : 0; } @@ -447,8 +450,6 @@ static int parse_args(int argc, char *argv[], const char *desc, log_level = map_log_level(nvme_cfg.verbose, false); nvme_init_default_logging(stderr, log_level, false, false); - set_dry_run(nvme_cfg.dry_run); - return 0; } @@ -515,6 +516,9 @@ bool nvme_is_output_format_json(void) void dev_close(struct nvme_dev *dev) { + if (log_level >= LOG_DEBUG) + nvme_show_finish(); + switch (dev->type) { case NVME_DEV_DIRECT: close(dev_fd(dev)); @@ -7601,7 +7605,7 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p else if (err != 0) nvme_show_status(err); else - printf("NVMe Copy: success\n"); + nvme_show_key_value("NVMe Copy", "success"); return err; } diff --git a/plugins/nbft/nbft-plugin.c b/plugins/nbft/nbft-plugin.c index 01d92229e1..292b516385 100644 --- a/plugins/nbft/nbft-plugin.c +++ b/plugins/nbft/nbft-plugin.c @@ -9,7 +9,7 @@ #include "nvme.h" #include "nbft.h" #include "fabrics.h" -#include "util/logging.h" +#include "logging.h" #define CREATE_CMD #include "nbft-plugin.h" diff --git a/plugins/ocp/ocp-hardware-component-log.c b/plugins/ocp/ocp-hardware-component-log.c index e4a8e065cf..81473e2140 100644 --- a/plugins/ocp/ocp-hardware-component-log.c +++ b/plugins/ocp/ocp-hardware-component-log.c @@ -7,7 +7,7 @@ #include "common.h" #include "util/types.h" -#include "util/logging.h" +#include "logging.h" #include "nvme-print.h" #include "ocp-hardware-component-log.h" #include "ocp-print.h" diff --git a/plugins/ocp/ocp-nvme.c b/plugins/ocp/ocp-nvme.c index e272cdc930..1f776c7f86 100644 --- a/plugins/ocp/ocp-nvme.c +++ b/plugins/ocp/ocp-nvme.c @@ -22,7 +22,7 @@ #include "plugin.h" #include "linux/types.h" #include "util/types.h" -#include "util/logging.h" +#include "logging.h" #include "nvme-print.h" #include "nvme-wrap.h" diff --git a/util/meson.build b/util/meson.build index b76677893a..75aed49c30 100644 --- a/util/meson.build +++ b/util/meson.build @@ -4,7 +4,6 @@ sources += [ 'util/argconfig.c', 'util/base64.c', 'util/crc32.c', - 'util/logging.c', 'util/mem.c', 'util/sighdl.c', 'util/suffix.c',