X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Futils%2Fdbi.c;h=26926183e4528a71dd0d495c10377d0c6adbfa67;hp=380a841321a1c6507f0c6f07a7f322e2d3e8ba6b;hb=ddb933096618a6bceded29e4dc2b37cb72134366;hpb=1905dca54fb53caed8171da2fcb842afc9f33216 diff --git a/src/utils/dbi.c b/src/utils/dbi.c index 380a841..2692618 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -26,6 +26,7 @@ */ #include "utils/dbi.h" +#include "utils/error.h" #include @@ -101,8 +102,8 @@ sdb_dbi_get_field(dbi_result res, unsigned int i, } break; default: - fprintf(stderr, "dbi: Unexpected type %i while " - "parsing query result.\n", type); + sdb_log(SDB_LOG_ERR, "dbi: Unexpected type %i while " + "parsing query result.", 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_log(SDB_LOG_ERR, "dbi: failed to fetch data: %s", sdb_dbi_strerror(client->conn)); return -1; } @@ -141,7 +142,7 @@ 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", + sdb_log(SDB_LOG_ERR, "dbi: Failed to retrieve row %llu: %s", n, sdb_dbi_strerror(client->conn)); continue; } @@ -288,21 +289,24 @@ 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_set("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_chomp(); + sdb_error_log(SDB_LOG_ERR); return -1; } client->conn = dbi_conn_open(driver); if (! client->conn) { - fprintf(stderr, "dbi: failed to open connection object.\n"); + sdb_log(SDB_LOG_ERR, "dbi: failed to open connection " + "object."); return -1; } @@ -316,14 +320,16 @@ sdb_dbi_client_connect(sdb_dbi_client_t *client) continue; /* else: error */ - fprintf(stderr, "dbi: failed to set option '%s': %s\n", + sdb_error_set("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_chomp(); + sdb_error_log(SDB_LOG_ERR); dbi_conn_close(client->conn); return -1; @@ -331,14 +337,14 @@ 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_log(SDB_LOG_ERR, "dbi: failed to set option 'dbname': %s", 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", + sdb_log(SDB_LOG_ERR, "dbi: failed to connect to database '%s': %s", client->database, sdb_dbi_strerror(client->conn)); dbi_conn_close(client->conn); return -1; @@ -374,14 +380,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_log(SDB_LOG_ERR, "dbi: failed to execute query '%s': %s", 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_log(SDB_LOG_ERR, "dbi: failed to fetch rows for query " + "'%s': %s", query, sdb_dbi_strerror(client->conn)); dbi_result_free(res); return -1; } @@ -398,9 +404,9 @@ 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) " - "for query '%s'.\n", num_fields, n, query); + sdb_log(SDB_LOG_ERR, "dbi: number of returned fields (%i) " + "does not match the number of requested fields (%i) " + "for query '%s'.", num_fields, n, query); dbi_result_free(res); return -1; } @@ -418,8 +424,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_log(SDB_LOG_ERR, "dbi: type of column '%s' (%u) " + "does not match requested type (%u).", dbi_result_get_field_name(res, (unsigned int)i + 1), field_type, type); status = -1;