comparison src/main.cpp @ 128:d77779789e76

Improve logging.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Aug 2017 13:41:01 +0300
parents b51cee929416
children f6685c2eb75d
comparison
equal deleted inserted replaced
127:7db32f6c1476 128:d77779789e76
21 // Global UI scale factor 21 // Global UI scale factor
22 double setScale; 22 double setScale;
23 23
24 24
25 // 25 //
26 // Convert QString to a double value, replacing comma
27 //
28 double slMoneyStrToValue(const QString &str)
29 {
30 QString str2 = str;
31 return str2.replace(",", ".").toDouble();
32 }
33
34
35 //
36 // Convert double value to formatted QString
37 //
38 QString slMoneyValueToStr(double val)
39 {
40 return QStringLiteral("%1").arg(val, 1, 'f', 2);
41 }
42
43
44 //
45 // Trim and cleanup given QString (removing double whitespace etc.)
46 //
47 QString slCleanupStr(const QString &str)
48 {
49 return str.simplified().trimmed();
50 }
51
52
53 //
54 // Manipulate given QDateTime value to get desired
55 // correct timestamp.
56 //
57 const QDateTime slDateTimeToLocal(const QDateTime &val)
58 {
59 QDateTime tmp = val;
60 tmp.setOffsetFromUtc(0);
61 return tmp.toLocalTime();
62 }
63
64
65 //
66 // Return a string representation of given QDateTime
67 // converted to local time.
68 //
69 const QString slDateTimeToStr(const QDateTime &val)
70 {
71 return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm"));
72 }
73
74
75 //
26 // Error logging 76 // Error logging
27 // 77 //
28 void slLog(QString msg) 78 void slLog(QString msg)
29 { 79 {
30 QString filename = qApp->applicationDirPath() + QDir::separator() + APP_LOG_FILE; 80 QString filename = qApp->applicationDirPath() + QDir::separator() + APP_LOG_FILE;
31 QFile fh(filename); 81 QFile fh(filename);
32 if (fh.open(QIODevice::WriteOnly | QIODevice::Append)) 82 if (fh.open(QIODevice::WriteOnly | QIODevice::Append))
33 { 83 {
34 QTextStream out(&fh); 84 QTextStream out(&fh);
35 out << msg << "\n"; 85 out <<
86 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
87 << " : " << msg << "\n";
36 fh.close(); 88 fh.close();
37 } 89 }
38 } 90 }
39 91
40 92
53 dlg.setIcon(QMessageBox::Critical); 105 dlg.setIcon(QMessageBox::Critical);
54 dlg.setStandardButtons(QMessageBox::Ok); 106 dlg.setStandardButtons(QMessageBox::Ok);
55 dlg.setDefaultButton(QMessageBox::Ok); 107 dlg.setDefaultButton(QMessageBox::Ok);
56 108
57 return dlg.exec(); 109 return dlg.exec();
58 }
59
60
61 //
62 // Convert QString to a double value, replacing comma
63 //
64 double slMoneyStrToValue(const QString &str)
65 {
66 QString str2 = str;
67 return str2.replace(",", ".").toDouble();
68 }
69
70
71 //
72 // Convert double value to formatted QString
73 //
74 QString slMoneyValueToStr(double val)
75 {
76 return QStringLiteral("%1").arg(val, 1, 'f', 2);
77 }
78
79
80 //
81 // Trim and cleanup given QString (removing double whitespace etc.)
82 //
83 QString slCleanupStr(const QString &str)
84 {
85 return str.simplified().trimmed();
86 }
87
88
89 //
90 // Manipulate given QDateTime value to get desired
91 // correct timestamp.
92 //
93 const QDateTime slDateTimeToLocal(const QDateTime &val)
94 {
95 QDateTime tmp = val;
96 tmp.setOffsetFromUtc(0);
97 return tmp.toLocalTime();
98 }
99
100
101 //
102 // Return a string representation of given QDateTime
103 // converted to local time.
104 //
105 const QString slDateTimeToStr(const QDateTime &val)
106 {
107 return slDateTimeToLocal(val).toString(QStringLiteral("yyyy-MM-dd hh:mm"));
108 } 110 }
109 111
110 112
111 // 113 //
112 // Check if an SQL error has occured (for given QSqlError) and 114 // Check if an SQL error has occured (for given QSqlError) and