annotate base/Debug.cpp @ 1867:2654bf447a84

CSV reader tests and fixes - avoid creating null events for lines in which the timings could not be read
author Chris Cannam
date Thu, 11 Jun 2020 14:09:59 +0100
parents 07a8793a0388
children 166d7a4c2cd6
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@685 26
Chris@1582 27 static SVDebug *svdebug = nullptr;
Chris@1582 28 static SVCerr *svcerr = nullptr;
Chris@950 29 static QMutex mutex;
Chris@950 30
Chris@1061 31 SVDebug &getSVDebug() {
Chris@1061 32 mutex.lock();
Chris@1275 33 if (!svdebug) {
Chris@1275 34 svdebug = new SVDebug();
Chris@1061 35 }
Chris@1061 36 mutex.unlock();
Chris@1275 37 return *svdebug;
Chris@1061 38 }
Chris@1061 39
Chris@1275 40 SVCerr &getSVCerr() {
Chris@1275 41 mutex.lock();
Chris@1275 42 if (!svcerr) {
Chris@1275 43 if (!svdebug) {
Chris@1275 44 svdebug = new SVDebug();
Chris@1275 45 }
Chris@1275 46 svcerr = new SVCerr(*svdebug);
Chris@1275 47 }
Chris@1275 48 mutex.unlock();
Chris@1275 49 return *svcerr;
Chris@1275 50 }
Chris@1275 51
Chris@1275 52 bool SVDebug::m_silenced = false;
Chris@1275 53 bool SVCerr::m_silenced = false;
Chris@1275 54
Chris@1061 55 SVDebug::SVDebug() :
Chris@1582 56 m_prefix(nullptr),
Chris@1061 57 m_ok(false),
Chris@1275 58 m_eol(true)
Chris@685 59 {
Chris@1275 60 if (m_silenced) return;
Chris@1275 61
Chris@1247 62 if (qApp->applicationName() == "") {
Chris@1247 63 cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl;
Chris@1247 64 throw std::logic_error("Can't use SVDEBUG before setting application name");
Chris@1247 65 }
Chris@1247 66
Chris@951 67 QString pfx = ResourceFinder().getUserResourcePrefix();
Chris@951 68 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
Chris@951 69
Chris@1061 70 m_prefix = strdup(QString("[%1]")
Chris@1061 71 .arg(QCoreApplication::applicationPid())
Chris@1061 72 .toLatin1().data());
Chris@951 73
Chris@1058 74 //!!! what to do if mkpath fails?
Chris@1058 75 if (!logdir.exists()) logdir.mkpath(logdir.path());
Chris@1058 76
Chris@1061 77 QString fileName = logdir.path() + "/sv-debug.log";
Chris@1061 78
Chris@1061 79 m_stream.open(fileName.toLocal8Bit().data(), std::ios_base::out);
Chris@1061 80
Chris@1061 81 if (!m_stream) {
Chris@1061 82 QDebug(QtWarningMsg) << (const char *)m_prefix
Chris@1061 83 << "Failed to open debug log file "
Chris@1061 84 << fileName << " for writing";
Chris@951 85 } else {
Chris@1061 86 m_ok = true;
Chris@1823 87 // cerr << "Log file is " << fileName << endl;
Chris@1554 88 (*this) << "Debug log started at "
Chris@1554 89 << QDateTime::currentDateTime().toString() << endl;
Chris@685 90 }
Chris@1061 91 }
Chris@685 92
Chris@1061 93 SVDebug::~SVDebug()
Chris@1061 94 {
Chris@1275 95 if (m_stream) m_stream.close();
Chris@685 96 }
Chris@685 97
Chris@685 98 QDebug &
Chris@685 99 operator<<(QDebug &dbg, const std::string &s)
Chris@685 100 {
Chris@685 101 dbg << QString::fromUtf8(s.c_str());
Chris@685 102 return dbg;
Chris@685 103 }
Chris@685 104
Chris@685 105 std::ostream &
Chris@685 106 operator<<(std::ostream &target, const QString &str)
Chris@685 107 {
Chris@846 108 return target << str.toStdString();
Chris@685 109 }
Chris@685 110
Chris@685 111 std::ostream &
Chris@685 112 operator<<(std::ostream &target, const QUrl &u)
Chris@685 113 {
Chris@846 114 return target << "<" << u.toString().toStdString() << ">";
Chris@685 115 }
Chris@685 116