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.pull/110/head
parent
a7f6c50d6d
commit
4691a35b7b
|
@ -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, ...);
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <threads.h>
|
||||
|
||||
#ifdef HAVE_LIBAVUTIL
|
||||
#include <libavutil/avutil.h>
|
||||
|
@ -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, ...)
|
||||
|
|
Loading…
Reference in New Issue