From dc4c86175fb77da7d8d3346400658a94f3bd17a7 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 10 Jul 2013 16:15:41 +0200 Subject: [PATCH] plugin: Added support for log callbacks. Use this logging mechanism to log messages from the error module. --- src/core/error.c | 5 ++--- src/core/plugin.c | 36 ++++++++++++++++++++++++++++++++++++ src/include/core/plugin.h | 24 ++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/core/error.c b/src/core/error.c index cdd5d04..7d14a25 100644 --- a/src/core/error.c +++ b/src/core/error.c @@ -26,6 +26,7 @@ */ #include "core/error.h" +#include "core/plugin.h" #include "utils/strbuf.h" #include @@ -162,9 +163,7 @@ sdb_do_log(int prio) if (ctx->logged) return 0; - ret = fprintf(stderr, "[%s] %s\n", - SDB_LOG_PRIO_TO_STRING(prio), - sdb_strbuf_string(ctx->msg)); + ret = sdb_plugin_log(prio, sdb_strbuf_string(ctx->msg)); ctx->logged = 1; return ret; } /* sdb_do_log */ diff --git a/src/core/plugin.c b/src/core/plugin.c index 53bba9e..f502582 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -100,6 +100,7 @@ static sdb_llist_t *config_list = NULL; static sdb_llist_t *init_list = NULL; static sdb_llist_t *collector_list = NULL; static sdb_llist_t *shutdown_list = NULL; +static sdb_llist_t *log_list = NULL; /* * private helper functions @@ -441,6 +442,14 @@ sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback, callback, user_data); } /* sdb_plugin_register_shutdown */ +int +sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback, + sdb_object_t *user_data) +{ + return sdb_plugin_add_callback(&log_list, "log", name, callback, + user_data); +} /* sdb_plugin_register_log */ + int sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback, const sdb_time_t *interval, sdb_object_t *user_data) @@ -580,6 +589,7 @@ sdb_plugin_init_all(void) } sdb_plugin_set_ctx(old_ctx); } + sdb_llist_iter_destroy(iter); return 0; } /* sdb_plugin_init_all */ @@ -681,5 +691,31 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop) return 0; } /* sdb_plugin_read_loop */ +int +sdb_plugin_log(int prio, const char *msg) +{ + sdb_llist_iter_t *iter; + int ret = -1; + + if (! log_list) + return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg); + + iter = sdb_llist_get_iter(log_list); + while (sdb_llist_iter_has_next(iter)) { + sdb_plugin_log_cb callback; + int tmp; + + sdb_object_t *obj = sdb_llist_iter_get_next(iter); + assert(obj); + + callback = SDB_PLUGIN_CB(obj)->cb_callback; + tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data); + if (tmp > ret) + ret = tmp; + } + sdb_llist_iter_destroy(iter); + return ret; +} /* sdb_plugin_log */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/include/core/plugin.h b/src/include/core/plugin.h index ac8656a..79a171a 100644 --- a/src/include/core/plugin.h +++ b/src/include/core/plugin.h @@ -92,6 +92,8 @@ typedef int (*sdb_plugin_config_cb)(oconfig_item_t *ci); typedef int (*sdb_plugin_init_cb)(sdb_object_t *user_data); typedef int (*sdb_plugin_collector_cb)(sdb_object_t *user_data); typedef int (*sdb_plugin_shutdown_cb)(sdb_object_t *user_data); +typedef int (*sdb_plugin_log_cb)(int prio, const char *msg, + sdb_object_t *user_data); /* * sdb_plugin_register_config: @@ -173,6 +175,20 @@ sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback, sdb_object_t *user_data); +/* + * sdb_plugin_register_log: + * Register a "log" function to be called whenever logging is to be done. + * + * Arguments: + * - user_data: If specified, this will be passed on to each call of the + * callback. The function will take ownership of the object, that is, + * increment the reference count by one. In case the caller does not longer + * use the object for other purposes, it should thus deref it. + */ +int +sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback, + sdb_object_t *user_data); + /* * sdb_plugin_get_ctx, sdb_plugin_set_ctx: * The plugin context defines a set of settings that are available whenever a @@ -216,6 +232,14 @@ sdb_plugin_init_all(void); int sdb_plugin_collector_loop(sdb_plugin_loop_t *loop); +/* + * sdb_plugin_log: + * Log the specified message using all registered log callbacks. The message + * will be logged with the specified priority. + */ +int +sdb_plugin_log(int prio, const char *msg); + #ifdef __cplusplus } /* extern "C" */ #endif -- 2.30.2