]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
log: Add nvme root global variable to set for default output
authorTokunori Ikegami <ikegami.t@gmail.com>
Mon, 26 Jun 2023 15:06:59 +0000 (00:06 +0900)
committerDaniel Wagner <wagi@monom.org>
Wed, 20 Sep 2023 10:03:40 +0000 (12:03 +0200)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
[dwagner: export new function, update docs, reorder free sequence]
Signed-off-by: Daniel Wagner <dwagner@suse.de>
src/libnvme.map
src/nvme/log.c
src/nvme/log.h
src/nvme/tree.c

index ea18c36efb44bbce9f180efddb375881b78167a5..6bb05b0f838c67a87db1bb0c7623e3de5db8a7d4 100644 (file)
@@ -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 {
index e4697dff7f5ee7a0f57c601ae45ea866da8ebed0..d4cef19fb0671c15aa75df552f07fe3a8f3934ff 100644 (file)
 #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;
+}
index 1cf797ae37b650159c8b6b15e58dccb53bc24b9f..7c345f6bd3937bb2df2786bf20885a8fe6061933 100644 (file)
  */
 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 */
index 07b2eefba75d394451baad166ff0a9aa52f2161a..00cf96f7b4582edc3be40787d5a5d3ab7f89f158 100644 (file)
@@ -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);
 }