From: Michael Tremer Date: Mon, 11 May 2026 09:02:28 +0000 (+0000) Subject: main: Create a basic logger X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99cc33a8c99c45cc60f1933d48e818f84f0fc003;p=zone-sync.git main: Create a basic logger Signed-off-by: Michael Tremer --- diff --git a/main.c b/main.c index 4feebc0..443dd28 100644 --- a/main.c +++ b/main.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -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";