diff --git a/src/core/plugin.c b/src/core/plugin.c
index f771d70936e5eda9fbbc2f2fbfa7c735b3e0605a..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 "core/error.h"
#include "core/time.h"
#include "utils/llist.h"
+#include "utils/strbuf.h"
#include <assert.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 *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;
static sdb_llist_t *shutdown_list = NULL;
static sdb_llist_t *log_list = NULL;
? -1 : 0;
} /* sdb_plugin_cmp_next_update */
? -1 : 0;
} /* sdb_plugin_cmp_next_update */
-static sdb_plugin_cb_t *
-sdb_plugin_find_by_name(sdb_llist_t *list, const char *name)
-{
- sdb_object_t tmp = SDB_OBJECT_INIT;
-
- sdb_object_t *obj;
- assert(name);
-
- if (! list)
- return NULL;
-
- tmp.name = strdup(name);
- if (! tmp.name)
- return NULL;
- obj = sdb_llist_search(list, &tmp, sdb_object_cmp_by_name);
- free(tmp.name);
- if (! obj)
- return NULL;
- return SDB_PLUGIN_CB(obj);
-} /* sdb_plugin_find_by_name */
-
/*
* private types
*/
/*
* private types
*/
assert(type);
assert(obj);
assert(type);
assert(obj);
- if (sdb_plugin_find_by_name(*list, obj->name)) {
+ if (sdb_llist_search_by_name(*list, obj->name)) {
sdb_log(SDB_LOG_WARNING, "plugin: %s callback '%s' "
"has already been registered. Ignoring newly "
"registered version.", type, obj->name);
sdb_log(SDB_LOG_WARNING, "plugin: %s callback '%s' "
"has already been registered. Ignoring newly "
"registered version.", type, obj->name);
sizeof(sdb_plugin_cb_t),
sdb_plugin_cb_init,
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,
};
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
};
static int
user_data);
} /* sdb_plugin_register_log */
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)
int
sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
const sdb_time_t *interval, sdb_object_t *user_data)
if ((! name) || (! ci))
return -1;
if ((! name) || (! ci))
return -1;
- plugin = sdb_plugin_find_by_name(config_list, name);
+ plugin = SDB_PLUGIN_CB(sdb_llist_search_by_name(config_list, name));
if (! plugin) {
/* XXX: check if any such plugin has been loaded */
sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
if (! plugin) {
/* XXX: check if any such plugin has been loaded */
sdb_log(SDB_LOG_ERR, "plugin: Plugin '%s' did not register "
return 0;
} /* sdb_plugin_read_loop */
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;
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);
if (! log_list)
return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
return ret;
} /* sdb_plugin_log */
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 : */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */