From 4691a35b7b0cd158b2f4a15fa1f005df4957e615 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Tue, 26 Dec 2023 11:58:35 +0000 Subject: [PATCH] logging: Add method to set thread local log function This allows the user to override the log function in the current thread without receiving log messages from concurrent tasks. --- include/neatvnc.h | 1 + src/logging.c | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/neatvnc.h b/include/neatvnc.h index 06034ee..535baea 100644 --- a/include/neatvnc.h +++ b/include/neatvnc.h @@ -238,5 +238,6 @@ void nvnc_set_cursor(struct nvnc*, struct nvnc_fb*, uint16_t width, void nvnc_default_logger(const struct nvnc_log_data* meta, const char* message); void nvnc_set_log_fn(nvnc_log_fn); +void nvnc_set_log_fn_thread_local(nvnc_log_fn fn); void nvnc_set_log_level(enum nvnc_log_level); void nvnc__log(const struct nvnc_log_data*, const char* fmt, ...); diff --git a/src/logging.c b/src/logging.c index 1dd651f..d43cb95 100644 --- a/src/logging.c +++ b/src/logging.c @@ -25,6 +25,7 @@ #include #include #include +#include #ifdef HAVE_LIBAVUTIL #include @@ -33,6 +34,7 @@ #define EXPORT __attribute__((visibility("default"))) static nvnc_log_fn log_fn = nvnc_default_logger; +static thread_local nvnc_log_fn thread_local_log_fn = NULL; #ifndef NDEBUG static enum nvnc_log_level log_level = NVNC_LOG_DEBUG; @@ -42,6 +44,11 @@ static enum nvnc_log_level log_level = NVNC_LOG_WARNING; static bool is_initialised = false; +static nvnc_log_fn get_log_fn(void) +{ + return thread_local_log_fn ? thread_local_log_fn : log_fn; +} + static char* trim_left(char* str) { while (isspace(*str)) @@ -97,7 +104,7 @@ static void nvnc__vlog(const struct nvnc_log_data* meta, const char* fmt, if (meta->level <= log_level) { vsnprintf(message, sizeof(message), fmt, args); - log_fn(meta, trim(message)); + get_log_fn()(meta, trim(message)); } if (meta->level == NVNC_LOG_PANIC) @@ -179,6 +186,12 @@ void nvnc_set_log_fn(nvnc_log_fn fn) log_fn = fn; } +EXPORT +void nvnc_set_log_fn_thread_local(nvnc_log_fn fn) +{ + thread_local_log_fn = fn; +} + EXPORT void nvnc__log(const struct nvnc_log_data* meta, const char* fmt, ...)