From 7fcbc35e719dbe1aaf4f033ce87c9b8feec3b237 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 22 May 2024 15:30:31 +0000 Subject: [PATCH] libloc: Allow passing a pointer to the log callback This is basically re-implemnting the logic to pass log settings as the previous functions were inappropriately named. Signed-off-by: Michael Tremer --- src/libloc.c | 34 ++++++++++++++++++++++------------ src/libloc.sym | 1 + src/libloc/libloc.h | 12 ++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/libloc.c b/src/libloc.c index 450c5e6..5d1b87e 100644 --- a/src/libloc.c +++ b/src/libloc.c @@ -30,10 +30,15 @@ struct loc_ctx { int refcount; - void (*log_fn)(struct loc_ctx* ctx, - int priority, const char *file, int line, const char *fn, - const char *format, va_list args); - int log_priority; + + // Logging + struct loc_ctx_logging { + int priority; + + // Callback + loc_log_callback callback; + void* data; + } log; }; void loc_log(struct loc_ctx* ctx, @@ -42,11 +47,11 @@ void loc_log(struct loc_ctx* ctx, va_list args; va_start(args, format); - ctx->log_fn(ctx, priority, file, line, fn, format, args); + ctx->log.callback(ctx, ctx->log.data, priority, file, line, fn, format, args); va_end(args); } -static void log_stderr(struct loc_ctx* ctx, +static void log_stderr(struct loc_ctx* ctx, void* data, int priority, const char* file, int line, const char* fn, const char* format, va_list args) { fprintf(stderr, "libloc: %s: ", fn); @@ -79,15 +84,15 @@ LOC_EXPORT int loc_new(struct loc_ctx** ctx) { return 1; c->refcount = 1; - c->log_fn = log_stderr; - c->log_priority = LOG_ERR; + c->log.callback = log_stderr; + c->log.priority = LOG_ERR; const char* env = secure_getenv("LOC_LOG"); if (env) loc_set_log_priority(c, log_priority(env)); INFO(c, "ctx %p created\n", c); - DEBUG(c, "log_priority=%d\n", c->log_priority); + DEBUG(c, "log_priority=%d\n", c->log.priority); *ctx = c; return 0; @@ -112,17 +117,22 @@ LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) { return NULL; } +LOC_EXPORT void loc_set_log_callback(struct loc_ctx* ctx, loc_log_callback callback, void* data) { + ctx->log.callback = callback; + ctx->log.data = data; +} + LOC_EXPORT void loc_set_log_fn(struct loc_ctx* ctx, void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file, int line, const char* fn, const char* format, va_list args)) { - ctx->log_fn = log_fn; + //ctx->log_fn = log_fn; INFO(ctx, "custom logging function %p registered\n", log_fn); } LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) { - return ctx->log_priority; + return ctx->log.priority; } LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) { - ctx->log_priority = priority; + ctx->log.priority = priority; } diff --git a/src/libloc.sym b/src/libloc.sym index 50734b3..b4bce8d 100644 --- a/src/libloc.sym +++ b/src/libloc.sym @@ -1,6 +1,7 @@ LIBLOC_1 { global: loc_ref; + loc_set_log_callback; loc_get_log_priority; loc_set_log_fn; loc_unref; diff --git a/src/libloc/libloc.h b/src/libloc/libloc.h index 938ed75..ea943a9 100644 --- a/src/libloc/libloc.h +++ b/src/libloc/libloc.h @@ -29,6 +29,18 @@ struct loc_ctx *loc_ref(struct loc_ctx* ctx); struct loc_ctx *loc_unref(struct loc_ctx* ctx); int loc_new(struct loc_ctx** ctx); + +typedef void (*loc_log_callback)( + struct loc_ctx* ctx, + void* data, + int priority, + const char* file, + int line, + const char* fn, + const char* format, + va_list args); +void loc_set_log_callback(struct loc_ctx* ctx, loc_log_callback callback, void* data); + void loc_set_log_fn(struct loc_ctx* ctx, void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file, int line, const char* fn, -- 2.47.3