]> git.ipfire.org Git - zone-sync.git/commitdiff
main: Create a basic logger
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 11 May 2026 09:02:28 +0000 (09:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 11 May 2026 09:02:28 +0000 (09:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
main.c

diff --git a/main.c b/main.c
index 4feebc0c81468440cfb40f440583ebb77d4532d0..443dd2875c962a5126ba0b6293d4f7de84b40ef9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <argp.h>
+#include <stdio.h>
 #include <syslog.h>
 
 #include <isc/mem.h>
@@ -30,6 +31,51 @@ typedef struct ctx {
        isc_mem_t* memctx;
 } ctx_t;
 
+static void logger(ctx_t* ctx, int priority, const char* format, ...) {
+       char buffer[4096];
+       FILE* f = NULL;
+       ssize_t length;
+       va_list args;
+
+       // Don't log if below log level
+       if (priority > ctx->log_level)
+               return;
+
+       // Format the log message
+       va_start(args, format);
+       length = vsnprintf(buffer, sizeof(buffer), format, args);
+       va_end(args);
+
+       // Fail if we could not format the string
+       if (length < 0)
+               return;
+
+       // Select the output stream
+       switch (priority) {
+               case LOG_ERR:
+                       f = stderr;
+                       break;
+
+               default:
+                       f = stdout;
+                       break;
+       }
+
+       // Write to the output stream
+       fwrite(buffer, 1, length, f);
+}
+
+// Logging functions
+#define INFO(ctx, ...) logger(ctx, LOG_INFO, __VA_ARGS__)
+#define ERROR(ctx, ...) logger(ctx, LOG_ERR, __VA_ARGS__)
+#define DEBUG(ctx, ...) logger(ctx, LOG_DEBUG, __VA_ARGS__)
+
+static void run_loop(void* data) {
+       ctx_t* ctx = data;
+
+       DEBUG(ctx, "Event loop started\n");
+}
+
 const char* argp_program_version = PACKAGE_VERSION;
 
 static const char* args_doc = "TODO";