comparison base/Debug.h @ 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 8f076d02569a
children 166d7a4c2cd6
comparison
equal deleted inserted replaced
1274:6974bd4efdb5 1275:ad2d3e0a8b7c
41 SVDebug(); 41 SVDebug();
42 ~SVDebug(); 42 ~SVDebug();
43 43
44 template <typename T> 44 template <typename T>
45 inline SVDebug &operator<<(const T &t) { 45 inline SVDebug &operator<<(const T &t) {
46 if (m_silenced) return *this;
46 if (m_ok) { 47 if (m_ok) {
47 if (m_eol) { 48 if (m_eol) {
48 m_stream << m_prefix << " "; 49 m_stream << m_prefix << " ";
49 } 50 }
50 m_stream << t; 51 m_stream << t;
52 } 53 }
53 return *this; 54 return *this;
54 } 55 }
55 56
56 inline SVDebug &operator<<(QTextStreamFunction) { 57 inline SVDebug &operator<<(QTextStreamFunction) {
58 if (m_silenced) return *this;
57 m_stream << std::endl; 59 m_stream << std::endl;
58 m_eol = true; 60 m_eol = true;
59 return *this; 61 return *this;
60 } 62 }
61 63
64 static void silence() { m_silenced = true; }
65
62 private: 66 private:
63 std::fstream m_stream; 67 std::fstream m_stream;
64 char *m_prefix; 68 char *m_prefix;
65 bool m_ok; 69 bool m_ok;
66 bool m_eol; 70 bool m_eol;
71 static bool m_silenced;
72 };
73
74 class SVCerr {
75 public:
76 SVCerr(SVDebug &d) : m_d(d) { }
77
78 template <typename T>
79 inline SVCerr &operator<<(const T &t) {
80 if (m_silenced) return *this;
81 m_d << t;
82 cerr << t;
83 return *this;
84 }
85
86 inline SVCerr &operator<<(QTextStreamFunction f) {
87 if (m_silenced) return *this;
88 m_d << f;
89 cerr << std::endl;
90 return *this;
91 }
92
93 static void silence() { m_silenced = true; }
94
95 private:
96 SVDebug &m_d;
97 static bool m_silenced;
67 }; 98 };
68 99
69 extern SVDebug &getSVDebug(); 100 extern SVDebug &getSVDebug();
101 extern SVCerr &getSVCerr();
70 102
103 // Writes to debug log only
71 #define SVDEBUG getSVDebug() 104 #define SVDEBUG getSVDebug()
105
106 // Writes to both SVDEBUG and cerr
107 #define SVCERR getSVCerr()
72 108
73 #endif /* !_DEBUG_H_ */ 109 #endif /* !_DEBUG_H_ */
74 110