changeset 1275:ad2d3e0a8b7c 3.0-integration

Add SVCERR and the ability to silence debug output (giving Sonic Annotator a --quiet mode)
author Chris Cannam
date Tue, 22 Nov 2016 16:39:17 +0000
parents 6974bd4efdb5
children adbd16d2c1e8
files base/Debug.cpp base/Debug.h
diffstat 2 files changed, 60 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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 <stdexcept>
 
-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 &
--- 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 <typename T>
     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 <typename T>
+    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_ */