Mercurial > hg > svcore
changeset 1061:c1e43c8d2527 tonioni
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
author | Chris Cannam |
---|---|
date | Tue, 31 Mar 2015 10:36:52 +0100 |
parents | c49d52386cde |
children | 1ec57a28e924 |
files | base/Debug.cpp base/Debug.h |
diffstat | 2 files changed, 70 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- 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 <QString> +#include <QMutex> +#include <QDir> #include <QUrl> -#include <QMutex> -#include <QMutexLocker> -#include <QFile> -#include <QDir> #include <QCoreApplication> -#include <QDateTime> -#include <QThreadStorage> -#include <cstdio> +#ifndef NDEBUG -static QThreadStorage<QDebug *> 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) {
--- 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 <QDebug> #include <QTextStream> @@ -23,6 +23,7 @@ #include <string> #include <iostream> +#include <fstream> class QString; class QUrl; @@ -37,29 +38,40 @@ #ifndef NDEBUG -extern QDebug &getSVDebug(); +class SVDebug { +public: + SVDebug(); + ~SVDebug(); + + template <typename T> + 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 <typename T> -inline QDebug &operator<<(QDebug &d, const T &t) { - QString s; - QTextStream ts(&s); - ts << t; - d << s; - return d; -} - #else class NoDebug