# HG changeset patch # User Chris Cannam # Date 1427794612 -3600 # Node ID c1e43c8d2527af44971a1f30c5231e94ab01b2a2 # Parent c49d52386cde0844cef30d8904a4fe409cec3d5c Thread-local debug was causing crash on exit with Qt 5.4.x. But we introduced that because QDebug itself was crashing when used from multiple threads. Replace with simpler fstream version diff -r c49d52386cde -r c1e43c8d2527 base/Debug.cpp --- a/base/Debug.cpp Wed Mar 25 10:54:51 2015 +0000 +++ b/base/Debug.cpp Tue Mar 31 10:36:52 2015 +0100 @@ -16,70 +16,57 @@ #include "Debug.h" #include "ResourceFinder.h" -#include +#include +#include #include -#include -#include -#include -#include #include -#include -#include -#include +#ifndef NDEBUG -static QThreadStorage debugs; +static SVDebug *debug = 0; static QMutex mutex; -static char *prefix = 0; -QDebug & -getSVDebug() +SVDebug &getSVDebug() { + mutex.lock(); + if (!debug) { + debug = new SVDebug(); + } + mutex.unlock(); + return *debug; +} + +SVDebug::SVDebug() : + m_prefix(0), + m_ok(false), + m_eol(false) { - mutex.lock(); - - QDebug *debug = 0; - QString pfx = ResourceFinder().getUserResourcePrefix(); QDir logdir(QString("%1/%2").arg(pfx).arg("log")); - if (!prefix) { - prefix = strdup(QString("[%1]") - .arg(QCoreApplication::applicationPid()) - .toLatin1().data()); - } + m_prefix = strdup(QString("[%1]") + .arg(QCoreApplication::applicationPid()) + .toLatin1().data()); //!!! what to do if mkpath fails? if (!logdir.exists()) logdir.mkpath(logdir.path()); - if (!debugs.hasLocalData()) { - QFile *logFile = new QFile(logdir.path() + "/sv-debug.log"); - if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) { - QDebug(QtDebugMsg) << (const char *)prefix - << "Opened debug log file " - << logFile->fileName(); - debug = new QDebug(logFile); - } else { - QDebug(QtWarningMsg) << (const char *)prefix - << "Failed to open debug log file " - << logFile->fileName() - << " for writing, using console debug instead"; - delete logFile; - logFile = 0; - debug = new QDebug(QtDebugMsg); - } - debugs.setLocalData(debug); - *debug << endl << (const char *)prefix << "Log started at " - << QDateTime::currentDateTime().toString(); + QString fileName = logdir.path() + "/sv-debug.log"; + + m_stream.open(fileName.toLocal8Bit().data(), std::ios_base::out); + + if (!m_stream) { + QDebug(QtWarningMsg) << (const char *)m_prefix + << "Failed to open debug log file " + << fileName << " for writing"; } else { - debug = debugs.localData(); + cerr << m_prefix << ": Log file is " << fileName << endl; + m_ok = true; } +} - mutex.unlock(); - - QDebug &dref = *debug; - dref << endl << (const char *)prefix; - - return dref; +SVDebug::~SVDebug() +{ + m_stream.close(); } QDebug & @@ -89,6 +76,8 @@ return dbg; } +#endif + std::ostream & operator<<(std::ostream &target, const QString &str) { diff -r c49d52386cde -r c1e43c8d2527 base/Debug.h --- a/base/Debug.h Wed Mar 25 10:54:51 2015 +0000 +++ b/base/Debug.h Tue Mar 31 10:36:52 2015 +0100 @@ -13,8 +13,8 @@ COPYING included with this distribution for more information. */ -#ifndef _DEBUG_H_ -#define _DEBUG_H_ +#ifndef SV_DEBUG_H +#define SV_DEBUG_H #include #include @@ -23,6 +23,7 @@ #include #include +#include class QString; class QUrl; @@ -37,29 +38,40 @@ #ifndef NDEBUG -extern QDebug &getSVDebug(); +class SVDebug { +public: + SVDebug(); + ~SVDebug(); + + template + inline SVDebug &operator<<(const T &t) { + if (m_ok) { + if (m_eol) { + m_stream << m_prefix << " "; + } + m_stream << t; + m_eol = false; + } + return *this; + } + + inline SVDebug &operator<<(QTextStreamFunction) { + m_stream << std::endl; + m_eol = true; + return *this; + } + +private: + std::fstream m_stream; + char *m_prefix; + bool m_ok; + bool m_eol; +}; + +extern SVDebug &getSVDebug(); #define SVDEBUG getSVDebug() -inline QDebug &operator<<(QDebug &d, const RealTime &rt) { - d << rt.toString(); - return d; -} - -inline QDebug &operator<<(QDebug &d, const Vamp::RealTime &rt) { - d << rt.toString(); - return d; -} - -template -inline QDebug &operator<<(QDebug &d, const T &t) { - QString s; - QTextStream ts(&s); - ts << t; - d << s; - return d; -} - #else class NoDebug