Code

Use the sdb_error* function throughout the project.
[sysdb.git] / src / backend / puppet-storeconfigs.c
index f03380871a1de9dc38c733cea763f592f7624c2f..4c92c151770c53a34b44b147df793658c3343752 100644 (file)
@@ -28,6 +28,7 @@
 #include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
+#include "utils/error.h"
 #include "utils/dbi.h"
 #include "utils/string.h"
 
@@ -66,15 +67,15 @@ sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
        status = sdb_store_host(&host);
 
        if (status < 0) {
-               fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
-                               "host '%s'.\n", host.host_name);
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "store/update host '%s'.\n", host.host_name);
                free(host.host_name);
                return -1;
        }
        else if (! status)
-               fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' "
-                               "(last update timestamp = %"PRIscTIME").\n",
-                               host.host_name, host.host_last_update);
+               sdb_error_set(SDB_LOG_DEBUG, "puppet storeconfigs backend: "
+                               "Added/updated host '%s' (last update timestamp = "
+                               "%"PRIscTIME").\n", host.host_name, host.host_last_update);
        free(host.host_name);
        return 0;
 } /* sdb_puppet_stcfg_get_hosts */
@@ -102,9 +103,9 @@ sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
        status = sdb_store_attribute(&attr);
 
        if (status < 0) {
-               fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
-                               "host attribute '%s' for host '%s'.\n", attr.attr_name,
-                               attr.hostname);
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "store/update host attribute '%s' for host '%s'.\n",
+                               attr.attr_name, attr.hostname);
                free(attr.hostname);
                free(attr.attr_name);
                free(attr.attr_value);
@@ -131,12 +132,12 @@ sdb_puppet_stcfg_init(sdb_object_t *user_data)
 
        client = SDB_OBJ_WRAPPER(user_data)->data;
        if (sdb_dbi_client_connect(client)) {
-               fprintf(stderr, "puppet storeconfigs backend: "
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
                                "Failed to connect to the storeconfigs DB.\n");
                return -1;
        }
 
-       fprintf(stderr, "puppet storeconfigs backend: Successfully "
+       sdb_error_set(SDB_LOG_INFO, "puppet storeconfigs backend: Successfully "
                        "connected to the storeconfigs DB.\n");
        return 0;
 } /* sdb_puppet_stcfg_init */
@@ -151,7 +152,7 @@ sdb_puppet_stcfg_collect(sdb_object_t *user_data)
 
        client = SDB_OBJ_WRAPPER(user_data)->data;
        if (sdb_dbi_client_check_conn(client)) {
-               fprintf(stderr, "puppet storeconfigs backend: "
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
                                "Connection to storeconfigs DB failed.\n");
                return -1;
        }
@@ -159,8 +160,8 @@ sdb_puppet_stcfg_collect(sdb_object_t *user_data)
        if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
                                sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
                                /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
-               fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
-                               "hosts from the storeconfigs DB.\n");
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "retrieve hosts from the storeconfigs DB.\n");
                return -1;
        }
 
@@ -177,8 +178,8 @@ sdb_puppet_stcfg_collect(sdb_object_t *user_data)
                                sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
                                /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
                                SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
-               fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
-                               "host attributes from the storeconfigs DB.\n");
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
+                               "retrieve host attributes from the storeconfigs DB.\n");
                return -1;
        }
        return 0;
@@ -200,8 +201,9 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
        int i;
 
        if (oconfig_get_string(ci, &name)) {
-               fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
-                               "single string argument\n\tUsage: <Connection NAME>\n");
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Connection "
+                               "requires a single string argument\n"
+                               "\tUsage: <Connection NAME>\n");
                return -1;
        }
 
@@ -213,8 +215,8 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
 
                if (! strcasecmp(child->key, "DBAdapter")) {
                        if (oconfig_get_string(child, &driver)) {
-                               fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
-                                               "requires a single string argument inside "
+                               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "DBAdapter requires a single string argument inside "
                                                "<Connection %s>\n\tUsage: DBAdapter NAME\n",
                                                name);
                        }
