Chris@685: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@685: Chris@685: /* Chris@685: Sonic Visualiser Chris@685: An audio file viewer and annotation editor. Chris@685: Centre for Digital Music, Queen Mary, University of London. Chris@685: This file copyright 2010-2011 Chris Cannam and QMUL. Chris@685: Chris@685: This program is free software; you can redistribute it and/or Chris@685: modify it under the terms of the GNU General Public License as Chris@685: published by the Free Software Foundation; either version 2 of the Chris@685: License, or (at your option) any later version. See the file Chris@685: COPYING included with this distribution for more information. Chris@685: */ Chris@685: Chris@1061: #ifndef SV_DEBUG_H Chris@1061: #define SV_DEBUG_H Chris@685: Chris@685: #include Chris@685: #include Chris@1864: #include Chris@843: Chris@1042: #include "RealTime.h" Chris@871: Chris@685: #include Chris@685: #include Chris@1061: #include Chris@685: Chris@685: class QString; Chris@685: class QUrl; Chris@685: Chris@685: QDebug &operator<<(QDebug &, const std::string &); Chris@685: std::ostream &operator<<(std::ostream &, const QString &); Chris@685: std::ostream &operator<<(std::ostream &, const QUrl &); Chris@685: Chris@843: using std::cout; Chris@843: using std::cerr; Chris@843: using std::endl; Chris@843: Chris@1061: class SVDebug { Chris@1061: public: Chris@1061: SVDebug(); Chris@1061: ~SVDebug(); Chris@1061: Chris@1061: template Chris@1061: inline SVDebug &operator<<(const T &t) { Chris@1275: if (m_silenced) return *this; Chris@1061: if (m_ok) { Chris@1061: if (m_eol) { Chris@1864: m_stream << m_prefix << "/" << m_timer.elapsed() << ": "; Chris@1061: } Chris@1061: m_stream << t; Chris@1061: m_eol = false; Chris@1061: } Chris@1061: return *this; Chris@1061: } Chris@1061: Chris@1061: inline SVDebug &operator<<(QTextStreamFunction) { Chris@1275: if (m_silenced) return *this; Chris@1061: m_stream << std::endl; Chris@1061: m_eol = true; Chris@1061: return *this; Chris@1061: } Chris@1061: Chris@1275: static void silence() { m_silenced = true; } Chris@1275: Chris@1061: private: Chris@1061: std::fstream m_stream; Chris@1061: char *m_prefix; Chris@1061: bool m_ok; Chris@1061: bool m_eol; Chris@1864: QElapsedTimer m_timer; Chris@1275: static bool m_silenced; Chris@1275: }; Chris@1275: Chris@1275: class SVCerr { Chris@1275: public: Chris@1275: SVCerr(SVDebug &d) : m_d(d) { } Chris@1275: Chris@1275: template Chris@1275: inline SVCerr &operator<<(const T &t) { Chris@1275: if (m_silenced) return *this; Chris@1275: m_d << t; Chris@1275: cerr << t; Chris@1275: return *this; Chris@1275: } Chris@1275: Chris@1275: inline SVCerr &operator<<(QTextStreamFunction f) { Chris@1275: if (m_silenced) return *this; Chris@1275: m_d << f; Chris@1275: cerr << std::endl; Chris@1275: return *this; Chris@1275: } Chris@1275: Chris@1275: static void silence() { m_silenced = true; } Chris@1275: Chris@1275: private: Chris@1275: SVDebug &m_d; Chris@1275: static bool m_silenced; Chris@1061: }; Chris@1061: Chris@1061: extern SVDebug &getSVDebug(); Chris@1275: extern SVCerr &getSVCerr(); Chris@685: Chris@1275: // Writes to debug log only Chris@690: #define SVDEBUG getSVDebug() Chris@685: Chris@1275: // Writes to both SVDEBUG and cerr Chris@1275: #define SVCERR getSVCerr() Chris@1275: Chris@685: #endif /* !_DEBUG_H_ */ Chris@685: