Debug.h
Go to the documentation of this file.
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 #include <QElapsedTimer>
22 
23 #include "RealTime.h"
24 
25 #include <string>
26 #include <iostream>
27 #include <fstream>
28 
29 class QString;
30 class QUrl;
31 
32 QDebug &operator<<(QDebug &, const std::string &);
33 std::ostream &operator<<(std::ostream &, const QString &);
34 std::ostream &operator<<(std::ostream &, const QUrl &);
35 
36 using std::cout;
37 using std::cerr;
38 using std::endl;
39 
40 class SVDebug {
41 public:
42  SVDebug();
43  ~SVDebug();
44 
45  template <typename T>
46  inline SVDebug &operator<<(const T &t) {
47  if (m_silenced) return *this;
48  if (m_ok) {
49  if (m_eol) {
50  m_stream << m_prefix << "/" << m_timer.elapsed() << ": ";
51  }
52  m_stream << t;
53  m_eol = false;
54  }
55  return *this;
56  }
57 
58  inline SVDebug &operator<<(QTextStreamFunction) {
59  if (m_silenced) return *this;
60  m_stream << std::endl;
61  m_eol = true;
62  return *this;
63  }
64 
65  static void silence() { m_silenced = true; }
66 
67 private:
68  std::fstream m_stream;
69  char *m_prefix;
70  bool m_ok;
71  bool m_eol;
72  QElapsedTimer m_timer;
73  static bool m_silenced;
74 };
75 
76 class SVCerr {
77 public:
78  SVCerr(SVDebug &d) : m_d(d) { }
79 
80  template <typename T>
81  inline SVCerr &operator<<(const T &t) {
82  if (m_silenced) return *this;
83  m_d << t;
84  cerr << t;
85  return *this;
86  }
87 
88  inline SVCerr &operator<<(QTextStreamFunction f) {
89  if (m_silenced) return *this;
90  m_d << f;
91  cerr << std::endl;
92  return *this;
93  }
94 
95  static void silence() { m_silenced = true; }
96 
97 private:
99  static bool m_silenced;
100 };
101 
102 extern SVDebug &getSVDebug();
103 extern SVCerr &getSVCerr();
104 
105 // Writes to debug log only
106 #define SVDEBUG getSVDebug()
107 
108 // Writes to both SVDEBUG and cerr
109 #define SVCERR getSVCerr()
110 
111 #endif /* !_DEBUG_H_ */
112 
~SVDebug()
Definition: Debug.cpp:96
SVCerr & operator<<(QTextStreamFunction f)
Definition: Debug.h:88
SVCerr & getSVCerr()
Definition: Debug.cpp:41
static void silence()
Definition: Debug.h:65
SVDebug & m_d
Definition: Debug.h:98
SVDebug & getSVDebug()
Definition: Debug.cpp:32
bool m_ok
Definition: Debug.h:70
bool m_eol
Definition: Debug.h:71
QElapsedTimer m_timer
Definition: Debug.h:72
Definition: Debug.h:76
char * m_prefix
Definition: Debug.h:69
QDebug & operator<<(QDebug &, const std::string &)
Definition: Debug.cpp:105
std::fstream m_stream
Definition: Debug.h:68
static bool m_silenced
Definition: Debug.h:99
SVDebug()
Definition: Debug.cpp:56
SVDebug & operator<<(QTextStreamFunction)
Definition: Debug.h:58
static bool m_silenced
Definition: Debug.h:73
static void silence()
Definition: Debug.h:95
SVDebug & operator<<(const T &t)
Definition: Debug.h:46
Definition: Debug.h:40
SVCerr & operator<<(const T &t)
Definition: Debug.h:81
SVCerr(SVDebug &d)
Definition: Debug.h:78