# HG changeset patch # User Matti Hamalainen # Date 1503483747 -10800 # Node ID b51cee929416ce93fac337f0f25358e8e44e34b6 # Parent 6e2d26e7a0b487b5eeb49e9754fd3dc54750c3a8 Add program debug logging. diff -r 6e2d26e7a0b4 -r b51cee929416 src/main.cpp --- a/src/main.cpp Mon Jul 10 13:56:04 2017 +0300 +++ b/src/main.cpp Wed Aug 23 13:22:27 2017 +0300 @@ -23,12 +23,30 @@ // +// Error logging +// +void slLog(QString msg) +{ + QString filename = qApp->applicationDirPath() + QDir::separator() + APP_LOG_FILE; + QFile fh(filename); + if (fh.open(QIODevice::WriteOnly | QIODevice::Append)) + { + QTextStream out(&fh); + out << msg << "\n"; + fh.close(); + } +} + + +// // Display an error dialog with given title and message // int slErrorMsg(QString title, QString msg) { QMessageBox dlg; + slLog("ERROR: "+ msg); + dlg.setText(title); dlg.setInformativeText(msg); dlg.setTextFormat(Qt::RichText); @@ -95,23 +113,28 @@ // report it to stdout if so. Return "false" if error has occured, // true otherwise. // -bool slCheckAndReportSQLError(const QString where, const QSqlError &err) +bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false) { if (err.isValid()) { - printf("SQL Error in %s: %s\n", - where.toUtf8().constData(), - err.text().toUtf8().constData()); + slLog( + QStringLiteral("SQL ERROR %1: %2"). + arg(where).arg(err.text())); return false; } else + { + if (report) + slLog(QStringLiteral("SQL OK %1").arg(where)); return true; + } } void PersonInfo::dump() { - printf("PersonInfo() #%lld '%s %s' (added=%s, updated=%s, balance %1.2f)\n#%s#\n", + printf( + "PersonInfo() #%lld '%s %s' (added=%s, updated=%s, balance %1.2f)\n#%s#\n", id, firstName.toUtf8().constData(), lastName.toUtf8().constData(), @@ -230,7 +253,7 @@ arg(SQL_LEN_LAST_NAME). arg(SQL_LEN_EXTRA_INFO)); - slCheckAndReportSQLError("CREATE TABLE people", query.lastError()); + slCheckAndReportSQLError("CREATE TABLE people", query.lastError(), true); } if (!db.tables().contains("transactions")) @@ -242,9 +265,11 @@ "value REAL, " "added DATETIME NOT NULL)")); - slCheckAndReportSQLError("CREATE TABLE transactions", query.lastError()); + slCheckAndReportSQLError("CREATE TABLE transactions", query.lastError(), true); } + query.finish(); + SyntilistaMainWindow swin; swin.show(); return sapp.exec(); @@ -365,6 +390,7 @@ // void SyntilistaMainWindow::statusMsg(const QString &msg) { + slLog(msg); ui->statusbar->showMessage(msg); } @@ -883,7 +909,6 @@ "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance," "updated FROM people"; - QSqlQuery query; QString queryOrderDir, queryOrderBy; // Sort order @@ -913,6 +938,7 @@ } // Are we filtering or not? + QSqlQuery query; if (peopleFilter != "") { // Filter by name(s) @@ -969,7 +995,7 @@ query.addBindValue(value); query.addBindValue(QDateTime::currentDateTimeUtc()); query.exec(); - if (!slCheckAndReportSQLError("addTransaction()", query.lastError())) + if (!slCheckAndReportSQLError(QStringLiteral("addTransaction(%1, %2)").arg(id).arg(value), query.lastError(), true)) { QSqlDatabase::database().rollback(); return -2; @@ -979,7 +1005,7 @@ query.addBindValue(QDateTime::currentDateTimeUtc()); query.addBindValue(id); query.exec(); - if (!slCheckAndReportSQLError("addTransaction update timestamp", query.lastError())) + if (!slCheckAndReportSQLError("addTransaction update timestamp", query.lastError(), true)) { QSqlDatabase::database().rollback(); return -3; @@ -1351,7 +1377,6 @@ return -1; QSqlDatabase::database().commit(); - updateModel(); return 0; } diff -r 6e2d26e7a0b4 -r b51cee929416 src/main.h --- a/src/main.h Mon Jul 10 13:56:04 2017 +0300 +++ b/src/main.h Wed Aug 23 13:22:27 2017 +0300 @@ -25,7 +25,7 @@ #define APP_ID "Kampus Syntilista" // Application ID (for settings) #define APP_NAME "Café Kampus Syntilista" // Application title/name #define APP_SQLITE_FILE "syntilista.sqlite3" // SQLite3 database file name (without path) - +#define APP_LOG_FILE "log.txt" // Application log file #define SQL_LEN_FIRST_NAME 128 #define SQL_LEN_LAST_NAME 128