comparison src/main.cpp @ 126:b51cee929416

Add program debug logging.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Aug 2017 13:22:27 +0300
parents 6e2d26e7a0b4
children d77779789e76
comparison
equal deleted inserted replaced
125:6e2d26e7a0b4 126:b51cee929416
21 // Global UI scale factor 21 // Global UI scale factor
22 double setScale; 22 double setScale;
23 23
24 24
25 // 25 //
26 // Error logging
27 //
28 void slLog(QString msg)
29 {
30 QString filename = qApp->applicationDirPath() + QDir::separator() + APP_LOG_FILE;
31 QFile fh(filename);
32 if (fh.open(QIODevice::WriteOnly | QIODevice::Append))
33 {
34 QTextStream out(&fh);
35 out << msg << "\n";
36 fh.close();
37 }
38 }
39
40
41 //
26 // Display an error dialog with given title and message 42 // Display an error dialog with given title and message
27 // 43 //
28 int slErrorMsg(QString title, QString msg) 44 int slErrorMsg(QString title, QString msg)
29 { 45 {
30 QMessageBox dlg; 46 QMessageBox dlg;
47
48 slLog("ERROR: "+ msg);
31 49
32 dlg.setText(title); 50 dlg.setText(title);
33 dlg.setInformativeText(msg); 51 dlg.setInformativeText(msg);
34 dlg.setTextFormat(Qt::RichText); 52 dlg.setTextFormat(Qt::RichText);
35 dlg.setIcon(QMessageBox::Critical); 53 dlg.setIcon(QMessageBox::Critical);
93 // 111 //
94 // Check if an SQL error has occured (for given QSqlError) and 112 // Check if an SQL error has occured (for given QSqlError) and
95 // report it to stdout if so. Return "false" if error has occured, 113 // report it to stdout if so. Return "false" if error has occured,
96 // true otherwise. 114 // true otherwise.
97 // 115 //
98 bool slCheckAndReportSQLError(const QString where, const QSqlError &err) 116 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false)
99 { 117 {
100 if (err.isValid()) 118 if (err.isValid())
101 { 119 {
102 printf("SQL Error in %s: %s\n", 120 slLog(
103 where.toUtf8().constData(), 121 QStringLiteral("SQL ERROR %1: %2").
104 err.text().toUtf8().constData()); 122 arg(where).arg(err.text()));
105 return false; 123 return false;
106 } 124 }
107 else 125 else
126 {
127 if (report)
128 slLog(QStringLiteral("SQL OK %1").arg(where));
108 return true; 129 return true;
130 }
109 } 131 }
110 132
111 133
112 void PersonInfo::dump() 134 void PersonInfo::dump()
113 { 135 {
114 printf("PersonInfo() #%lld '%s %s' (added=%s, updated=%s, balance %1.2f)\n#%s#\n", 136 printf(
137 "PersonInfo() #%lld '%s %s' (added=%s, updated=%s, balance %1.2f)\n#%s#\n",
115 id, 138 id,
116 firstName.toUtf8().constData(), 139 firstName.toUtf8().constData(),
117 lastName.toUtf8().constData(), 140 lastName.toUtf8().constData(),
118 slDateTimeToStr(added).toUtf8().constData(), 141 slDateTimeToStr(added).toUtf8().constData(),
119 slDateTimeToStr(updated).toUtf8().constData(), 142 slDateTimeToStr(updated).toUtf8().constData(),
228 "updated DATETIME NOT NULL)"). 251 "updated DATETIME NOT NULL)").
229 arg(SQL_LEN_FIRST_NAME). 252 arg(SQL_LEN_FIRST_NAME).
230 arg(SQL_LEN_LAST_NAME). 253 arg(SQL_LEN_LAST_NAME).
231 arg(SQL_LEN_EXTRA_INFO)); 254 arg(SQL_LEN_EXTRA_INFO));
232 255
233 slCheckAndReportSQLError("CREATE TABLE people", query.lastError()); 256 slCheckAndReportSQLError("CREATE TABLE people", query.lastError(), true);
234 } 257 }
235 258
236 if (!db.tables().contains("transactions")) 259 if (!db.tables().contains("transactions"))
237 { 260 {
238 query.exec(QStringLiteral( 261 query.exec(QStringLiteral(
240 "id INTEGER PRIMARY KEY, " 263 "id INTEGER PRIMARY KEY, "
241 "person INT NOT NULL, " 264 "person INT NOT NULL, "
242 "value REAL, " 265 "value REAL, "
243 "added DATETIME NOT NULL)")); 266 "added DATETIME NOT NULL)"));
244 267
245 slCheckAndReportSQLError("CREATE TABLE transactions", query.lastError()); 268 slCheckAndReportSQLError("CREATE TABLE transactions", query.lastError(), true);
246 } 269 }
270
271 query.finish();
247 272
248 SyntilistaMainWindow swin; 273 SyntilistaMainWindow swin;
249 swin.show(); 274 swin.show();
250 return sapp.exec(); 275 return sapp.exec();
251 } 276 }
363 // 388 //
364 // Helper function for showing messages in the statusbar/line 389 // Helper function for showing messages in the statusbar/line
365 // 390 //
366 void SyntilistaMainWindow::statusMsg(const QString &msg) 391 void SyntilistaMainWindow::statusMsg(const QString &msg)
367 { 392 {
393 slLog(msg);
368 ui->statusbar->showMessage(msg); 394 ui->statusbar->showMessage(msg);
369 } 395 }
370 396
371 397
372 // 398 //
881 static QString queryBase = 907 static QString queryBase =
882 "SELECT id,last_name,first_name," 908 "SELECT id,last_name,first_name,"
883 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance," 909 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance,"
884 "updated FROM people"; 910 "updated FROM people";
885 911
886 QSqlQuery query;
887 QString queryOrderDir, queryOrderBy; 912 QString queryOrderDir, queryOrderBy;
888 913
889 // Sort order 914 // Sort order
890 if (peopleSortOrder == Qt::AscendingOrder) 915 if (peopleSortOrder == Qt::AscendingOrder)
891 queryOrderDir = QStringLiteral("ASC"); 916 queryOrderDir = QStringLiteral("ASC");
911 default: 936 default:
912 queryOrderBy = ""; 937 queryOrderBy = "";
913 } 938 }
914 939
915 // Are we filtering or not? 940 // Are we filtering or not?
941 QSqlQuery query;
916 if (peopleFilter != "") 942 if (peopleFilter != "")
917 { 943 {
918 // Filter by name(s) 944 // Filter by name(s)
919 QString tmp = "%"+ peopleFilter +"%"; 945 QString tmp = "%"+ peopleFilter +"%";
920 query.prepare(queryBase +" WHERE first_name LIKE ? OR last_name LIKE ?" + queryOrderBy); 946 query.prepare(queryBase +" WHERE first_name LIKE ? OR last_name LIKE ?" + queryOrderBy);
967 query.prepare("INSERT INTO transactions (person,value,added) VALUES (?,?,?)"); 993 query.prepare("INSERT INTO transactions (person,value,added) VALUES (?,?,?)");
968 query.addBindValue(id); 994 query.addBindValue(id);
969 query.addBindValue(value); 995 query.addBindValue(value);
970 query.addBindValue(QDateTime::currentDateTimeUtc()); 996 query.addBindValue(QDateTime::currentDateTimeUtc());
971 query.exec(); 997 query.exec();
972 if (!slCheckAndReportSQLError("addTransaction()", query.lastError())) 998 if (!slCheckAndReportSQLError(QStringLiteral("addTransaction(%1, %2)").arg(id).arg(value), query.lastError(), true))
973 { 999 {
974 QSqlDatabase::database().rollback(); 1000 QSqlDatabase::database().rollback();
975 return -2; 1001 return -2;
976 } 1002 }
977 1003
978 query.prepare("UPDATE people SET updated=? WHERE id=?"); 1004 query.prepare("UPDATE people SET updated=? WHERE id=?");
979 query.addBindValue(QDateTime::currentDateTimeUtc()); 1005 query.addBindValue(QDateTime::currentDateTimeUtc());
980 query.addBindValue(id); 1006 query.addBindValue(id);
981 query.exec(); 1007 query.exec();
982 if (!slCheckAndReportSQLError("addTransaction update timestamp", query.lastError())) 1008 if (!slCheckAndReportSQLError("addTransaction update timestamp", query.lastError(), true))
983 { 1009 {
984 QSqlDatabase::database().rollback(); 1010 QSqlDatabase::database().rollback();
985 return -3; 1011 return -3;
986 } 1012 }
987 1013
1349 1375
1350 if (!slCheckAndReportSQLError("PersonSQLModel::updatePerson()", np.lastError())) 1376 if (!slCheckAndReportSQLError("PersonSQLModel::updatePerson()", np.lastError()))
1351 return -1; 1377 return -1;
1352 1378
1353 QSqlDatabase::database().commit(); 1379 QSqlDatabase::database().commit();
1354
1355 updateModel(); 1380 updateModel();
1356 return 0; 1381 return 0;
1357 } 1382 }
1358 1383
1359 1384