From 1bdd951ee2fcc906a0394ee0b387e355d2bab850 Mon Sep 17 00:00:00 2001 From: Gustavo Sverzut Barbieri Date: Thu, 8 Dec 2011 13:47:55 -0200 Subject: [PATCH] log: give log function its data instead of kmod_ctx. This will be the most common use case for logging, also changed log_stderr() to log_filep() with data being stderr to test it. --- libkmod/libkmod-private.h | 2 +- libkmod/libkmod.c | 23 ++++++++++++++--------- libkmod/libkmod.h | 5 +++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 578edf2..ce81ae0 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -33,7 +33,7 @@ static __always_inline __printf_format(2, 3) void #define KMOD_EXPORT __attribute__ ((visibility("default"))) -void kmod_log(struct kmod_ctx *ctx, +void kmod_log(const struct kmod_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, ...) __attribute__((format(printf, 6, 7))) __attribute__((nonnull(1, 3, 5))); diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 20418e0..b08c6af 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -52,32 +52,34 @@ struct kmod_ctx { int refcount; int log_priority; - void (*log_fn)(struct kmod_ctx *ctx, + void (*log_fn)(void *data, int priority, const char *file, int line, const char *fn, const char *format, va_list args); + void *log_data; const void *userdata; char *dirname; struct kmod_config *config; struct kmod_hash *modules_by_name; }; -void kmod_log(struct kmod_ctx *ctx, +void kmod_log(const struct kmod_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, ...) { va_list args; va_start(args, format); - ctx->log_fn(ctx, priority, file, line, fn, format, args); + ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); va_end(args); } -static void log_stderr(struct kmod_ctx *ctx, +static void log_filep(void *data, int priority, const char *file, int line, const char *fn, const char *format, va_list args) { - fprintf(stderr, "libkmod: %s: ", fn); - vfprintf(stderr, format, args); + FILE *fp = data; + fprintf(fp, "libkmod: %s: ", fn); + vfprintf(fp, format, args); } const char *kmod_get_dirname(const struct kmod_ctx *ctx) @@ -173,7 +175,8 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname) return NULL; ctx->refcount = 1; - ctx->log_fn = log_stderr; + ctx->log_fn = log_filep; + ctx->log_data = stderr; ctx->log_priority = LOG_ERR; ctx->dirname = get_kernel_release(dirname); @@ -258,12 +261,14 @@ KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) * **/ KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, - void (*log_fn)(struct kmod_ctx *ctx, + void (*log_fn)(void *data, int priority, const char *file, int line, const char *fn, - const char *format, va_list args)) + const char *format, va_list args), + const void *data) { ctx->log_fn = log_fn; + ctx->log_data = (void *)data; INFO(ctx, "custom logging function %p registered\n", log_fn); } diff --git a/libkmod/libkmod.h b/libkmod/libkmod.h index 72f74d0..11cd9b9 100644 --- a/libkmod/libkmod.h +++ b/libkmod/libkmod.h @@ -39,10 +39,11 @@ struct kmod_ctx *kmod_new(const char *dirname); struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx); struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx); void kmod_set_log_fn(struct kmod_ctx *ctx, - void (*log_fn)(struct kmod_ctx *ctx, + void (*log_fn)(void *log_data, int priority, const char *file, int line, const char *fn, const char *format, - va_list args)); + va_list args), + const void *data); int kmod_get_log_priority(const struct kmod_ctx *ctx); void kmod_set_log_priority(struct kmod_ctx *ctx, int priority); void *kmod_get_userdata(const struct kmod_ctx *ctx); -- 2.7.4