Mercurial > hg > svcore
comparison base/Debug.cpp @ 1365:3382d914e110
Merge from branch 3.0-integration
author | Chris Cannam |
---|---|
date | Fri, 13 Jan 2017 10:29:44 +0000 |
parents | 838a45cff62d |
children | 04a198887a3d |
comparison
equal
deleted
inserted
replaced
1272:6a7ea3bd0e10 | 1365:3382d914e110 |
---|---|
19 #include <QMutex> | 19 #include <QMutex> |
20 #include <QDir> | 20 #include <QDir> |
21 #include <QUrl> | 21 #include <QUrl> |
22 #include <QCoreApplication> | 22 #include <QCoreApplication> |
23 | 23 |
24 #ifndef NDEBUG | 24 #include <stdexcept> |
25 | 25 |
26 static SVDebug *debug = 0; | 26 static SVDebug *svdebug = 0; |
27 static SVCerr *svcerr = 0; | |
27 static QMutex mutex; | 28 static QMutex mutex; |
28 | 29 |
29 SVDebug &getSVDebug() { | 30 SVDebug &getSVDebug() { |
30 mutex.lock(); | 31 mutex.lock(); |
31 if (!debug) { | 32 if (!svdebug) { |
32 debug = new SVDebug(); | 33 svdebug = new SVDebug(); |
33 } | 34 } |
34 mutex.unlock(); | 35 mutex.unlock(); |
35 return *debug; | 36 return *svdebug; |
36 } | 37 } |
38 | |
39 SVCerr &getSVCerr() { | |
40 mutex.lock(); | |
41 if (!svcerr) { | |
42 if (!svdebug) { | |
43 svdebug = new SVDebug(); | |
44 } | |
45 svcerr = new SVCerr(*svdebug); | |
46 } | |
47 mutex.unlock(); | |
48 return *svcerr; | |
49 } | |
50 | |
51 bool SVDebug::m_silenced = false; | |
52 bool SVCerr::m_silenced = false; | |
37 | 53 |
38 SVDebug::SVDebug() : | 54 SVDebug::SVDebug() : |
39 m_prefix(0), | 55 m_prefix(0), |
40 m_ok(false), | 56 m_ok(false), |
41 m_eol(false) | 57 m_eol(true) |
42 { | 58 { |
59 if (m_silenced) return; | |
60 | |
61 if (qApp->applicationName() == "") { | |
62 cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl; | |
63 throw std::logic_error("Can't use SVDEBUG before setting application name"); | |
64 } | |
65 | |
43 QString pfx = ResourceFinder().getUserResourcePrefix(); | 66 QString pfx = ResourceFinder().getUserResourcePrefix(); |
44 QDir logdir(QString("%1/%2").arg(pfx).arg("log")); | 67 QDir logdir(QString("%1/%2").arg(pfx).arg("log")); |
45 | 68 |
46 m_prefix = strdup(QString("[%1]") | 69 m_prefix = strdup(QString("[%1]") |
47 .arg(QCoreApplication::applicationPid()) | 70 .arg(QCoreApplication::applicationPid()) |
57 if (!m_stream) { | 80 if (!m_stream) { |
58 QDebug(QtWarningMsg) << (const char *)m_prefix | 81 QDebug(QtWarningMsg) << (const char *)m_prefix |
59 << "Failed to open debug log file " | 82 << "Failed to open debug log file " |
60 << fileName << " for writing"; | 83 << fileName << " for writing"; |
61 } else { | 84 } else { |
62 cerr << m_prefix << ": Log file is " << fileName << endl; | 85 // cerr << m_prefix << ": Log file is " << fileName << endl; |
63 m_ok = true; | 86 m_ok = true; |
64 } | 87 } |
65 } | 88 } |
66 | 89 |
67 SVDebug::~SVDebug() | 90 SVDebug::~SVDebug() |
68 { | 91 { |
69 m_stream.close(); | 92 if (m_stream) m_stream.close(); |
70 } | 93 } |
71 | 94 |
72 QDebug & | 95 QDebug & |
73 operator<<(QDebug &dbg, const std::string &s) | 96 operator<<(QDebug &dbg, const std::string &s) |
74 { | 97 { |
75 dbg << QString::fromUtf8(s.c_str()); | 98 dbg << QString::fromUtf8(s.c_str()); |
76 return dbg; | 99 return dbg; |
77 } | 100 } |
78 | |
79 #endif | |
80 | 101 |
81 std::ostream & | 102 std::ostream & |
82 operator<<(std::ostream &target, const QString &str) | 103 operator<<(std::ostream &target, const QString &str) |
83 { | 104 { |
84 return target << str.toStdString(); | 105 return target << str.toStdString(); |