comparison src/main.cpp @ 133:df03ab8b6413

Improve logging.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Aug 2017 17:17:45 +0300
parents dc9fe580da42
children 478ce4c94f6b
comparison
equal deleted inserted replaced
132:dc9fe580da42 133:df03ab8b6413
74 74
75 75
76 // 76 //
77 // Error logging 77 // Error logging
78 // 78 //
79 void slLog(QString msg) 79 void slLog(const QString &mtype, const QString &msg)
80 { 80 {
81 QString filename = appDataPath + QDir::separator() + APP_LOG_FILE; 81 QString filename = appDataPath + QDir::separator() + APP_LOG_FILE;
82 QFile fh(filename); 82 QFile fh(filename);
83 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) 83 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
84 { 84 {
85 QTextStream out(&fh); 85 QTextStream out(&fh);
86 out << 86 out <<
87 slDateTimeToLocal(QDateTime::currentDateTimeUtc()). 87 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).
88 toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")) 88 toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
89 << " : " << msg << "\n"; 89 << " [" << mtype << "]: " << msg << "\n";
90 fh.close(); 90 fh.close();
91 } 91 }
92 } 92 }
93 93
94 94
97 // 97 //
98 int slErrorMsg(QString title, QString msg) 98 int slErrorMsg(QString title, QString msg)
99 { 99 {
100 QMessageBox dlg; 100 QMessageBox dlg;
101 101
102 slLog("ERROR: "+ msg); 102 slLog("ERROR", msg);
103 103
104 dlg.setText(title); 104 dlg.setText(title);
105 dlg.setInformativeText(msg); 105 dlg.setInformativeText(msg);
106 dlg.setTextFormat(Qt::RichText); 106 dlg.setTextFormat(Qt::RichText);
107 dlg.setIcon(QMessageBox::Critical); 107 dlg.setIcon(QMessageBox::Critical);
119 // 119 //
120 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false) 120 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false)
121 { 121 {
122 if (err.isValid()) 122 if (err.isValid())
123 { 123 {
124 slLog( 124 slLog("ERROR",
125 QStringLiteral("SQL ERROR %1: %2"). 125 QStringLiteral("SQL %1: %2").
126 arg(where).arg(err.text())); 126 arg(where).arg(err.text()));
127 return false; 127 return false;
128 } 128 }
129 else 129 else
130 { 130 {
131 if (report) 131 if (report)
132 slLog(QStringLiteral("SQL OK %1").arg(where)); 132 {
133 slLog("NOTE",
134 QStringLiteral("SQL OK %1").arg(where));
135 }
133 return true; 136 return true;
134 } 137 }
135 } 138 }
136 139
137 140
399 // 402 //
400 // Helper function for showing messages in the statusbar/line 403 // Helper function for showing messages in the statusbar/line
401 // 404 //
402 void SyntilistaMainWindow::statusMsg(const QString &msg) 405 void SyntilistaMainWindow::statusMsg(const QString &msg)
403 { 406 {
404 slLog(msg); 407 slLog("STATUS", msg);
405 ui->statusbar->showMessage(msg); 408 ui->statusbar->showMessage(msg);
406 } 409 }
407 410
408 411
409 // 412 //