@@ -222,8 +224,8 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                }
                else if (! strcasecmp(child->key, "DBName")) {
                        if (oconfig_get_string(child, &database)) {
-                               fprintf(stderr, "puppet storeconfigs backend: DBName"
-                                               "requires a single string argument inside "
+                               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "DBName requires a single string argument inside "
                                                "<Connection %s>\n\tUsage: DBName NAME\n",
                                                name);
                        }
@@ -249,10 +251,10 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        if ((child->values_num != 2)
                                        || (child->values[0].type != OCONFIG_TYPE_STRING)
                                        || (child->values[1].type != OCONFIG_TYPE_STRING)) {
-                               fprintf(stderr, "puppet storeconfigs backend: DBIOption "
-                                               "requires exactly two string arguments inside "
-                                               "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
-                                               name);
+                               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "DBIOption requires exactly two string arguments "
+                                               "inside <Connection %s>\n"
+                                               "\tUsage: DBIOption KEY VALUE\n", name);
                                continue;
                        }
 
@@ -261,15 +263,15 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        value = child->values[1].value.string;
                }
                else {
-                       fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
-                                       "config option '%s' inside <Connection %s>.\n",
-                                       child->key, name);
+                       sdb_error_set(SDB_LOG_WARNING, "puppet storeconfigs backend: "
+                                       "Ignoring unknown config option '%s' inside "
+                                       "<Connection %s>.\n", child->key, name);
                        continue;
                }
 
                if (status) {
-                       fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
-                                       "requires a single string argument inside "
+                       sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Option "
+                                       "'%s' requires a single string argument inside "
                                        "<Connection %s>\n\tUsage: DBAdapter NAME\n",
                                        child->key, name);
                        continue;
@@ -280,8 +282,8 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                if (! options) {
                        if (! (options = sdb_dbi_options_create())) {
                                char errmsg[1024];
-                               fprintf(stderr, "puppet storeconfigs backend: Failed to "
-                                               "create DBI options object: %s\n",
+                               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                               "Failed to create DBI options object: %s\n",
                                                sdb_strerror(errno, errmsg, sizeof(errmsg)));
                                continue;
                        }
@@ -289,21 +291,22 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
 
                if (sdb_dbi_options_add(options, key, value)) {
                        char errmsg[1024];
-                       fprintf(stderr, "puppet storeconfigs backend: Failed to add "
-                                       "option '%s': %s\n", key,
+                       sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                                       "Failed to add option '%s': %s\n", key,
                                        sdb_strerror(errno, errmsg, sizeof(errmsg)));
                        continue;
                }
        }
 
        if (! driver) {
-               fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
-                               "missing the 'DBAdapter' option.\n", name);
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                               "Connection '%s' " "missing the 'DBAdapter' option.\n",
+                               name);
                return -1;
        }
        if (! database) {
-               fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
-                               "missing the 'DBName' option.\n", name);
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
+                               "Connection '%s' missing the 'DBName' option.\n", name);
                return -1;
        }
 
@@ -313,7 +316,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
        client = sdb_dbi_client_create(driver, database);
        if (! client) {
                char errbuf[1024];
-               fprintf(stderr, "puppet storeconfigs backend: "
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
                                "Failed to create DBI client: %s\n",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
@@ -325,7 +328,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        (void (*)(void *))sdb_dbi_client_destroy);
        if (! user_data) {
                sdb_dbi_client_destroy(client);
-               fprintf(stderr, "puppet storeconfigs backend: "
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
                                "Failed to allocate sdb_object_t\n");
                return -1;
        }
@@ -350,8 +353,8 @@ sdb_puppet_stcfg_config(oconfig_item_t *ci)
                if (! strcasecmp(child->key, "Connection"))
                        sdb_puppet_stcfg_config_conn(child);
                else
-                       fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
-                                       "config option '%s'.\n", child->key);
+                       sdb_error_set(SDB_LOG_WARNING, "puppet storeconfigs backend: "
+                                       "Ignoring unknown config option '%s'.\n", child->key);
        }
        return 0;
 } /* sdb_puppet_stcfg_config */
@@ -369,8 +372,9 @@ sdb_module_init(sdb_plugin_info_t *info)
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
 
        if (dbi_initialize(/* driver dir = */ NULL) < 0) {
-               fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
-                               "DBI; possibly you don't have any drivers installed.\n");
+               sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: failed to "
+                               "initialize DBI; possibly you don't have any drivers "
+                               "installed.\n");
                return -1;
        }