From: Hannes Reinecke Date: Thu, 24 Jun 2021 06:26:46 +0000 (+0200) Subject: log: export last error message X-Git-Tag: v1.0-rc0~121^2~9 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=b8b45653211ec078a55cb2b5b32b9fd809a99a63;p=users%2Fsagi%2Flibnvme.git log: export last error message Add a static string 'nvme_log_message' containing the last error message written out by 'nvme_msg'. Signed-off-by: Hannes Reinecke --- diff --git a/src/nvme/log.c b/src/nvme/log.c index 66cf692b..9a7bcafe 100644 --- a/src/nvme/log.c +++ b/src/nvme/log.c @@ -23,6 +23,7 @@ #include #include #include +#include #define LOG_FUNCNAME 1 #include "log.h" #include "cleanup.h" @@ -34,6 +35,7 @@ int nvme_log_level = DEFAULT_LOGLEVEL; bool nvme_log_timestamp; bool nvme_log_pid; +char *nvme_log_message = NULL; void __attribute__((format(printf, 3, 4))) __nvme_msg(int lvl, const char *func, const char *format, ...) @@ -55,9 +57,6 @@ __nvme_msg(int lvl, const char *func, const char *format, ...) char *message __cleanup__(cleanup_charp) = NULL; int idx; - if (lvl > nvme_log_level) - return; - if (nvme_log_timestamp) { struct timespec now; @@ -84,7 +83,12 @@ __nvme_msg(int lvl, const char *func, const char *format, ...) message = NULL; va_end(ap); - fprintf(stderr, "%s%s", header ? header : "", - message ? message : ""); + if (nvme_log_message) + free(nvme_log_message); + nvme_log_message = strdup(message); + + if (lvl <= nvme_log_level) + fprintf(stderr, "%s%s", header ? header : "", + message ? message : ""); } diff --git a/src/nvme/log.h b/src/nvme/log.h index 36f56e87..4ed695c4 100644 --- a/src/nvme/log.h +++ b/src/nvme/log.h @@ -23,6 +23,7 @@ extern int nvme_log_level; extern bool nvme_log_timestamp; extern bool nvme_log_pid; +extern char *nvme_log_message; void __attribute__((format(printf, 3, 4))) __nvme_msg(int lvl, const char *func, const char *format, ...);