X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fplugin.c;h=72e3591cbae51859db8c4cde71132fa90512fae5;hb=ce79ade493b71b2c6a3dbc18b07d791739ad0cc2;hp=734e8a0a2d381472e03616c3107574fe3588730e;hpb=6e41c3b1f024d7944e5e8010a87933555c662474;p=collectd.git diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 734e8a0a..72e3591c 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -94,7 +94,7 @@ typedef struct flush_callback_s flush_callback_t; /* * Private variables */ -static c_avl_tree_t *plugins_loaded = NULL; +static c_avl_tree_t *plugins_loaded; static llist_t *list_init; static llist_t *list_write; @@ -104,41 +104,41 @@ static llist_t *list_shutdown; static llist_t *list_log; static llist_t *list_notification; -static fc_chain_t *pre_cache_chain = NULL; -static fc_chain_t *post_cache_chain = NULL; +static fc_chain_t *pre_cache_chain; +static fc_chain_t *post_cache_chain; static c_avl_tree_t *data_sets; -static char *plugindir = NULL; +static char *plugindir; #ifndef DEFAULT_MAX_READ_INTERVAL #define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T_STATIC(86400) #endif -static c_heap_t *read_heap = NULL; +static c_heap_t *read_heap; static llist_t *read_list; static int read_loop = 1; static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER; -static pthread_t *read_threads = NULL; -static size_t read_threads_num = 0; +static pthread_t *read_threads; +static size_t read_threads_num; static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL; static write_queue_t *write_queue_head; static write_queue_t *write_queue_tail; -static long write_queue_length = 0; +static long write_queue_length; static bool write_loop = true; static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER; -static pthread_t *write_threads = NULL; -static size_t write_threads_num = 0; +static pthread_t *write_threads; +static size_t write_threads_num; static pthread_key_t plugin_ctx_key; static bool plugin_ctx_key_initialized; -static long write_limit_high = 0; -static long write_limit_low = 0; +static long write_limit_high; +static long write_limit_low; -static derive_t stats_values_dropped = 0; +static derive_t stats_values_dropped; static bool record_statistics; /* @@ -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; } @@ -2025,7 +2033,7 @@ static double get_drop_probability(void) /* {{{ */ static bool check_drop_value(void) /* {{{ */ { - static cdtime_t last_message_time = 0; + static cdtime_t last_message_time; static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER; double p; @@ -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) {