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@685: #include "Debug.h" Chris@685: #include "ResourceFinder.h" Chris@685: Chris@1061: #include Chris@1061: #include Chris@685: #include Chris@685: #include Chris@1554: #include Chris@685: Chris@1247: #include Chris@685: Chris@1275: static SVDebug *svdebug = 0; Chris@1275: static SVCerr *svcerr = 0; Chris@950: static QMutex mutex; Chris@950: Chris@1061: SVDebug &getSVDebug() { Chris@1061: mutex.lock(); Chris@1275: if (!svdebug) { Chris@1275: svdebug = new SVDebug(); Chris@1061: } Chris@1061: mutex.unlock(); Chris@1275: return *svdebug; Chris@1061: } Chris@1061: Chris@1275: SVCerr &getSVCerr() { Chris@1275: mutex.lock(); Chris@1275: if (!svcerr) { Chris@1275: if (!svdebug) { Chris@1275: svdebug = new SVDebug(); Chris@1275: } Chris@1275: svcerr = new SVCerr(*svdebug); Chris@1275: } Chris@1275: mutex.unlock(); Chris@1275: return *svcerr; Chris@1275: } Chris@1275: Chris@1275: bool SVDebug::m_silenced = false; Chris@1275: bool SVCerr::m_silenced = false; Chris@1275: Chris@1061: SVDebug::SVDebug() : Chris@1061: m_prefix(0), Chris@1061: m_ok(false), Chris@1275: m_eol(true) Chris@685: { Chris@1275: if (m_silenced) return; Chris@1275: Chris@1247: if (qApp->applicationName() == "") { Chris@1247: cerr << "ERROR: Can't use SVDEBUG before setting application name" << endl; Chris@1247: throw std::logic_error("Can't use SVDEBUG before setting application name"); Chris@1247: } Chris@1247: Chris@951: QString pfx = ResourceFinder().getUserResourcePrefix(); Chris@951: QDir logdir(QString("%1/%2").arg(pfx).arg("log")); Chris@951: Chris@1061: m_prefix = strdup(QString("[%1]") Chris@1061: .arg(QCoreApplication::applicationPid()) Chris@1061: .toLatin1().data()); Chris@951: Chris@1058: //!!! what to do if mkpath fails? Chris@1058: if (!logdir.exists()) logdir.mkpath(logdir.path()); Chris@1058: Chris@1061: QString fileName = logdir.path() + "/sv-debug.log"; Chris@1061: Chris@1061: m_stream.open(fileName.toLocal8Bit().data(), std::ios_base::out); Chris@1061: Chris@1061: if (!m_stream) { Chris@1061: QDebug(QtWarningMsg) << (const char *)m_prefix Chris@1061: << "Failed to open debug log file " Chris@1061: << fileName << " for writing"; Chris@951: } else { Chris@1061: m_ok = true; Chris@1554: (*this) << "Debug log started at " Chris@1554: << QDateTime::currentDateTime().toString() << endl; Chris@685: } Chris@1061: } Chris@685: Chris@1061: SVDebug::~SVDebug() Chris@1061: { Chris@1275: if (m_stream) m_stream.close(); Chris@685: } Chris@685: Chris@685: QDebug & Chris@685: operator<<(QDebug &dbg, const std::string &s) Chris@685: { Chris@685: dbg << QString::fromUtf8(s.c_str()); Chris@685: return dbg; Chris@685: } Chris@685: Chris@685: std::ostream & Chris@685: operator<<(std::ostream &target, const QString &str) Chris@685: { Chris@846: return target << str.toStdString(); Chris@685: } Chris@685: Chris@685: std::ostream & Chris@685: operator<<(std::ostream &target, const QUrl &u) Chris@685: { Chris@846: return target << "<" << u.toString().toStdString() << ">"; Chris@685: } Chris@685: