Code

plugin: Added support for log callbacks.
authorSebastian Harl <sh@tokkee.org>
Wed, 10 Jul 2013 14:15:41 +0000 (16:15 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 10 Jul 2013 14:15:41 +0000 (16:15 +0200)
Use this logging mechanism to log messages from the error module.

src/core/error.c
src/core/plugin.c
src/include/core/plugin.h

index cdd5d04afcdd98320d172691ac1d8cdbd451c756..7d14a2526aab596343563b72303484ae4394bfd1 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 #include "core/error.h"
+#include "core/plugin.h"
 #include "utils/strbuf.h"
 
 #include <pthread.h>
@@ -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 */
index 53bba9e74bfc6bcb95522b36ab5c478035e733dc..f502582c4b420172c6927e315a4e31177da9a91b 100644 (file)
@@ -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 : */
 
index ac8656ab3b36c8a76a10c1abac91feb96fe15d92..79a171ab16330ed047b2aef2afc043e8217f4d98 100644 (file)
@@ -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