comparison base/Debug.cpp @ 1864:166d7a4c2cd6 startup-timing

Add timings to debug output
author Chris Cannam
date Wed, 03 Jun 2020 13:57:37 +0100
parents 07a8793a0388
children 38be2fa29efd
comparison
equal deleted inserted replaced
1859:4c5736a517e0 1864:166d7a4c2cd6
21 #include <QUrl> 21 #include <QUrl>
22 #include <QCoreApplication> 22 #include <QCoreApplication>
23 #include <QDateTime> 23 #include <QDateTime>
24 24
25 #include <stdexcept> 25 #include <stdexcept>
26 #include <memory>
26 27
27 static SVDebug *svdebug = nullptr; 28 static std::unique_ptr<SVDebug> svdebug = nullptr;
28 static SVCerr *svcerr = nullptr; 29 static std::unique_ptr<SVCerr> svcerr = nullptr;
29 static QMutex mutex; 30 static QMutex mutex;
30 31
31 SVDebug &getSVDebug() { 32 SVDebug &getSVDebug() {
32 mutex.lock(); 33 mutex.lock();
33 if (!svdebug) { 34 if (!svdebug) {
34 svdebug = new SVDebug(); 35 svdebug = std::make_unique<SVDebug>();
35 } 36 }
36 mutex.unlock(); 37 mutex.unlock();
37 return *svdebug; 38 return *svdebug;
38 } 39 }
39 40
40 SVCerr &getSVCerr() { 41 SVCerr &getSVCerr() {
41 mutex.lock(); 42 mutex.lock();
42 if (!svcerr) { 43 if (!svcerr) {
43 if (!svdebug) { 44 if (!svdebug) {
44 svdebug = new SVDebug(); 45 svdebug = std::make_unique<SVDebug>();
45 } 46 }
46 svcerr = new SVCerr(*svdebug); 47 svcerr = std::make_unique<SVCerr>(*svdebug);
47 } 48 }
48 mutex.unlock(); 49 mutex.unlock();
49 return *svcerr; 50 return *svcerr;
50 } 51 }
51 52
56 m_prefix(nullptr), 57 m_prefix(nullptr),
57 m_ok(false), 58 m_ok(false),
58 m_eol(true) 59 m_eol(true)
59 { 60 {
60 if (m_silenced) return; 61 if (m_silenced) return;
62
63 m_timer.start();
61 64
62 if (qApp->applicationName() == "") { 65 if (qApp->applicationName() == "") {
63 cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl; 66 cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl;
64 throw std::logic_error("Can't use SVDEBUG before setting application name"); 67 throw std::logic_error("Can't use SVDEBUG before setting application name");
65 } 68 }
90 } 93 }
91 } 94 }
92 95
93 SVDebug::~SVDebug() 96 SVDebug::~SVDebug()
94 { 97 {
95 if (m_stream) m_stream.close(); 98 if (m_stream) {
99 (*this) << "Debug log ends" << endl;
100 m_stream.close();
101 }
96 } 102 }
97 103
98 QDebug & 104 QDebug &
99 operator<<(QDebug &dbg, const std::string &s) 105 operator<<(QDebug &dbg, const std::string &s)
100 { 106 {
103 } 109 }
104 110
105 std::ostream & 111 std::ostream &
106 operator<<(std::ostream &target, const QString &str) 112 operator<<(std::ostream &target, const QString &str)
107 { 113 {
108 return target << str.toStdString(); 114 return target << str.toUtf8().data();
109 } 115 }
110 116
111 std::ostream & 117 std::ostream &
112 operator<<(std::ostream &target, const QUrl &u) 118 operator<<(std::ostream &target, const QUrl &u)
113 { 119 {
114 return target << "<" << u.toString().toStdString() << ">"; 120 return target << "<" << u.toString() << ">";
115 } 121 }
116 122