From 0982eab98d7b8521ba67d809cd9f86fe25d382c1 Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Sun, 7 Jan 2018 22:32:24 +0200 Subject: [PATCH] error print cleanup --- src/cfg.c | 2 +- src/common.c | 2 +- src/dbaccess.c | 8 ++++---- src/dbsql.c | 22 +++++++++++++++------- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index 492227e..f475390 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -673,7 +673,7 @@ int opencfgfile(const char *cfgfile, FILE **fd) if (debug) printf("Config file: --config\n"); } else { - snprintf(errorstring, 1024, "Unable to open given config file \"%s\": %s\n", cfgfile, strerror(errno)); + snprintf(errorstring, 1024, "Unable to open given config file \"%s\": %s", cfgfile, strerror(errno)); printe(PT_Error); return 0; } diff --git a/src/common.c b/src/common.c index 112b406..4729dda 100644 --- a/src/common.c +++ b/src/common.c @@ -258,7 +258,7 @@ int isnumeric(const char *s) void panicexit(const char *sourcefile, const int sourceline) { - snprintf(errorstring, 1024, "Unexpected error (%s), exiting. (%s:%d)\n", strerror(errno), sourcefile, sourceline); + snprintf(errorstring, 1024, "Unexpected error (%s), exiting. (%s:%d)", strerror(errno), sourcefile, sourceline); fprintf(stderr, "%s\n", errorstring); printe(PT_Error); exit(EXIT_FAILURE); diff --git a/src/dbaccess.c b/src/dbaccess.c index eff5a61..1c0126e 100644 --- a/src/dbaccess.c +++ b/src/dbaccess.c @@ -377,9 +377,9 @@ int lockdb(int fd, int dbwrite) /* give up if lock can't be obtained */ if (locktry>=LOCKTRYLIMIT) { if (dbwrite) { - snprintf(errorstring, 1024, "Locking database file for write failed for %d tries:\n%s (%d)", locktry, strerror(errno), errno); + snprintf(errorstring, 1024, "Locking database file for write failed for %d tries: %s", locktry, strerror(errno)); } else { - snprintf(errorstring, 1024, "Locking database file for read failed for %d tries:\n%s (%d)", locktry, strerror(errno), errno); + snprintf(errorstring, 1024, "Locking database file for read failed for %d tries: %s", locktry, strerror(errno)); } printe(PT_Error); return 0; @@ -392,9 +392,9 @@ int lockdb(int fd, int dbwrite) /* real error */ } else { if (dbwrite) { - snprintf(errorstring, 1024, "Locking database file for write failed:\n%s (%d)", strerror(errno), errno); + snprintf(errorstring, 1024, "Locking database file for write failed: %s", strerror(errno)); } else { - snprintf(errorstring, 1024, "Locking database file for read failed:\n%s (%d)", strerror(errno), errno); + snprintf(errorstring, 1024, "Locking database file for read failed: %s", strerror(errno)); } printe(PT_Error); return 0; diff --git a/src/dbsql.c b/src/dbsql.c index 612849e..8cf3192 100644 --- a/src/dbsql.c +++ b/src/dbsql.c @@ -103,7 +103,7 @@ int db_setpragmas(void) rc = sqlite3_prepare_v2(db, "PRAGMA foreign_keys;", -1, &sqlstmt, NULL); if (rc != SQLITE_OK) { db_errcode = rc; - snprintf(errorstring, 1024, "Exec prepare \"PRAGMA foreign_keys;\" failed (%d): %s\n", rc, sqlite3_errmsg(db)); + snprintf(errorstring, 1024, "Exec prepare \"PRAGMA foreign_keys;\" failed (%d): %s", rc, sqlite3_errmsg(db)); printe(PT_Error); return 0; } @@ -112,7 +112,7 @@ int db_setpragmas(void) rc = sqlite3_step(sqlstmt); if (rc != SQLITE_ROW) { db_errcode = rc; - snprintf(errorstring, 1024, "PRAGMA foreign_keys returned no row (%d): %s\n", rc, sqlite3_errmsg(db)); + snprintf(errorstring, 1024, "PRAGMA foreign_keys returned no row (%d): %s", rc, sqlite3_errmsg(db)); printe(PT_Error); sqlite3_finalize(sqlstmt); return 0; @@ -121,7 +121,7 @@ int db_setpragmas(void) rc = sqlite3_finalize(sqlstmt); if (rc) { db_errcode = rc; - snprintf(errorstring, 1024, " Exec finalize \"PRAGMA foreign_keys;\" failed (%d): %s\n", rc, sqlite3_errmsg(db)); + snprintf(errorstring, 1024, " Exec finalize \"PRAGMA foreign_keys;\" failed (%d): %s", rc, sqlite3_errmsg(db)); printe(PT_Error); return 0; } @@ -902,7 +902,7 @@ void dbiflistfree(dbiflist **dbifl) int db_getdata(dbdatalist **dbdata, dbdatalistinfo *listinfo, const char *iface, const char *table, const uint32_t resultlimit) { - int ret = 1, i; + int ret = 1, i, rc; char *datatables[] = {"fiveminute", "hour", "day", "month", "year", "top"}; char sql[512], limit[64]; sqlite3_int64 ifaceid = 0; @@ -943,11 +943,18 @@ int db_getdata(dbdatalist **dbdata, dbdatalistinfo *listinfo, const char *iface, sqlite3_snprintf(512, sql, "select id, strftime('%%s', date, 'utc'), rx, tx from %s where interface=%"PRId64" order by date desc %s;", table, (int64_t)ifaceid, limit); } - if (sqlite3_prepare_v2(db, sql, -1, &sqlstmt, NULL) != SQLITE_OK) { + rc = sqlite3_prepare_v2(db, sql, -1, &sqlstmt, NULL); + if (rc != SQLITE_OK) { + db_errcode = rc; + snprintf(errorstring, 1024, "Get data prepare failed (%d: %s): \"%s\"", rc, sqlite3_errmsg(db), sql); + printe(PT_Error); return 0; } - if (sqlite3_column_count(sqlstmt) != 4) { + rc = sqlite3_column_count(sqlstmt); + if (rc != 4) { + snprintf(errorstring, 1024, "Get data returned unexpected column count %d instead of 4: \"%s\"", rc, sql); + printe(PT_Error); sqlite3_finalize(sqlstmt); return 0; } @@ -958,7 +965,8 @@ int db_getdata(dbdatalist **dbdata, dbdatalistinfo *listinfo, const char *iface, rx = (uint64_t)sqlite3_column_int64(sqlstmt, 2); tx = (uint64_t)sqlite3_column_int64(sqlstmt, 3); if (!dbdatalistadd(dbdata, rx, tx, timestamp, rowid)) { - /* TODO: some info print may be needed here */ + snprintf(errorstring, 1024, "Storing data for processing failed: %s", strerror(errno)); + printe(PT_Error); ret = 0; break; } -- 2.40.0