1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Sonic Visualiser
5     An audio file viewer and annotation editor.
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2010-2011 Chris Cannam and QMUL.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_DEBUG_H
17 #define SV_DEBUG_H
18 
19 #include <QDebug>
20 #include <QTextStream>
21 
22 #include "RealTime.h"
23 
24 #include <string>
25 #include <iostream>
26 #include <fstream>
27 
28 class QString;
29 class QUrl;
30 
31 QDebug &operator<<(QDebug &, const std::string &);
32 std::ostream &operator<<(std::ostream &, const QString &);
33 std::ostream &operator<<(std::ostream &, const QUrl &);
34 
35 using std::cout;
36 using std::cerr;
37 using std::endl;
38 
39 class SVDebug {
40 public:
41     SVDebug();
42     ~SVDebug();
43 
44     template <typename T>
45     inline SVDebug &operator<<(const T &t) {
46         if (m_silenced) return *this;
47         if (m_ok) {
48             if (m_eol) {
49                 m_stream << m_prefix << " ";
50             }
51             m_stream << t;
52             m_eol = false;
53         }
54         return *this;
55     }
56 
57     inline SVDebug &operator<<(QTextStreamFunction) {
58         if (m_silenced) return *this;
59         m_stream << std::endl;
60         m_eol = true;
61         return *this;
62     }
63 
silence()64     static void silence() { m_silenced = true; }
65 
66 private:
67     std::fstream m_stream;
68     char *m_prefix;
69     bool m_ok;
70     bool m_eol;
71     static bool m_silenced;
72 };
73 
74 class SVCerr {
75 public:
SVCerr(SVDebug & d)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 
silence()93     static void silence() { m_silenced = true; }
94 
95 private:
96     SVDebug &m_d;
97     static bool m_silenced;
98 };
99 
100 extern SVDebug &getSVDebug();
101 extern SVCerr &getSVCerr();
102 
103 // Writes to debug log only
104 #define SVDEBUG getSVDebug()
105 
106 // Writes to both SVDEBUG and cerr
107 #define SVCERR getSVCerr()
108 
109 #endif /* !_DEBUG_H_ */
110 
111