Code

core: Automatically determine config callback names.
[sysdb.git] / src / core / plugin.c
index 8cd490a22a7da2f39e946f4d06be445fda1ebba7..282a5213a45e98cae33ae3c1fde14484972d972a 100644 (file)
@@ -689,9 +689,16 @@ sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
 } /* sdb_plugin_set_info */
 
 int
-sdb_plugin_register_config(const char *name, sdb_plugin_config_cb callback)
+sdb_plugin_register_config(sdb_plugin_config_cb callback)
 {
-       return plugin_add_callback(&config_list, "init", name,
+       ctx_t *ctx = ctx_get();
+
+       if (! ctx) {
+               sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
+                               "config callback from outside a plugin");
+               return -1;
+       }
+       return plugin_add_callback(&config_list, "init", ctx->info.plugin_name,
                        (void *)callback, NULL);
 } /* sdb_plugin_register_config */
 
@@ -829,7 +836,9 @@ sdb_plugin_configure(const char *name, oconfig_item_t *ci)
        if (! plugin) {
                ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
                if (! ctx)
-                       sdb_log(SDB_LOG_ERR, "core: Plugin '%s' not loaded.", name);
+                       sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
+                                       "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
+                                       name, name);
                else
                        sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
                                        "a config callback.", name);
@@ -1113,12 +1122,11 @@ sdb_plugin_log(int prio, const char *msg)
        sdb_llist_iter_t *iter;
        int ret = -1;
 
+       _Bool logged = 0;
+
        if (! msg)
                return 0;
 
-       if (! sdb_llist_len(log_list))
-               return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
-
        iter = sdb_llist_get_iter(log_list);
        while (sdb_llist_iter_has_next(iter)) {
                sdb_plugin_log_cb callback;
@@ -1131,8 +1139,15 @@ sdb_plugin_log(int prio, const char *msg)
                tmp = callback(prio, msg, SDB_PLUGIN_CB(obj)->cb_user_data);
                if (tmp > ret)
                        ret = tmp;
+
+               if (SDB_PLUGIN_CB(obj)->cb_ctx)
+                       logged = 1;
+               /* else: this is an internally registered callback */
        }
        sdb_llist_iter_destroy(iter);
+
+       if (! logged)
+               return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
        return ret;
 } /* sdb_plugin_log */