src/daemon/plugin.[ch]: Add the plugin name to the context.
[collectd.git] / src / daemon / plugin.c
index 427a813..72e3591 100644 (file)
@@ -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) {