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 <QDebug>
Chris@685: #include <QTextStream>
Chris@843: 
Chris@1042: #include "RealTime.h"
Chris@871: 
Chris@685: #include <string>
Chris@685: #include <iostream>
Chris@1061: #include <fstream>
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@685: #ifndef NDEBUG
Chris@685: 
Chris@1061: class SVDebug {
Chris@1061: public:
Chris@1061:     SVDebug();
Chris@1061:     ~SVDebug();
Chris@1061: 
Chris@1061:     template <typename T>
Chris@1061:     inline SVDebug &operator<<(const T &t) {
Chris@1061:         if (m_ok) {
Chris@1061:             if (m_eol) {
Chris@1061:                 m_stream << m_prefix << " ";
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@1061:         m_stream << std::endl;
Chris@1061:         m_eol = true;
Chris@1061:         return *this;
Chris@1061:     }
Chris@1061: 
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@1061: };
Chris@1061: 
Chris@1061: extern SVDebug &getSVDebug();
Chris@685: 
Chris@690: #define SVDEBUG getSVDebug()
Chris@685: 
Chris@685: #else
Chris@685: 
Chris@685: class NoDebug
Chris@685: {
Chris@685: public:
Chris@685:     inline NoDebug() {}
Chris@685:     inline ~NoDebug(){}
Chris@685: 
Chris@685:     template <typename T>
Chris@685:     inline NoDebug &operator<<(const T &) { return *this; }
Chris@685: 
Chris@685:     inline NoDebug &operator<<(QTextStreamFunction) { return *this; }
Chris@685: };
Chris@685: 
Chris@690: #define SVDEBUG NoDebug()
Chris@685: 
Chris@685: #endif /* !NDEBUG */
Chris@685: 
Chris@685: #endif /* !_DEBUG_H_ */
Chris@685: