From ce79ade493b71b2c6a3dbc18b07d791739ad0cc2 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 4 Oct 2016 20:48:17 +0200 Subject: [PATCH] src/daemon/plugin.[ch]: Add the plugin name to the context. Issue: #1949 --- src/daemon/configfile.c | 20 +++++++++----------- src/daemon/plugin.c | 32 ++++++++++++++++++++++---------- src/daemon/plugin.h | 1 + 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index f88d234f..d647ead4 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -251,11 +251,7 @@ static int dispatch_value_plugindir(oconfig_item_t *ci) { } static int dispatch_loadplugin(oconfig_item_t *ci) { - const char *name; bool global = false; - plugin_ctx_t ctx = {0}; - plugin_ctx_t old_ctx; - int ret_val; assert(strcasecmp(ci->key, "LoadPlugin") == 0); @@ -265,14 +261,16 @@ static int dispatch_loadplugin(oconfig_item_t *ci) { return -1; } - name = ci->values[0].value.string; + const char *name = ci->values[0].value.string; if (strcmp("libvirt", name) == 0) name = "virt"; /* default to the global interval set before loading this plugin */ - ctx.interval = cf_get_default_interval(); - ctx.flush_interval = 0; - ctx.flush_timeout = 0; + plugin_ctx_t ctx = { + .interval = cf_get_default_interval(), .name = strdup(name), + }; + if (ctx.name == NULL) + return ENOMEM; for (int i = 0; i < ci->children_num; ++i) { oconfig_item_t *child = ci->children + i; @@ -288,12 +286,12 @@ static int dispatch_loadplugin(oconfig_item_t *ci) { else { WARNING("Ignoring unknown LoadPlugin option \"%s\" " "for plugin \"%s\"", - child->key, ci->values[0].value.string); + child->key, name); } } - old_ctx = plugin_set_ctx(ctx); - ret_val = plugin_load(name, global); + plugin_ctx_t old_ctx = plugin_set_ctx(ctx); + int ret_val = plugin_load(name, global); /* reset to the "global" context */ plugin_set_ctx(old_ctx); diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 427a8134..72e3591c 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -346,19 +346,22 @@ static void log_list_callbacks(llist_t **list, /* {{{ */ static int create_register_callback(llist_t **list, /* {{{ */ const char *name, void *callback, user_data_t const *ud) { - callback_func_t *cf; - cf = calloc(1, sizeof(*cf)); + if ((name == NULL) || (callback == NULL)) + return EINVAL; + + callback_func_t *cf = calloc(1, sizeof(*cf)); if (cf == NULL) { free_userdata(ud); ERROR("plugin: create_register_callback: calloc failed."); - return -1; + return ENOMEM; } cf->cf_callback = callback; if (ud == NULL) { - cf->cf_udata.data = NULL; - cf->cf_udata.free_func = NULL; + cf->cf_udata = (user_data_t){ + .data = NULL, .free_func = NULL, + }; } else { cf->cf_udata = *ud; } @@ -1714,8 +1717,12 @@ int plugin_write(const char *plugin, /* {{{ */ callback_func_t *cf = le->value; plugin_write_cb callback; - /* do not switch plugin context; rather keep the context (interval) - * information of the calling read plugin */ + /* Keep the read plugin's interval and flush information but update the + * plugin name. */ + plugin_ctx_t old_ctx = plugin_get_ctx(); + plugin_ctx_t ctx = old_ctx; + ctx.name = cf->cf_ctx.name; + plugin_set_ctx(ctx); DEBUG("plugin: plugin_write: Writing values via %s.", le->key); callback = cf->cf_callback; @@ -1725,6 +1732,7 @@ int plugin_write(const char *plugin, /* {{{ */ else success++; + plugin_set_ctx(old_ctx); le = le->next; } @@ -2200,7 +2208,7 @@ int plugin_dispatch_notification(const notification_t *notif) { } /* int plugin_dispatch_notification */ void plugin_log(int level, const char *format, ...) { - char msg[1024]; + char msg[1024] = ""; va_list ap; llentry_t *le; @@ -2209,9 +2217,13 @@ void plugin_log(int level, const char *format, ...) { return; #endif + char const *name = plugin_get_ctx().name; + if (name != NULL) + snprintf(msg, sizeof(msg), "%s plugin: ", name); + va_start(ap, format); - vsnprintf(msg, sizeof(msg), format, ap); - msg[sizeof(msg) - 1] = '\0'; + vsnprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), format, ap); + msg[sizeof(msg) - 1] = 0; va_end(ap); if (list_log == NULL) { diff --git a/src/daemon/plugin.h b/src/daemon/plugin.h index 03690678..024e5384 100644 --- a/src/daemon/plugin.h +++ b/src/daemon/plugin.h @@ -171,6 +171,7 @@ struct user_data_s { typedef struct user_data_s user_data_t; struct plugin_ctx_s { + char *name; cdtime_t interval; cdtime_t flush_interval; cdtime_t flush_timeout; -- 2.11.0