From 3b0067675c25e67ea535d78388972188996ccfcf Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 28 Sep 2024 11:19:20 +0000 Subject: [PATCH] logging: Implement with a callback so we can change it later Signed-off-by: Michael Tremer --- src/ctx.c | 3 +++ src/ctx.h | 10 ++++++++++ src/logging.c | 21 +++++++++++++++++++-- src/logging.h | 3 +++ src/main.c | 2 ++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/ctx.c b/src/ctx.c index 91b77be..4ddf23f 100644 --- 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) diff --git a/src/ctx.h b/src/ctx.h index ed156f9..475a3d9 100644 --- a/src/ctx.h +++ b/src/ctx.h @@ -22,6 +22,7 @@ #define FIREPERF_CTX_H #include +#include #include "constants.h" #include "stats.h" @@ -30,7 +31,16 @@ // 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; diff --git a/src/logging.c b/src/logging.c index 128ee02..3d37ad7 100644 --- a/src/logging.c +++ b/src/logging.c @@ -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); } diff --git a/src/logging.h b/src/logging.h index f7ee661..af30cee 100644 --- a/src/logging.h +++ b/src/logging.h @@ -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))); diff --git a/src/main.c b/src/main.c index b1019a0..28b0ad9 100644 --- a/src/main.c +++ b/src/main.c @@ -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); -- 2.47.2