From 9797fd2639d3cac3d98d31018019e69b55e64247 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Tue, 27 Jun 2023 00:06:59 +0900 Subject: [PATCH] log: Add nvme root global variable to set for default output Signed-off-by: Tokunori Ikegami [dwagner: export new function, update docs, reorder free sequence] Signed-off-by: Daniel Wagner --- src/libnvme.map | 5 +++-- src/nvme/log.c | 15 ++++++++++++++- src/nvme/log.h | 13 +++++++++++++ src/nvme/tree.c | 2 ++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/libnvme.map b/src/libnvme.map index ea18c36e..6bb05b0f 100644 --- a/src/libnvme.map +++ b/src/libnvme.map @@ -5,6 +5,7 @@ LIBNVME_1_6 { nvme_ctrl_find; nvme_ctrl_get_src_addr; nvme_ctrl_release_fd; + nvme_get_debug; nvme_get_features_err_recovery2; nvme_get_features_host_mem_buf2; nvme_get_features_iocs_profile; @@ -14,14 +15,14 @@ LIBNVME_1_6 { nvme_host_release_fds; nvme_ns_release_fd; nvme_root_release_fds; + nvme_set_debug; nvme_set_features_iocs_profile; nvme_set_features_resv_mask2; nvme_set_features_resv_persist2; nvme_set_features_write_protect2; + nvme_set_root; nvme_subsystem_get_iopolicy; nvme_subsystem_release_fds; - nvme_set_debug; - nvme_get_debug; }; LIBNVME_1_5 { diff --git a/src/nvme/log.c b/src/nvme/log.c index e4697dff..d4cef19f 100644 --- a/src/nvme/log.c +++ b/src/nvme/log.c @@ -26,11 +26,13 @@ #define LOG_CLOCK CLOCK_MONOTONIC #endif +static nvme_root_t root; + void __attribute__((format(printf, 4, 5))) __nvme_msg(nvme_root_t r, int lvl, const char *func, const char *format, ...) { - FILE *fp = r ? r->fp : stderr; + FILE *fp = stderr; va_list ap; char pidbuf[16]; char timebuf[32]; @@ -48,6 +50,12 @@ __nvme_msg(nvme_root_t r, int lvl, char *message __cleanup__(cleanup_charp) = NULL; int idx = 0; + if (!r) + r = root; + + if (r) + fp = r->fp; + if (r && lvl > r->log_level) return; @@ -90,3 +98,8 @@ void nvme_init_logging(nvme_root_t r, int lvl, bool log_pid, bool log_tstamp) r->log_pid = log_pid; r->log_timestamp = log_tstamp; } + +void nvme_set_root(nvme_root_t r) +{ + root = r; +} diff --git a/src/nvme/log.h b/src/nvme/log.h index 1cf797ae..7c345f6b 100644 --- a/src/nvme/log.h +++ b/src/nvme/log.h @@ -35,4 +35,17 @@ */ void nvme_init_logging(nvme_root_t r, int lvl, bool log_pid, bool log_tstamp); +/** + * nvme_set_root() - Set nvme_root_t context + * @r: nvme_root_t context + * + * In order to be able to log from code paths where no root object is passed in + * via the arguments use the the default one which can be set via this call. + * When creating a new root object with @nvme_create_root the global root object + * will be set as well. This means the global root object is always pointing to + * the latest created root object. Note the first @nvme_free_tree call will reset + * the global root object. + */ +void nvme_set_root(nvme_root_t r); + #endif /* _LOG_H */ diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 07b2eefb..00cf96f7 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -198,6 +198,7 @@ nvme_root_t nvme_create_root(FILE *fp, int log_level) r->fp = fp; list_head_init(&r->hosts); list_head_init(&r->endpoints); + nvme_set_root(r); return r; } @@ -364,6 +365,7 @@ void nvme_free_tree(nvme_root_t r) free(r->config_file); if (r->application) free(r->application); + nvme_set_root(NULL); free(r); }