]> git.ipfire.org Git - fireperf.git/commitdiff
logging: Implement with a callback so we can change it later
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 11:19:20 +0000 (11:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 11:19:20 +0000 (11:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/ctx.c
src/ctx.h
src/logging.c
src/logging.h
src/main.c

index 91b77be6345b0bc712ccdbdc8e09aef56d4aa049..4ddf23f037153eadabb32da9703969556eb356cc 100644 (file)
--- a/src/ctx.c
+++ b/src/ctx.c
@@ -258,6 +258,9 @@ int fireperf_ctx_create(struct fireperf_ctx** ctx, int argc, char* argv[]) {
        // Fetch how many workers we would launch
        c->max_workers        = sysconf(_SC_NPROCESSORS_ONLN);
 
+       // Set the default log callback
+       fireperf_set_log_callback(c, NULL, NULL);
+
        // Parse the command line
        r = parse_argv(c, argc, argv);
        if (r)
index ed156f9eef1dc0f3a9d63a380490582a20e1abba..475a3d93a79c1580fb23bef0a49b1587cf60f851 100644 (file)
--- a/src/ctx.h
+++ b/src/ctx.h
@@ -22,6 +22,7 @@
 #define FIREPERF_CTX_H
 
 #include <netinet/in.h>
+#include <stdarg.h>
 
 #include "constants.h"
 #include "stats.h"
 // Forward declarations
 struct fireperf_worker;
 
+typedef void (*fireperf_log_callback)(void* data, int priority, const char* file,
+       int line, const char* fn, const char* format, va_list args);
+
 struct fireperf_ctx {
+       // Logging
+       struct {
+               fireperf_log_callback callback;
+               void* data;
+       } log;
+
        // TUI
        struct fireperf_tui* tui;
 
index 128ee02e513cc1065089fde00dc6e2db96a8d699..3d37ad7d8d26b140170036f5441c4fe07d682d31 100644 (file)
@@ -29,8 +29,10 @@ int fireperf_get_log_level(struct fireperf_ctx* ctx) {
        return ctx->loglevel;
 }
 
-static void fireperf_log_console(struct fireperf_ctx* ctx, int priority,
+static void fireperf_log_console(void* data, int priority,
                const char* file, int line, const char* fn, const char* format, va_list args) {
+       struct fireperf_ctx* ctx = data;
+
        switch (priority) {
                // Print error messages to stderr
                case LOG_ERR:
@@ -44,11 +46,26 @@ static void fireperf_log_console(struct fireperf_ctx* ctx, int priority,
        }
 }
 
+void fireperf_set_log_callback(struct fireperf_ctx* ctx,
+               fireperf_log_callback callback, void* data) {
+       if (!callback) {
+               callback = fireperf_log_console;
+               data     = ctx;
+       }
+
+       ctx->log.callback = callback;
+       ctx->log.data     = data;
+}
+
 void fireperf_log(struct fireperf_ctx* ctx, int priority,
                const char* file, int line, const char* fn, const char* format, ...) {
        va_list args;
 
+       // Return immediately if we don't have a callback registered
+       if (!ctx->log.callback)
+               return;
+
        va_start(args, format);
-       fireperf_log_console(ctx, priority, file, line, fn, format, args);
+       ctx->log.callback(ctx->log.data, priority, file, line, fn, format, args);
        va_end(args);
 }
index f7ee66193309ea2ded31bb20e4e23edba4401366..af30cee1952a6a1639728993337c8baec278c0fa 100644 (file)
@@ -46,6 +46,9 @@ static inline void __attribute__((always_inline, format(printf, 2, 3)))
 
 int fireperf_get_log_level(struct fireperf_ctx* ctx);
 
+void fireperf_set_log_callback(struct fireperf_ctx* ctx,
+       fireperf_log_callback callback, void* data);
+
 void fireperf_log(struct fireperf_ctx* ctx,
        int priority, const char* file, int line, const char* fn,
        const char* format, ...) __attribute__((format(printf, 6, 7)));
index b1019a06e4edcd3cf2c2e536b7e1505c7f5eb1f5..28b0ad919451d2fd6f526cb82f512a6b2fb9ff6b 100644 (file)
@@ -172,6 +172,8 @@ int main(int argc, char* argv[]) {
 
        struct epoll_event events[EPOLL_MAX_EVENTS];
 
+       INFO(ctx, "Ready...\n");
+
        // Main loop
        for (;;) {
                ready = epoll_wait(epollfd, events, EPOLL_MAX_EVENTS, -1);