diff --git a/gb.db.mysql/src/main.c b/gb.db.mysql/src/main.c index 78dec8d55..f2f86fc2f 100644 --- a/gb.db.mysql/src/main.c +++ b/gb.db.mysql/src/main.c @@ -481,8 +481,7 @@ static void check_connection(MYSQL *conn) if (mysql_thread_id(conn) != thread_id) { - if (DB.IsDebug()) - fprintf(stderr, "gb.db.mysql: connection lost\n"); + DB.Debug("gb.db.mysql", "connection lost\n"); // Connection has been reestablished, set utf8 again mysql_query(conn, "set names 'utf8'"); } @@ -512,8 +511,7 @@ static int do_query(DB_DATABASE *db, const char *error, MYSQL_RES **pres, const else query = qtemp; - if (DB.IsDebug()) - fprintf(stderr, "gb.db.mysql: %p: %s\n", conn, query); + DB.Debug("gb.db.mysql", "%p: %s", conn, query); check_connection(conn); diff --git a/gb.db.odbc/src/main.c b/gb.db.odbc/src/main.c index baa7f1936..57e72e1f8 100644 --- a/gb.db.odbc/src/main.c +++ b/gb.db.odbc/src/main.c @@ -136,15 +136,15 @@ void reportODBCError(const char *fn, SQLSMALLINT len; SQLRETURN ret; - if(DB.IsDebug()) + if (DB.IsDebug()) { - fprintf(stderr, "gb.db.odbc: %s\n", fn); + DB.Debug("gb.db.odbc", fn); do { ret = SQLGetDiagRec(type, handle, ++i, state, &native, text, sizeof(text), &len); if (SQL_SUCCEEDED(ret)) - fprintf(stderr, "gb.db.odbc: %d:%s:%d:%s\n", (int)i, (char *)state, (int)native, (char *)text); + DB.Debug("gb.db.odbc", "%d:%s:%d:%s", (int)i, (char *)state, (int)native, (char *)text); } while (ret == SQL_SUCCESS); } @@ -221,10 +221,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable) //Make sure the statement has a cursor if (!(stmtHandle && (cursorScrollable == SQL_TRUE))) { - if (DB.IsDebug()) - { - fprintf(stderr, "gb.db.odbc: Cannot do GetRecordCount()!\n"); - } + DB.Debug("gb.db.odbc", "cannot do GetRecordCount()!"); return ((int) myRecCnt); } @@ -250,10 +247,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable) //Make sure the statement has a cursor if (formerRecIdx < 0) { - if (DB.IsDebug()) - { - fprintf(stderr, "gb.db.odbc.GetRecordCount: Current record returned %d, returning -1 as count.\n", formerRecIdx); - } + DB.Debug("gb.db.odbc", "GetRecordCount: Current record returned %d, returning -1 as count", formerRecIdx); return ((int) myRecCnt); } @@ -270,10 +264,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable) if (SQL_SUCCEEDED(retcode)) { //Inform first recno if in Debug mode and carry on - if (DB.IsDebug()) - { - fprintf(stderr, "gb.db.odbc.GetRecordCount: First recno=%d\n", (int) firstRecNo); - } + DB.Debug("gb.db.odbc", "GetRecordCount: First recno=%d", (int) firstRecNo); } else { //Could not fetch the first recno: Abort! reportODBCError("SQLFetchScroll SQL_ATTR_ROW_NUMBER (first recno)", stmtHandle, SQL_HANDLE_STMT); @@ -292,11 +283,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable) if (SQL_SUCCEEDED(retcode)) { //Set ret value - if (DB.IsDebug()) - { - fprintf(stderr, "gb.db.odbc.GetRecordCount: Last recno=%d\n", (int) lastRecNo); - } - + DB.Debug("gb.db.odbc", "GetRecordCount: Last recno=%d", (int) lastRecNo); } else { reportODBCError("SQLGetStmtAttr SQL_ATTR_ROW_NUMBER (last recno)", stmtHandle, SQL_HANDLE_STMT); } @@ -325,10 +312,7 @@ int GetRecordCount(SQLHANDLE stmtHandle, SQLINTEGER cursorScrollable) } myRecCnt = (lastRecNo - firstRecNo + 1); - if (DB.IsDebug()) - { - fprintf(stderr, "gb.db.odbc.GetRecordCount: Record count=%d\n", (int) myRecCnt); - } + DB.Debug("gb.db.odbc", "GetRecordCount: Record count=%d", (int) myRecCnt); return ((int) myRecCnt); @@ -704,15 +688,10 @@ void GetConnectedDBName(DB_DESC *desc, ODBC_CONN *odbc) free(dbName); } - if (DB.IsDebug()) - { - if (desc->name) - { - fprintf(stderr, "gb.db.odbc.GetConnectedDBName: desc->name (%d chars):'%s'.\n", (int)charsNeeded, desc->name); - } else { - fprintf(stderr, "gb.db.odbc.GetConnectedDBName: desc->name: NULL.\n"); - } - } + if (desc->name) + DB.Debug("gb.db.odbc", "GetConnectedDBName: desc->name (%d chars): '%s'", (int)charsNeeded, desc->name); + else + DB.Debug("gb.db.odbc", "GetConnectedDBName: desc->name: NULL"); } @@ -1091,8 +1070,8 @@ fflush(stderr); else query = qtemp; - if (DB.IsDebug()) - fprintf(stderr, "gb.db.odbc.do_query: res %p, dbc handle %p, query '%s'\n", res, handle, query); + //DB.Debug("gb.db.odbc", "do_query: res %p, dbc handle %p, query '%s'", res, handle, query); + DB.Debug("gb.db.odbc", "%p: %s", handle, query); GB.AllocZero(POINTER(&odbcres), sizeof(ODBC_RESULT)); @@ -1118,8 +1097,7 @@ fflush(stderr); retcode = SQLExecDirect(odbcres->odbcStatHandle, (SQLCHAR *) query, SQL_NTS); if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_NO_DATA)) { - if (DB.IsDebug()) - fprintf(stderr, "gb.db.odbc.do_query: SQLExecDirect() returned code %d\n", (int)retcode); + DB.Debug("gb.db.odbc", "do_query: SQLExecDirect() returned code %d", (int)retcode); throwODBCError("SQLExecDirect", odbcres->odbcStatHandle, SQL_HANDLE_STMT); SQLFreeHandle(SQL_HANDLE_STMT, odbcres->odbcStatHandle); //GB.Error("Error while executing the statement"); diff --git a/gb.db.postgresql/src/main.c b/gb.db.postgresql/src/main.c index 50a349c93..ce0cbcad7 100644 --- a/gb.db.postgresql/src/main.c +++ b/gb.db.postgresql/src/main.c @@ -551,11 +551,7 @@ static int do_query(DB_DATABASE *db, const char *error, PGresult **pres, const c else query = qtemp; - if (DB.IsDebug()) - { - fprintf(stderr, "gb.db.postgresql: %p: %s\n", conn, query); - fflush(stderr); - } + DB.Debug("gb.db.postgresql", "%p: %s", conn, query); res = PQexec(conn, query); ret = check_result(res, error); diff --git a/gb.db.sqlite2/src/main.cpp b/gb.db.sqlite2/src/main.cpp index cc21bc48b..0ffa7ad52 100644 --- a/gb.db.sqlite2/src/main.cpp +++ b/gb.db.sqlite2/src/main.cpp @@ -284,8 +284,7 @@ static int do_query(DB_DATABASE *db, const char *error, Dataset **pres, _print_query = FALSE; } - if (DB.IsDebug()) - fprintf(stderr, "sqlite2: %p: %s\n", conn, query); + DB.Debug("sqlite2","%p: %s", conn, query); if (strncasecmp("select",query,6) == 0){ diff --git a/gb.db.sqlite3/src/main.c b/gb.db.sqlite3/src/main.c index 7686ea96b..449e8226b 100644 --- a/gb.db.sqlite3/src/main.c +++ b/gb.db.sqlite3/src/main.c @@ -265,8 +265,7 @@ static int do_query(DB_DATABASE *db, const char *error, SQLITE_RESULT **pres, co _print_query = FALSE; } - if (DB.IsDebug()) - fprintf(stderr, "gb.db.sqlite3: %p: %s\n", conn, query); + DB.Debug("gb.db.sqlite3","%p: %s", conn, query); if (db->timeout > 0) max_retry = db->timeout * 5; @@ -2343,8 +2342,7 @@ static int database_create(DB_DATABASE *db, const char *name) _CREATE_DATABASE: - if (DB.IsDebug()) - fprintf(stderr, "sqlite3: create database: %s\n", fullpath); + DB.Debug("gb.db.sqlite3", "create database: %s", fullpath); conn = sqlite_open_database(fullpath, host); GB.FreeString(&fullpath); diff --git a/main/gbx/gbx_local.c b/main/gbx/gbx_local.c index 6bb0d2411..34a801fc1 100644 --- a/main/gbx/gbx_local.c +++ b/main/gbx/gbx_local.c @@ -1762,4 +1762,3 @@ void LOCAL_set_first_day_of_week(char day) if (day >= -1 && day <= 6) LOCAL_first_day_of_week = day; } - diff --git a/main/lib/db/gb.db.h b/main/lib/db/gb.db.h index cdad5cc24..62f6d6356 100644 --- a/main/lib/db/gb.db.h +++ b/main/lib/db/gb.db.h @@ -42,7 +42,7 @@ typedef /* LIMIT position */ -#define DB_LIMIT_NONE 0 +#define DB_LIMIT_NONE 0 #define DB_LIMIT_AT_BEGIN 1 #define DB_LIMIT_AT_END 2 @@ -225,7 +225,8 @@ typedef void (*Register)(DB_DRIVER *); void (*Format)(DB_DRIVER *, GB_VALUE *, DB_FORMAT_CALLBACK); void (*FormatVariant)(DB_DRIVER *, GB_VARIANT_VALUE *, DB_FORMAT_CALLBACK); - int (*IsDebug)(void); + bool (*IsDebug)(); + void (*Debug)(const char *, const char *, ...); void (*TryAnother)(const char *); char *(*SubstString)(const char *, int, DB_SUBST_CALLBACK); char *(*QuoteString)(const char *, int, char); diff --git a/main/lib/db/main.c b/main/lib/db/main.c index 1d071b570..15997c2b0 100644 --- a/main/lib/db/main.c +++ b/main/lib/db/main.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "gb_common.h" @@ -466,6 +467,34 @@ int DB_IsDebug(void) return _debug; } +void DB_Debug(const char *prefix, const char *msg, ...) +{ + va_list args; + struct timeval tv; + GB_DATE_SERIAL *date; + GB_DATE val; + + if (!_debug) + return; + + if (gettimeofday(&tv, NULL) == 0) + { + GB.MakeDateFromTime((time_t)tv.tv_sec, tv.tv_usec, &val); + date = GB.SplitDate(&val); + fprintf(stderr, "%04d-%02d-%02d %02d:%02d:%02d.%03d ", date->year, date->month, date->day, date->hour, date->min, date->sec, date->msec); + } + + fprintf(stderr, "%s: ", prefix); + + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + + fputc('\n', stderr); + fflush(stderr); +} + + static char *_quote; DB_SUBST_CALLBACK _quote_cb; @@ -660,6 +689,7 @@ void *GB_DB_1[] EXPORT = { (void *)DB_Format, (void *)DB_FormatVariant, (void *)DB_IsDebug, + (void *)DB_Debug, (void *)DB_TryAnother, (void *)DB_SubstString, (void *)DB_QuoteString, diff --git a/main/lib/db/main.h b/main/lib/db/main.h index 3acbdefac..01f1f9f70 100644 --- a/main/lib/db/main.h +++ b/main/lib/db/main.h @@ -51,6 +51,7 @@ GB_ARRAY DB_StringArrayToGambasArray(char **array); int DB_FindStringArray(char **array, const char *elt); void DB_SetDebug(int debug); int DB_IsDebug(void); +void DB_Debug(const char *prefix, const char *msg, ...); void DB_TryAnother(const char *); void q_init(void);