]> git.ipfire.org Git - location/libloc.git/commitdiff
libloc: Allow passing a pointer to the log callback
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2024 15:30:31 +0000 (15:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2024 15:30:31 +0000 (15:30 +0000)
This is basically re-implemnting the logic to pass log settings as the
previous functions were inappropriately named.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libloc.c
src/libloc.sym
src/libloc/libloc.h

index 450c5e6fb4df341ac61e72f20ee956bc72b01e75..5d1b87e0c7a571847edbad0e382c5fb391b24fb9 100644 (file)
 
 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;
 }
index 50734b3b26bc6dc12b5f4cf7acbff0a490593b18..b4bce8d3baff7d4e175c78caa7115aed6bdee38c 100644 (file)
@@ -1,6 +1,7 @@
 LIBLOC_1 {
 global:
        loc_ref;
+       loc_set_log_callback;
        loc_get_log_priority;
        loc_set_log_fn;
        loc_unref;
index 938ed75e0f1d77e4d28fd29e4c0e75f8e0c6fa2e..ea943a95d4cf2baa99c4d205a283c2e19e6e772f 100644 (file)
@@ -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,