From a6bb88e99588be4c62aa7a81bfae643b3c1888fa Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 10 Mar 2013 21:45:08 +0100 Subject: [PATCH] Use the sdb_error* function throughout the project. --- src/backend/collectd.c | 55 +++++++++--------- src/backend/mk-livestatus.c | 64 ++++++++++----------- src/backend/puppet-storeconfigs.c | 92 ++++++++++++++++--------------- src/core/plugin.c | 57 ++++++++++--------- src/core/store.c | 27 ++++----- src/daemon/config.c | 23 ++++---- src/daemon/sysdbd.c | 35 ++++++------ src/utils/dbi.c | 48 ++++++++-------- src/utils/unixsock.c | 55 +++++++++--------- 9 files changed, 244 insertions(+), 212 deletions(-) diff --git a/src/backend/collectd.c b/src/backend/collectd.c index e0e924e..2a834f5 100644 --- a/src/backend/collectd.c +++ b/src/backend/collectd.c @@ -28,6 +28,7 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" +#include "utils/error.h" #include "utils/string.h" #include "utils/unixsock.h" @@ -75,14 +76,14 @@ sdb_collectd_add_host(const char *hostname, sdb_time_t last_update) status = sdb_store_host(&host); if (status < 0) { - fprintf(stderr, "collectd backend: Failed to store/update " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to store/update " "host '%s'.\n", name); return -1; } else if (status > 0) /* value too old */ return 0; - fprintf(stderr, "collectd backend: Added/updated host '%s' " + sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated host '%s' " "(last update timestamp = %"PRIscTIME").\n", name, last_update); return 0; @@ -107,7 +108,7 @@ sdb_collectd_add_svc(const char *hostname, const char *plugin, status = sdb_store_service(&svc); if (status < 0) { - fprintf(stderr, "collectd backend: Failed to store/update " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to store/update " "service '%s/%s'.\n", host, name); return -1; } @@ -147,7 +148,7 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client, if (! state->current_host) { char errbuf[1024]; - fprintf(stderr, "collectd backend: Failed to allocate " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to allocate " "string buffer: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; @@ -170,7 +171,7 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client, /* new host */ sdb_collectd_add_host(hostname, last_update); - fprintf(stderr, "collectd backend: Added/updated " + sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated " "%i service%s (%i failed) for host '%s'.\n", state->svc_updated, state->svc_updated == 1 ? "" : "s", state->svc_failed, state->current_host); @@ -196,12 +197,12 @@ sdb_collectd_init(sdb_object_t *user_data) client = SDB_OBJ_WRAPPER(user_data)->data; if (sdb_unixsock_client_connect(client)) { - fprintf(stderr, "collectd backend: " + sdb_error_set(SDB_LOG_ERR, "collectd backend: " "Failed to connect to collectd.\n"); return -1; } - fprintf(stderr, "collectd backend: Successfully " + sdb_error_set(SDB_LOG_INFO, "collectd backend: Successfully " "connected to collectd @ %s.\n", sdb_unixsock_client_path(client)); return 0; @@ -235,14 +236,15 @@ sdb_collectd_collect(sdb_object_t *user_data) client = SDB_OBJ_WRAPPER(user_data)->data; if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) { - fprintf(stderr, "collectd backend: Failed to send LISTVAL command " - "to collectd @ %s.\n", sdb_unixsock_client_path(client)); + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to send LISTVAL " + "command to collectd @ %s.\n", + sdb_unixsock_client_path(client)); return -1; } line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer)); if (! line) { - fprintf(stderr, "collectd backend: Failed to read status " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to read status " "of LISTVAL command from collectd @ %s.\n", sdb_unixsock_client_path(client)); return -1; @@ -257,15 +259,16 @@ sdb_collectd_collect(sdb_object_t *user_data) errno = 0; count = strtol(line, &endptr, /* base */ 0); if (errno || (line == endptr)) { - fprintf(stderr, "collectd backend: Failed to parse status " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to parse status " "of LISTVAL command from collectd @ %s.\n", sdb_unixsock_client_path(client)); return -1; } if (count < 0) { - fprintf(stderr, "collectd backend: Failed to get value list " - "from collectd @ %s: %s\n", sdb_unixsock_client_path(client), + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to get value " + "list from collectd @ %s: %s\n", + sdb_unixsock_client_path(client), msg ? msg : line); return -1; } @@ -275,14 +278,14 @@ sdb_collectd_collect(sdb_object_t *user_data) /* column count = */ 4, SDB_TYPE_DATETIME, SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) { - fprintf(stderr, "collectd backend: Failed to read response " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to read response " "from collectd @ %s.\n", sdb_unixsock_client_path(client)); return -1; } if (state.current_host) { sdb_collectd_add_host(state.current_host, state.current_timestamp); - fprintf(stderr, "collectd backend: Added/updated " + sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated " "%i service%s (%i failed) for host '%s'.\n", state.svc_updated, state.svc_updated == 1 ? "" : "s", state.svc_failed, state.current_host); @@ -304,8 +307,8 @@ sdb_collectd_config_instance(oconfig_item_t *ci) int i; if (oconfig_get_string(ci, &name)) { - fprintf(stderr, "collectd backend: Instance requires a single " - "string argument\n\tUsage: \n"); + sdb_error_set(SDB_LOG_ERR, "collectd backend: Instance requires a " + "single string argument\n\tUsage: \n"); return -1; } @@ -315,13 +318,13 @@ sdb_collectd_config_instance(oconfig_item_t *ci) if (! strcasecmp(child->key, "Socket")) oconfig_get_string(child, &socket_path); else - fprintf(stderr, "collectd backend: Ignoring unknown config " - "option '%s' inside .\n", + sdb_error_set(SDB_LOG_WARNING, "collectd backend: Ignoring " + "unknown config option '%s' inside .\n", child->key, name); } if (! socket_path) { - fprintf(stderr, "collectd backend: Instance '%s' missing " + sdb_error_set(SDB_LOG_ERR, "collectd backend: Instance '%s' missing " "the 'Socket' option.\n", name); return -1; } @@ -332,8 +335,9 @@ sdb_collectd_config_instance(oconfig_item_t *ci) client = sdb_unixsock_client_create(socket_path); if (! client) { char errbuf[1024]; - fprintf(stderr, "collectd backend: Failed to create unixsock client: " - "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to create " + "unixsock client: %s\n", + sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } @@ -341,7 +345,8 @@ sdb_collectd_config_instance(oconfig_item_t *ci) (void (*)(void *))sdb_unixsock_client_destroy); if (! user_data) { sdb_unixsock_client_destroy(client); - fprintf(stderr, "collectd backend: Failed to allocate sdb_object_t\n"); + sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to allocate " + "sdb_object_t\n"); return -1; } @@ -367,8 +372,8 @@ sdb_collectd_config(oconfig_item_t *ci) if (! strcasecmp(child->key, "Instance")) sdb_collectd_config_instance(child); else - fprintf(stderr, "collectd backend: Ignoring unknown config " - "option '%s'.\n", child->key); + sdb_error_set(SDB_LOG_WARNING, "collectd backend: Ignoring " + "unknown config option '%s'.\n", child->key); } return 0; } /* sdb_collectd_config */ diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index a5126fb..f65e758 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -28,6 +28,7 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" +#include "utils/error.h" #include "utils/string.h" #include "utils/unixsock.h" @@ -72,16 +73,16 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client, status = sdb_store_host(&host); if (status < 0) { - fprintf(stderr, "MK Livestatus backend: Failed to store/update " - "host '%s'.\n", hostname); + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to " + "store/update host '%s'.\n", hostname); free(hostname); return -1; } else if (status > 0) /* value too old */ return 0; - fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' " - "(last update timestamp = %"PRIscTIME").\n", + sdb_error_set(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " + "host '%s' (last update timestamp = %"PRIscTIME").\n", hostname, timestamp); free(hostname); return 0; @@ -116,8 +117,8 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client, status = sdb_store_service(&svc); if (status < 0) { - fprintf(stderr, "MK Livestatus backend: Failed to store/update " - "service '%s / %s'.\n", hostname, svcname); + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to " + "store/update service '%s / %s'.\n", hostname, svcname); free(hostname); free(svcname); return -1; @@ -125,8 +126,8 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client, else if (status > 0) /* value too old */ return 0; - fprintf(stderr, "MK Livestatus backend: Added/updated service '%s / %s' " - "(last update timestamp = %"PRIscTIME").\n", + sdb_error_set(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " + "service '%s / %s' (last update timestamp = %"PRIscTIME").\n", hostname, svcname, timestamp); free(hostname); free(svcname); @@ -147,13 +148,13 @@ sdb_livestatus_init(sdb_object_t *user_data) client = SDB_OBJ_WRAPPER(user_data)->data; if (sdb_unixsock_client_connect(client)) { - fprintf(stderr, "MK Livestatus backend: " + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: " "Failed to connect to livestatus @ %s.\n", sdb_unixsock_client_path(client)); return -1; } - fprintf(stderr, "MK Livestatus backend: Successfully " + sdb_error_set(SDB_LOG_INFO, "MK Livestatus backend: Successfully " "connected to livestatus @ %s.\n", sdb_unixsock_client_path(client)); return 0; @@ -174,7 +175,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) status = sdb_unixsock_client_send(client, "GET hosts\r\n" "Columns: name last_check"); if (status <= 0) { - fprintf(stderr, "MK Livestatus backend: Failed to send " + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to send " "'GET hosts' command to livestatus @ %s.\n", sdb_unixsock_client_path(client)); return -1; @@ -185,8 +186,8 @@ sdb_livestatus_collect(sdb_object_t *user_data) if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host, /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";", /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) { - fprintf(stderr, "MK Livestatus backend: Failed to read response " - "from livestatus @ %s while reading hosts.\n", + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " + "response from livestatus @ %s while reading hosts.\n", sdb_unixsock_client_path(client)); return -1; } @@ -194,8 +195,8 @@ sdb_livestatus_collect(sdb_object_t *user_data) if ((! sdb_unixsock_client_eof(client)) || sdb_unixsock_client_error(client)) { char errbuf[1024]; - fprintf(stderr, "MK Livestatus backend: Failed to read host " - "from livestatus @ %s: %s\n", + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " + "host from livestatus @ %s: %s\n", sdb_unixsock_client_path(client), sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; @@ -204,7 +205,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) status = sdb_unixsock_client_send(client, "GET services\r\n" "Columns: host_name description last_check"); if (status <= 0) { - fprintf(stderr, "MK Livestatus backend: Failed to send " + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to send " "'GET services' command to livestatus @ %s.\n", sdb_unixsock_client_path(client)); return -1; @@ -216,8 +217,8 @@ sdb_livestatus_collect(sdb_object_t *user_data) /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";", /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) { - fprintf(stderr, "MK Livestatus backend: Failed to read response " - "from livestatus @ %s while reading services.\n", + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " + "response from livestatus @ %s while reading services.\n", sdb_unixsock_client_path(client)); return -1; } @@ -225,8 +226,8 @@ sdb_livestatus_collect(sdb_object_t *user_data) if ((! sdb_unixsock_client_eof(client)) || sdb_unixsock_client_error(client)) { char errbuf[1024]; - fprintf(stderr, "MK Livestatus backend: Failed to read services " - "from livestatus @ %s: %s\n", + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " + "services from livestatus @ %s: %s\n", sdb_unixsock_client_path(client), sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; @@ -248,8 +249,8 @@ sdb_livestatus_config_instance(oconfig_item_t *ci) int i; if (oconfig_get_string(ci, &name)) { - fprintf(stderr, "MK Livestatus backend: Instance requires a single " - "string argument\n\tUsage: \n"); + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Instance requires " + "a single string argument\n\tUsage: \n"); return -1; } @@ -259,14 +260,14 @@ sdb_livestatus_config_instance(oconfig_item_t *ci) if (! strcasecmp(child->key, "Socket")) oconfig_get_string(child, &socket_path); else - fprintf(stderr, "MK Livestatus backend: Ignoring unknown config " - "option '%s' inside .\n", + sdb_error_set(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring " + "unknown config option '%s' inside .\n", child->key, name); } if (! socket_path) { - fprintf(stderr, "MK Livestatus backend: Instance '%s' missing " - "the 'Socket' option.\n", name); + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' " + "missing the 'Socket' option.\n", name); return -1; } @@ -276,8 +277,9 @@ sdb_livestatus_config_instance(oconfig_item_t *ci) client = sdb_unixsock_client_create(socket_path); if (! client) { char errbuf[1024]; - fprintf(stderr, "MK Livestatus backend: Failed to create unixsock " - "client: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to create " + "unixsock client: %s\n", + sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } @@ -285,7 +287,7 @@ sdb_livestatus_config_instance(oconfig_item_t *ci) (void (*)(void *))sdb_unixsock_client_destroy); if (! user_data) { sdb_unixsock_client_destroy(client); - fprintf(stderr, "MK Livestatus backend: Failed to " + sdb_error_set(SDB_LOG_ERR, "MK Livestatus backend: Failed to " "allocate sdb_object_t\n"); return -1; } @@ -310,8 +312,8 @@ sdb_livestatus_config(oconfig_item_t *ci) if (! strcasecmp(child->key, "Instance")) sdb_livestatus_config_instance(child); else - fprintf(stderr, "MK Livestatus backend: Ignoring unknown config " - "option '%s'.\n", child->key); + sdb_error_set(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring " + "unknown config option '%s'.\n", child->key); } return 0; } /* sdb_livestatus_config */ diff --git a/src/backend/puppet-storeconfigs.c b/src/backend/puppet-storeconfigs.c index f033808..4c92c15 100644 --- a/src/backend/puppet-storeconfigs.c +++ b/src/backend/puppet-storeconfigs.c @@ -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: \n"); + sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Connection " + "requires a single string argument\n" + "\tUsage: \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 " "\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 " "\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 " - "\n\tUsage: DBIOption KEY VALUE\n", - name); + sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: " + "DBIOption requires exactly two string arguments " + "inside \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 .\n", - child->key, name); + sdb_error_set(SDB_LOG_WARNING, "puppet storeconfigs backend: " + "Ignoring unknown config option '%s' inside " + ".\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 " "\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; } diff --git a/src/core/plugin.c b/src/core/plugin.c index 5c9a233..7f5883a 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -27,6 +27,7 @@ #include "sysdb.h" #include "core/plugin.h" +#include "utils/error.h" #include "utils/llist.h" #include "utils/string.h" #include "utils/time.h" @@ -198,9 +199,9 @@ sdb_plugin_cb_init(sdb_object_t *obj, va_list ap) assert(obj); if (sdb_plugin_find_by_name(*list, name)) { - fprintf(stderr, "plugin: %s callback '%s' has already been " - "registered. Ignoring newly registered version.\n", - type, name); + sdb_error_set(SDB_LOG_WARNING, "plugin: %s callback '%s' " + "has already been registered. Ignoring newly " + "registered version.\n", type, name); return -1; } @@ -252,7 +253,8 @@ sdb_plugin_add_callback(sdb_llist_t **list, const char *type, /* pass control to the list */ sdb_object_deref(obj); - fprintf(stderr, "plugin: Registered %s callback '%s'.\n", type, name); + sdb_error_set(SDB_LOG_INFO, "plugin: Registered %s callback '%s'.\n", + type, name); return 0; } /* sdb_plugin_add_callback */ @@ -278,7 +280,7 @@ sdb_plugin_load(const char *name) if (access(filename, R_OK)) { char errbuf[1024]; - fprintf(stderr, "plugin: Failed to load plugin '%s': %s\n", + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s\n", name, sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } @@ -288,7 +290,7 @@ sdb_plugin_load(const char *name) lh = lt_dlopen(filename); if (! lh) { - fprintf(stderr, "plugin: Failed to load plugin '%s': %s\n" + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': %s\n" "The most common cause for this problem are missing " "dependencies.\n", name, lt_dlerror()); return -1; @@ -296,27 +298,28 @@ sdb_plugin_load(const char *name) mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init"); if (! mod_init) { - fprintf(stderr, "plugin: Failed to load plugin '%s': " + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to load plugin '%s': " "could not find symbol 'sdb_module_init'\n", name); return -1; } status = mod_init(&plugin_info); if (status) { - fprintf(stderr, "plugin: Failed to initialize plugin '%s'\n", name); + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to initialize " + "plugin '%s'\n", name); return -1; } /* compare minor version */ if ((plugin_info.version < 0) || ((int)(plugin_info.version / 100) != (int)(SDB_VERSION / 100))) - fprintf(stderr, "plugin: WARNING: version of plugin '%s' (%i.%i.%i) " - "does not match our version (%i.%i.%i); " - "this might cause problems\n", + sdb_error_set(SDB_LOG_WARNING, "plugin: WARNING: version of " + "plugin '%s' (%i.%i.%i) does not match our version " + "(%i.%i.%i); this might cause problems\n", name, SDB_VERSION_DECODE(plugin_info.version), SDB_VERSION_DECODE(SDB_VERSION)); - fprintf(stderr, "plugin: Successfully loaded " + sdb_error_set(SDB_LOG_INFO, "plugin: Successfully loaded " "plugin '%s' v%i (%s)\n\t%s\n", plugin_info.name, plugin_info.plugin_version, plugin_info.description, plugin_info.copyright); @@ -435,8 +438,8 @@ sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback if (! (SDB_PLUGIN_CCB(obj)->ccb_next_update = sdb_gettime())) { char errbuf[1024]; - fprintf(stderr, "plugin: Failed to determine current time: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to determine current " + "time: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); sdb_object_deref(obj); return -1; } @@ -450,7 +453,7 @@ sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback /* pass control to the list */ sdb_object_deref(obj); - fprintf(stderr, "plugin: Registered collector callback '%s' " + sdb_error_set(SDB_LOG_INFO, "plugin: Registered collector callback '%s' " "(interval = %.3fs).\n", name, SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval)); return 0; @@ -507,7 +510,8 @@ sdb_plugin_configure(const char *name, oconfig_item_t *ci) plugin = sdb_plugin_find_by_name(config_list, name); if (! plugin) { - fprintf(stderr, "plugin: Plugin '%s' did not register " + /* XXX: check if any such plugin has been loaded */ + sdb_error_set(SDB_LOG_ERR, "plugin: Plugin '%s' did not register " "a config callback.\n", name); errno = ENOENT; return -1; @@ -564,8 +568,8 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop) if (! (now = sdb_gettime())) { char errbuf[1024]; - fprintf(stderr, "plugin: Failed to determine current time: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to determine current " + "time: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); now = SDB_PLUGIN_CCB(obj)->ccb_next_update; } @@ -576,7 +580,7 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop) while (loop->do_loop && sdb_sleep(interval, &interval)) { if (errno != EINTR) { char errbuf[1024]; - fprintf(stderr, "plugin: Failed to sleep: %s\n", + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to sleep: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } @@ -597,7 +601,7 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop) if (! interval) interval = loop->default_interval; if (! interval) { - fprintf(stderr, "plugin: No interval configured " + sdb_error_set(SDB_LOG_WARNING, "plugin: No interval configured " "for plugin '%s'; skipping any further " "iterations.\n", SDB_PLUGIN_CCB(obj)->ccb_name); sdb_object_deref(obj); @@ -608,22 +612,23 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop) if (! (now = sdb_gettime())) { char errbuf[1024]; - fprintf(stderr, "plugin: Failed to determine current time: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to determine current " + "time: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); now = SDB_PLUGIN_CCB(obj)->ccb_next_update; } if (now > SDB_PLUGIN_CCB(obj)->ccb_next_update) { - fprintf(stderr, "plugin: Plugin '%s' took too long; " - "skipping iterations to keep up.\n", + sdb_error_set(SDB_LOG_WARNING, "plugin: Plugin '%s' took too " + "long; skipping iterations to keep up.\n", SDB_PLUGIN_CCB(obj)->ccb_name); SDB_PLUGIN_CCB(obj)->ccb_next_update = now; } if (sdb_llist_insert_sorted(collector_list, obj, sdb_plugin_cmp_next_update)) { - fprintf(stderr, "plugin: Failed to re-insert " - "plugin '%s' into collector list.\n", + sdb_error_set(SDB_LOG_ERR, "plugin: Failed to re-insert " + "plugin '%s' into collector list. Unable to further " + "use the plugin.\n", SDB_PLUGIN_CCB(obj)->ccb_name); sdb_object_deref(obj); return -1; diff --git a/src/core/store.c b/src/core/store.c index cbd5f0c..a0bbf55 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -27,6 +27,7 @@ #include "sysdb.h" #include "core/store.h" +#include "utils/error.h" #include "utils/llist.h" #include "utils/string.h" @@ -257,7 +258,7 @@ sdb_store_host(const sdb_host_t *host) if (old) { if (old->host_last_update > last_update) { - fprintf(stderr, "store: Cannot update host '%s' - " + sdb_error_set(SDB_LOG_DEBUG, "store: Cannot update host '%s' - " "value too old (%"PRIscTIME" < %"PRIscTIME")\n", host->host_name, last_update, old->host_last_update); /* don't report an error; the host may be updated by multiple @@ -272,8 +273,8 @@ sdb_store_host(const sdb_host_t *host) sdb_host_t *new = sdb_host_clone(host); if (! new) { char errbuf[1024]; - fprintf(stderr, "store: Failed to clone host object: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "store: Failed to clone host object: " + "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); pthread_rwlock_unlock(&host_lock); return -1; } @@ -281,7 +282,7 @@ sdb_store_host(const sdb_host_t *host) if (! new->attributes) { if (! (new->attributes = sdb_llist_create())) { char errbuf[1024]; - fprintf(stderr, "store: Failed to initialize " + sdb_error_set(SDB_LOG_ERR, "store: Failed to initialize " "host object '%s': %s\n", host->host_name, sdb_strerror(errno, errbuf, sizeof(errbuf))); sdb_object_deref(SDB_OBJ(new)); @@ -293,7 +294,7 @@ sdb_store_host(const sdb_host_t *host) if (! new->services) { if (! (new->services = sdb_llist_create())) { char errbuf[1024]; - fprintf(stderr, "store: Failed to initialize " + sdb_error_set(SDB_LOG_ERR, "store: Failed to initialize " "host object '%s': %s\n", host->host_name, sdb_strerror(errno, errbuf, sizeof(errbuf))); sdb_object_deref(SDB_OBJ(new)); @@ -401,8 +402,8 @@ sdb_store_attribute(const sdb_attribute_t *attr) if (old) { if (old->host_last_update > last_update) { - fprintf(stderr, "store: Cannot update attribute '%s/%s' - " - "value too old (%"PRIscTIME" < %"PRIscTIME")\n", + sdb_error_set(SDB_LOG_DEBUG, "store: Cannot update attribute " + "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")\n", attr->hostname, attr->attr_name, last_update, old->host_last_update); status = 1; @@ -415,8 +416,8 @@ sdb_store_attribute(const sdb_attribute_t *attr) sdb_attribute_t *new = sdb_attribute_clone(attr); if (! new) { char errbuf[1024]; - fprintf(stderr, "store: Failed to clone attribute object: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "store: Failed to clone attribute " + "object: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); pthread_rwlock_unlock(&host_lock); return -1; } @@ -499,8 +500,8 @@ sdb_store_service(const sdb_service_t *svc) if (old) { if (old->host_last_update > last_update) { - fprintf(stderr, "store: Cannot update service '%s/%s' - " - "value too old (%"PRIscTIME" < %"PRIscTIME")\n", + sdb_error_set(SDB_LOG_DEBUG, "store: Cannot update service " + "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")\n", svc->hostname, svc->svc_name, last_update, old->host_last_update); status = 1; @@ -513,8 +514,8 @@ sdb_store_service(const sdb_service_t *svc) sdb_service_t *new = sdb_service_clone(svc); if (! new) { char errbuf[1024]; - fprintf(stderr, "store: Failed to clone service object: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "store: Failed to clone service " + "object: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); pthread_rwlock_unlock(&host_lock); return -1; } diff --git a/src/daemon/config.c b/src/daemon/config.c index aa5cb99..ea870c2 100644 --- a/src/daemon/config.c +++ b/src/daemon/config.c @@ -27,6 +27,7 @@ #include "sysdb.h" #include "core/plugin.h" +#include "utils/error.h" #include "utils/time.h" #include "daemon/config.h" @@ -55,14 +56,14 @@ config_get_interval(oconfig_item_t *ci, sdb_time_t *interval) assert(ci && interval); if (oconfig_get_number(ci, &interval_dbl)) { - fprintf(stderr, "config: Interval requires " + sdb_error_set(SDB_LOG_ERR, "config: Interval requires " "a single numeric argument\n" "\tUsage: Interval SECONDS\n"); return -1; } if (interval_dbl <= 0.0) { - fprintf(stderr, "config: Invalid interval: %f\n" + sdb_error_set(SDB_LOG_ERR, "config: Invalid interval: %f\n" "\tInterval may not be less than or equal to zero.\n", interval_dbl); return -1; @@ -101,7 +102,7 @@ daemon_load_backend(oconfig_item_t *ci) ctx.interval = default_interval; if (oconfig_get_string(ci, &name)) { - fprintf(stderr, "config: LoadBackend requires a single " + sdb_error_set(SDB_LOG_ERR, "config: LoadBackend requires a single " "string argument\n" "\tUsage: LoadBackend BACKEND\n"); return -1; @@ -117,9 +118,10 @@ daemon_load_backend(oconfig_item_t *ci) return -1; } else { - fprintf(stderr, "config: Unknown option '%s' inside 'LoadBackend' " - "-- see the documentation for details.\n", child->key); - return -1; + sdb_error_set(SDB_LOG_WARNING, "config: Unknown option '%s' " + "inside 'LoadBackend' -- see the documentation for " + "details.\n", child->key); + continue; } } @@ -137,7 +139,7 @@ daemon_configure_plugin(oconfig_item_t *ci) assert(ci); if (oconfig_get_string(ci, &name)) { - fprintf(stderr, "config: %s requires a single " + sdb_error_set(SDB_LOG_ERR, "config: %s requires a single " "string argument\n" "\tUsage: LoadBackend BACKEND\n", ci->key); @@ -179,12 +181,13 @@ daemon_parse_config(const char *filename) } if (status) { - fprintf(stderr, "config: Failed to parse option '%s'\n", - child->key); + sdb_error_start(SDB_LOG_ERR, "config: Failed to parse option " + "'%s'\n", child->key); if (status > 0) - fprintf(stderr, "\tUnknown option '%s' -- " + sdb_error_append("\tUnknown option '%s' -- " "see the documentation for details\n", child->key); + sdb_error_finish(); retval = -1; } } diff --git a/src/daemon/sysdbd.c b/src/daemon/sysdbd.c index ef53a24..d4851f8 100644 --- a/src/daemon/sysdbd.c +++ b/src/daemon/sysdbd.c @@ -32,6 +32,7 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" +#include "utils/error.h" #include "utils/string.h" #include "daemon/config.h" @@ -108,7 +109,7 @@ daemonize(void) if ((pid = fork()) < 0) { char errbuf[1024]; - fprintf(stderr, "Failed to fork to background: %s\n", + sdb_error_set(SDB_LOG_ERR, "Failed to fork to background: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return errno; } @@ -119,7 +120,8 @@ daemonize(void) if (chdir("/")) { char errbuf[1024]; - fprintf(stderr, "Failed to change working directory to /: %s\n", + sdb_error_set(SDB_LOG_ERR, "Failed to change working directory to " + "the root directory: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return errno; } @@ -130,24 +132,24 @@ daemonize(void) close(0); if (open("/dev/null", O_RDWR)) { char errbuf[1024]; - fprintf(stderr, "Failed to connect stdin to '/dev/null': %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "Failed to connect stdin to '/dev/null': " + "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return errno; } close(1); if (dup(0) != 1) { char errbuf[1024]; - fprintf(stderr, "Could not connect stdout to '/dev/null': %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "Could not connect stdout to '/dev/null': " + "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return errno; } close(2); if (dup(0) != 2) { char errbuf[1024]; - fprintf(stdout, "Could not connect stderr to '/dev/null': %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "Could not connect stderr to '/dev/null': " + "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return errno; } return 0; @@ -193,7 +195,7 @@ main(int argc, char **argv) config_filename = CONFIGFILE; if (daemon_parse_config(config_filename)) { - fprintf(stderr, "Failed to parse configuration file.\n"); + sdb_error_set(SDB_LOG_ERR, "Failed to parse configuration file.\n"); exit(1); } @@ -203,14 +205,14 @@ main(int argc, char **argv) if (sigaction(SIGINT, &sa_intterm, /* old action */ NULL)) { char errbuf[1024]; - fprintf(stderr, "Failed to install signal handler for SIGINT: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "Failed to install signal handler for " + "SIGINT: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); exit(1); } if (sigaction(SIGTERM, &sa_intterm, /* old action */ NULL)) { char errbuf[1024]; - fprintf(stderr, "Failed to install signal handler for SIGTERM: %s\n", - sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "Failed to install signal handler for " + "SIGTERM: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); exit(1); } @@ -218,13 +220,14 @@ main(int argc, char **argv) if (daemonize()) exit(1); - fprintf(stderr, "SysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA - " (pid %i) initialized successfully\n", (int)getpid()); + sdb_error_set(SDB_LOG_INFO, "SysDB daemon "SDB_VERSION_STRING + SDB_VERSION_EXTRA " (pid %i) initialized successfully\n", + (int)getpid()); sdb_plugin_init_all(); sdb_plugin_collector_loop(&plugin_main_loop); - fprintf(stderr, "Shutting down SysDB daemon "SDB_VERSION_STRING + sdb_error_set(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING SDB_VERSION_EXTRA" (pid %i)\n", (int)getpid()); fprintf(stderr, "Store dump:\n"); diff --git a/src/utils/dbi.c b/src/utils/dbi.c index 380a841..dc1d6e4 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -25,6 +25,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "utils/error.h" #include "utils/dbi.h" #include @@ -101,7 +102,7 @@ sdb_dbi_get_field(dbi_result res, unsigned int i, } break; default: - fprintf(stderr, "dbi: Unexpected type %i while " + sdb_error_set(SDB_LOG_ERR, "dbi: Unexpected type %i while " "parsing query result.\n", type); return -1; } @@ -128,7 +129,7 @@ sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res, for (i = 0; i < num_fields; ++i) { types[i] = dbi_result_get_field_type_idx(res, i + 1); if (types[i] == DBI_TYPE_ERROR) { - fprintf(stderr, "dbi: failed to fetch data: %s\n", + sdb_error_set(SDB_LOG_ERR, "dbi: failed to fetch data: %s\n", sdb_dbi_strerror(client->conn)); return -1; } @@ -141,8 +142,8 @@ sdb_dbi_get_data(sdb_dbi_client_t *client, dbi_result res, for (n = 0; n < num_rows; ++n) { if (! dbi_result_seek_row(res, n + 1)) { - fprintf(stderr, "dbi: Failed to retrieve row %llu: %s\n", - n, sdb_dbi_strerror(client->conn)); + sdb_error_set(SDB_LOG_ERR, "dbi: Failed to retrieve row %llu: " + "%s\n", n, sdb_dbi_strerror(client->conn)); continue; } @@ -288,21 +289,23 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) driver = dbi_driver_open(client->driver); if (! driver) { - fprintf(stderr, "dbi: failed to open DBI driver '%s'; " + sdb_error_start(SDB_LOG_ERR, "dbi: failed to open DBI driver '%s'; " "possibly it's not installed.\n", client->driver); - fprintf(stderr, "dbi: known drivers:\n"); + sdb_error_append("dbi: known drivers:\n"); for (driver = dbi_driver_list(NULL); driver; driver = dbi_driver_list(driver)) { - fprintf(stderr, "\t- %s\n", dbi_driver_get_name(driver)); + sdb_error_append("\t- %s\n", dbi_driver_get_name(driver)); } + sdb_error_finish(); return -1; } client->conn = dbi_conn_open(driver); if (! client->conn) { - fprintf(stderr, "dbi: failed to open connection object.\n"); + sdb_error_set(SDB_LOG_ERR, "dbi: failed to open connection " + "object.\n"); return -1; } @@ -316,14 +319,15 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) continue; /* else: error */ - fprintf(stderr, "dbi: failed to set option '%s': %s\n", - client->options->options[i].key, + sdb_error_start(SDB_LOG_ERR, "dbi: failed to set option '%s': " + "%s\n", client->options->options[i].key, sdb_dbi_strerror(client->conn)); - fprintf(stderr, "dbi: known driver options:\n"); + sdb_error_append("dbi: known driver options:\n"); for (opt = dbi_conn_get_option_list(client->conn, NULL); opt; opt = dbi_conn_get_option_list(client->conn, opt)) - fprintf(stderr, "\t- %s\n", opt); + sdb_error_append("\t- %s\n", opt); + sdb_error_finish(); dbi_conn_close(client->conn); return -1; @@ -331,15 +335,15 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) } if (dbi_conn_set_option(client->conn, "dbname", client->database)) { - fprintf(stderr, "dbi: failed to set option 'dbname': %s\n", + sdb_error_set(SDB_LOG_ERR, "dbi: failed to set option 'dbname': %s\n", sdb_dbi_strerror(client->conn)); dbi_conn_close(client->conn); return -1; } if (dbi_conn_connect(client->conn) < 0) { - fprintf(stderr, "dbi: failed to connect to database '%s': %s\n", - client->database, sdb_dbi_strerror(client->conn)); + sdb_error_set(SDB_LOG_ERR, "dbi: failed to connect to database '%s': " + "%s\n", client->database, sdb_dbi_strerror(client->conn)); dbi_conn_close(client->conn); return -1; } @@ -374,14 +378,14 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, res = dbi_conn_query(client->conn, query); if (! res) { - fprintf(stderr, "dbi: failed to execute query '%s': %s\n", + sdb_error_set(SDB_LOG_ERR, "dbi: failed to execute query '%s': %s\n", query, sdb_dbi_strerror(client->conn)); return -1; } if (dbi_result_get_numrows(res) == DBI_ROW_ERROR) { - fprintf(stderr, "dbi: failed to fetch rows for query '%s': %s\n", - query, sdb_dbi_strerror(client->conn)); + sdb_error_set(SDB_LOG_ERR, "dbi: failed to fetch rows for query " + "'%s': %s\n", query, sdb_dbi_strerror(client->conn)); dbi_result_free(res); return -1; } @@ -398,8 +402,8 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, int i; if (n != (int)num_fields) { - fprintf(stderr, "dbi: number of returned fields (%i) does not " - "match the number of requested fields (%i) " + sdb_error_set(SDB_LOG_ERR, "dbi: number of returned fields (%i) " + "does not match the number of requested fields (%i) " "for query '%s'.\n", num_fields, n, query); dbi_result_free(res); return -1; @@ -418,8 +422,8 @@ sdb_dbi_exec_query(sdb_dbi_client_t *client, const char *query, /* column count starts at 1 */ if ((unsigned int)field_type != type) { - fprintf(stderr, "dbi: type of column '%s' (%u) does not match " - "requested type (%u).\n", + sdb_error_set(SDB_LOG_ERR, "dbi: type of column '%s' (%u) " + "does not match requested type (%u).\n", dbi_result_get_field_name(res, (unsigned int)i + 1), field_type, type); status = -1; diff --git a/src/utils/unixsock.c b/src/utils/unixsock.c index 564aba7..b4b9772 100644 --- a/src/utils/unixsock.c +++ b/src/utils/unixsock.c @@ -26,6 +26,7 @@ */ #include "utils/unixsock.h" +#include "utils/error.h" #include "utils/string.h" #include @@ -116,7 +117,7 @@ sdb_unixsock_parse_cell(char *string, int type, sdb_data_t *data) data->data.binary.datum = (const unsigned char *)string; break; default: - fprintf(stderr, "unixsock: Unexpected type %i while " + sdb_error_set(SDB_LOG_ERR, "unixsock: Unexpected type %i while " "parsing query result.\n", type); return -1; } @@ -125,14 +126,14 @@ sdb_unixsock_parse_cell(char *string, int type, sdb_data_t *data) || (type == SDB_TYPE_DATETIME)) { if (errno || (string == endptr)) { char errbuf[1024]; - fprintf(stderr, "unixsock: Failed to parse string '%s' " - "as numeric value (type %i): %s\n", string, type, + sdb_error_set(SDB_LOG_ERR, "unixsock: Failed to parse string " + "'%s' as numeric value (type %i): %s\n", string, type, sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } else if (endptr && (*endptr != '\0')) - fprintf(stderr, "unixsock: Ignoring garbage after number " - "while parsing numeric value (type %i): %s.\n", + sdb_error_set(SDB_LOG_WARNING, "unixsock: Ignoring garbage after " + "number while parsing numeric value (type %i): %s.\n", type, endptr); } @@ -157,9 +158,10 @@ sdb_unixsock_client_process_one_line(sdb_unixsock_client_t *client, char *next; if (! line) { /* this must no happen */ - fprintf(stderr, "unixsock: Unexpected EOL while parsing line " - "(expected %i columns delimited by '%s'; got %i): %s\n", - column_count, delim, /* last line number */ i, orig_line); + sdb_error_set(SDB_LOG_ERR, "unixsock: Unexpected EOL while " + "parsing line (expected %i columns delimited by '%s'; " + "got %i): %s\n", column_count, delim, + /* last line number */ i, orig_line); return -1; } @@ -230,7 +232,7 @@ sdb_unixsock_client_connect(sdb_unixsock_client_t *client) fd = socket(AF_UNIX, SOCK_STREAM, /* protocol = */ 0); if (fd < 0) { char errbuf[1024]; - fprintf(stderr, "unixsock: Failed to open socket: %s\n", + sdb_error_set(SDB_LOG_ERR, "unixsock: Failed to open socket: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } @@ -241,7 +243,7 @@ sdb_unixsock_client_connect(sdb_unixsock_client_t *client) if (connect(fd, (struct sockaddr *)&sa, sizeof(sa))) { char errbuf[1024]; - fprintf(stderr, "unixsock: Failed to connect to %s: %s\n", + sdb_error_set(SDB_LOG_ERR, "unixsock: Failed to connect to %s: %s\n", sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf))); close(fd); return -1; @@ -250,8 +252,9 @@ sdb_unixsock_client_connect(sdb_unixsock_client_t *client) client->fh = fdopen(fd, "r+"); if (! client->fh) { char errbuf[1024]; - fprintf(stderr, "unixsock: Failed to open I/O stream for %s: %s\n", - sa.sun_path, sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "unixsock: Failed to open I/O " + "stream for %s: %s\n", sa.sun_path, + sdb_strerror(errno, errbuf, sizeof(errbuf))); close(fd); return -1; } @@ -275,8 +278,9 @@ sdb_unixsock_client_send(sdb_unixsock_client_t *client, status = fprintf(client->fh, "%s\r\n", msg); if (status < 0) { char errbuf[1024]; - fprintf(stderr, "unixsock: Failed to write to socket (%s): %s\n", - client->path, sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "unixsock: Failed to write to " + "socket (%s): %s\n", client->path, + sdb_strerror(errno, errbuf, sizeof(errbuf))); return status; } return status; @@ -296,8 +300,9 @@ sdb_unixsock_client_recv(sdb_unixsock_client_t *client, if (! buffer) { if (! feof(client->fh)) { char errbuf[1024]; - fprintf(stderr, "unixsock: Failed to read from socket (%s): %s\n", - client->path, sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_error_set(SDB_LOG_ERR, "unixsock: Failed to read " + "from socket (%s): %s\n", client->path, + sdb_strerror(errno, errbuf, sizeof(errbuf))); } return buffer; } @@ -336,9 +341,9 @@ sdb_unixsock_client_process_lines(sdb_unixsock_client_t *client, types[i] = va_arg(ap, int); if ((types[i] < 1) || (types[i] > SDB_TYPE_BINARY)) { - fprintf(stderr, "unixsock: Unknown column type %i while " - "processing response from the UNIX socket @ %s.\n", - types[i], client->path); + sdb_error_set(SDB_LOG_ERR, "unixsock: Unknown column " + "type %i while processing response from the " + "UNIX socket @ %s.\n", types[i], client->path); va_end(ap); free(types); return -1; @@ -369,10 +374,10 @@ sdb_unixsock_client_process_lines(sdb_unixsock_client_t *client, column_count = sdb_unixsock_get_column_count(line, delim); if ((n_cols >= 0) && (n_cols != column_count)) { - fprintf(stderr, "unixsock: number of columns (%i) does not " - "match the number of requested columns (%i) while " - "processing response from the UNIX socket @ %s: %s\n", - column_count, n_cols, client->path, line); + sdb_error_set(SDB_LOG_ERR, "unixsock: number of columns (%i) " + "does not match the number of requested columns (%i) " + "while processing response from the UNIX socket @ %s: " + "%s\n", column_count, n_cols, client->path, line); continue; } @@ -390,8 +395,8 @@ sdb_unixsock_client_process_lines(sdb_unixsock_client_t *client, || ((max_lines < 0) && (! sdb_unixsock_client_eof(client))) || sdb_unixsock_client_error(client)) { char errbuf[1024]; - fprintf(stderr, "unixsock: Unexpected end of data while reading " - "from socket (%s): %s\n", client->path, + sdb_error_set(SDB_LOG_ERR, "unixsock: Unexpected end of data while " + "reading from socket (%s): %s\n", client->path, sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } -- 2.30.2