annotate base/Debug.h @ 1864:166d7a4c2cd6 startup-timing

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