annotate base/Debug.cpp @ 1886:f803d3c33f76 tip

Switch off copious debug in soft synth driving
author Chris Cannam
date Fri, 14 Aug 2020 10:44:44 +0100
parents 38be2fa29efd
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@685 16 #include "Debug.h"
Chris@685 17 #include "ResourceFinder.h"
Chris@685 18
Chris@1061 19 #include <QMutex>
Chris@1061 20 #include <QDir>
Chris@685 21 #include <QUrl>
Chris@685 22 #include <QCoreApplication>
Chris@1554 23 #include <QDateTime>
Chris@685 24
Chris@1247 25 #include <stdexcept>
Chris@1864 26 #include <memory>
Chris@685 27
Chris@1864 28 static std::unique_ptr<SVDebug> svdebug = nullptr;
Chris@1864 29 static std::unique_ptr<SVCerr> svcerr = nullptr;
Chris@950 30 static QMutex mutex;
Chris@950 31
Chris@1061 32 SVDebug &getSVDebug() {
Chris@1061 33 mutex.lock();
Chris@1275 34 if (!svdebug) {
Chris@1882 35 svdebug = std::unique_ptr<SVDebug>(new SVDebug());
Chris@1061 36 }
Chris@1061 37 mutex.unlock();
Chris@1275 38 return *svdebug;
Chris@1061 39 }
Chris@1061 40
Chris@1275 41 SVCerr &getSVCerr() {
Chris@1275 42 mutex.lock();
Chris@1275 43 if (!svcerr) {
Chris@1275 44 if (!svdebug) {
Chris@1882 45 svdebug = std::unique_ptr<SVDebug>(new SVDebug());
Chris@1275 46 }
Chris@1882 47 svcerr = std::unique_ptr<SVCerr>(new SVCerr(*svdebug));
Chris@1275 48 }
Chris@1275 49 mutex.unlock();
Chris@1275 50 return *svcerr;
Chris@1275 51 }
Chris@1275 52
Chris@1275 53 bool SVDebug::m_silenced = false;
Chris@1275 54 bool SVCerr::m_silenced = false;
Chris@1275 55
Chris@1061 56 SVDebug::SVDebug() :
Chris@1582 57 m_prefix(nullptr),
Chris@1061 58 m_ok(false),
Chris@1275 59 m_eol(true)
Chris@685 60 {
Chris@1275 61 if (m_silenced) return;
Chris@1864 62
Chris@1864 63 m_timer.start();
Chris@1275 64
Chris@1247 65 if (qApp->applicationName() == "") {
Chris@1247 66 cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl;
Chris@1247 67 throw std::logic_error("Can't use SVDEBUG before setting application name");
Chris@1247 68 }
Chris@1247 69
Chris@951 70 QString pfx = ResourceFinder().getUserResourcePrefix();
Chris@951 71 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
Chris@951 72
Chris@1061 73 m_prefix = strdup(QString("[%1]")
Chris@1061 74 .arg(QCoreApplication::applicationPid())
Chris@1061 75 .toLatin1().data());
Chris@951 76
Chris@1058 77 //!!! what to do if mkpath fails?
Chris@1058 78 if (!logdir.exists()) logdir.mkpath(logdir.path());
Chris@1058 79
Chris@1061 80 QString fileName = logdir.path() + "/sv-debug.log";
Chris@1061 81
Chris@1061 82 m_stream.open(fileName.toLocal8Bit().data(), std::ios_base::out);
Chris@1061 83
Chris@1061 84 if (!m_stream) {
Chris@1061 85 QDebug(QtWarningMsg) << (const char *)m_prefix
Chris@1061 86 << "Failed to open debug log file "
Chris@1061 87 << fileName << " for writing";
Chris@951 88 } else {
Chris@1061 89 m_ok = true;
Chris@1823 90 // cerr << "Log file is " << fileName << endl;
Chris@1554 91 (*this) << "Debug log started at "
Chris@1554 92 << QDateTime::currentDateTime().toString() << endl;
Chris@685 93 }
Chris@1061 94 }
Chris@685 95
Chris@1061 96 SVDebug::~SVDebug()
Chris@1061 97 {
Chris@1864 98 if (m_stream) {
Chris@1864 99 (*this) << "Debug log ends" << endl;
Chris@1864 100 m_stream.close();
Chris@1864 101 }
Chris@685 102 }
Chris@685 103
Chris@685 104 QDebug &
Chris@685 105 operator<<(QDebug &dbg, const std::string &s)
Chris@685 106 {
Chris@685 107 dbg << QString::fromUtf8(s.c_str());
Chris@685 108 return dbg;
Chris@685 109 }
Chris@685 110
Chris@685 111 std::ostream &
Chris@685 112 operator<<(std::ostream &target, const QString &str)
Chris@685 113 {
Chris@1864 114 return target << str.toUtf8().data();
Chris@685 115 }
Chris@685 116
Chris@685 117 std::ostream &
Chris@685 118 operator<<(std::ostream &target, const QUrl &u)
Chris@685 119 {
Chris@1864 120 return target << "<" << u.toString() << ">";
Chris@685 121 }
Chris@685 122