Skip to content

Commit

Permalink
log: Add nvme root global variable to set for default output
Browse files Browse the repository at this point in the history
Signed-off-by: Tokunori Ikegami <[email protected]>
[dwagner: export new function, update docs, reorder free sequence]
Signed-off-by: Daniel Wagner <[email protected]>
  • Loading branch information
ikegami-t authored and igaw committed Sep 20, 2023
1 parent 6de7efe commit 9797fd2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/libnvme.map
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion src/nvme/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
13 changes: 13 additions & 0 deletions src/nvme/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 2 additions & 0 deletions src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 9797fd2

Please sign in to comment.