diff --git a/src/core/plugin.c b/src/core/plugin.c
index 0a69bff2364a50e2b7566e4f4d0c4aaa6ec5b74c..8ca0fd74472a23b7b7f0ff0daa953ff1329ce0cf 100644 (file)
--- a/src/core/plugin.c
+++ b/src/core/plugin.c
#include "core/error.h"
#include "core/time.h"
#include "utils/llist.h"
+#include "utils/strbuf.h"
#include <assert.h>
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 *cname_list = NULL;
static sdb_llist_t *shutdown_list = NULL;
static sdb_llist_t *log_list = NULL;
sizeof(sdb_plugin_cb_t),
sdb_plugin_cb_init,
- sdb_plugin_cb_destroy,
- /* clone = */ NULL
+ sdb_plugin_cb_destroy
};
static sdb_type_t sdb_plugin_collector_cb_type = {
sizeof(sdb_plugin_collector_cb_t),
sdb_plugin_cb_init,
- sdb_plugin_cb_destroy,
- /* clone = */ NULL
+ sdb_plugin_cb_destroy
};
static int
user_data);
} /* sdb_plugin_register_log */
+int
+sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
+ sdb_object_t *user_data)
+{
+ return sdb_plugin_add_callback(&cname_list, "cname", name, callback,
+ user_data);
+} /* sdb_plugin_register_cname */
+
int
sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
const sdb_time_t *interval, sdb_object_t *user_data)
return 0;
} /* sdb_plugin_read_loop */
+char *
+sdb_plugin_cname(char *hostname)
+{
+ sdb_llist_iter_t *iter;
+
+ if (! hostname)
+ return NULL;
+
+ if (! cname_list)
+ return hostname;
+
+ iter = sdb_llist_get_iter(cname_list);
+ while (sdb_llist_iter_has_next(iter)) {
+ sdb_plugin_cname_cb callback;
+ char *cname;
+
+ sdb_object_t *obj = sdb_llist_iter_get_next(iter);
+ assert(obj);
+
+ callback = SDB_PLUGIN_CB(obj)->cb_callback;
+ cname = callback(hostname, SDB_PLUGIN_CB(obj)->cb_user_data);
+ if (cname) {
+ free(hostname);
+ hostname = cname;
+ }
+ /* else: don't change hostname */
+ }
+ sdb_llist_iter_destroy(iter);
+ return hostname;
+} /* sdb_plugin_cname */
+
int
sdb_plugin_log(int prio, const char *msg)
{
sdb_llist_iter_t *iter;
int ret = -1;
+ if (! msg)
+ return 0;
+
if (! log_list)
return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
return ret;
} /* sdb_plugin_log */
+int
+sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
+{
+ sdb_strbuf_t *buf;
+ int ret;
+
+ if (! fmt)
+ return 0;
+
+ buf = sdb_strbuf_create(64);
+ if (! buf) {
+ ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
+ ret += vfprintf(stderr, fmt, ap);
+ return ret;
+ }
+
+ if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
+ sdb_strbuf_destroy(buf);
+ return -1;
+ }
+
+ ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
+ sdb_strbuf_destroy(buf);
+ return ret;
+} /* sdb_plugin_vlogf */
+
+int
+sdb_plugin_logf(int prio, const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ if (! fmt)
+ return 0;
+
+ va_start(ap, fmt);
+ ret = sdb_plugin_vlogf(prio, fmt, ap);
+ va_end(ap);
+ return ret;
+} /* sdb_plugin_logf */
+
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */