# HG changeset patch # User Chris Cannam # Date 1479832757 0 # Node ID ad2d3e0a8b7c8a59f45efe1dfe4ab8925b2d4788 # Parent 6974bd4efdb5521eb0d9b61965398a2ded34ab21 Add SVCERR and the ability to silence debug output (giving Sonic Annotator a --quiet mode) diff -r 6974bd4efdb5 -r ad2d3e0a8b7c base/Debug.cpp --- a/base/Debug.cpp Mon Nov 21 17:08:02 2016 +0000 +++ b/base/Debug.cpp Tue Nov 22 16:39:17 2016 +0000 @@ -23,23 +23,41 @@ #include -static SVDebug *debug = 0; +static SVDebug *svdebug = 0; +static SVCerr *svcerr = 0; static QMutex mutex; SVDebug &getSVDebug() { mutex.lock(); - if (!debug) { - debug = new SVDebug(); + if (!svdebug) { + svdebug = new SVDebug(); } mutex.unlock(); - return *debug; + return *svdebug; } +SVCerr &getSVCerr() { + mutex.lock(); + if (!svcerr) { + if (!svdebug) { + svdebug = new SVDebug(); + } + svcerr = new SVCerr(*svdebug); + } + mutex.unlock(); + return *svcerr; +} + +bool SVDebug::m_silenced = false; +bool SVCerr::m_silenced = false; + SVDebug::SVDebug() : m_prefix(0), m_ok(false), - m_eol(false) + m_eol(true) { + if (m_silenced) return; + if (qApp->applicationName() == "") { cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl; throw std::logic_error("Can't use SVDEBUG before setting application name"); @@ -71,7 +89,7 @@ SVDebug::~SVDebug() { - m_stream.close(); + if (m_stream) m_stream.close(); } QDebug & diff -r 6974bd4efdb5 -r ad2d3e0a8b7c base/Debug.h --- a/base/Debug.h Mon Nov 21 17:08:02 2016 +0000 +++ b/base/Debug.h Tue Nov 22 16:39:17 2016 +0000 @@ -43,6 +43,7 @@ template inline SVDebug &operator<<(const T &t) { + if (m_silenced) return *this; if (m_ok) { if (m_eol) { m_stream << m_prefix << " "; @@ -54,21 +55,56 @@ } inline SVDebug &operator<<(QTextStreamFunction) { + if (m_silenced) return *this; m_stream << std::endl; m_eol = true; return *this; } + static void silence() { m_silenced = true; } + private: std::fstream m_stream; char *m_prefix; bool m_ok; bool m_eol; + static bool m_silenced; +}; + +class SVCerr { +public: + SVCerr(SVDebug &d) : m_d(d) { } + + template + inline SVCerr &operator<<(const T &t) { + if (m_silenced) return *this; + m_d << t; + cerr << t; + return *this; + } + + inline SVCerr &operator<<(QTextStreamFunction f) { + if (m_silenced) return *this; + m_d << f; + cerr << std::endl; + return *this; + } + + static void silence() { m_silenced = true; } + +private: + SVDebug &m_d; + static bool m_silenced; }; extern SVDebug &getSVDebug(); +extern SVCerr &getSVCerr(); +// Writes to debug log only #define SVDEBUG getSVDebug() +// Writes to both SVDEBUG and cerr +#define SVCERR getSVCerr() + #endif /* !_DEBUG_H_ */