]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
log: give log function its data instead of kmod_ctx.
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Thu, 8 Dec 2011 15:47:55 +0000 (13:47 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Thu, 8 Dec 2011 16:06:50 +0000 (14:06 -0200)
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
libkmod/libkmod.c
libkmod/libkmod.h

index 578edf2e95de3496445e1570a14a27c3cf82f92f..ce81ae0202c37caac66921b4ba13a99640e80ea4 100644 (file)
@@ -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)));
 
index 20418e02bab407f820e23c8e774c9f613cc06c6b..b08c6af2a003e98872582440af3c1ac741a475f0 100644 (file)
 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);
 }
 
index 72f74d03dad607127703f542b94394b9caa96336..11cd9b97e78f593ebfe18c051fecb45762af9607 100644 (file)
@@ -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);