svcore  1.9
Debug.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2010-2011 Chris Cannam and QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "Debug.h"
00017 #include "ResourceFinder.h"
00018 
00019 #include <QString>
00020 #include <QUrl>
00021 #include <QMutex>
00022 #include <QMutexLocker>
00023 #include <QFile>
00024 #include <QDir>
00025 #include <QCoreApplication>
00026 #include <QDateTime>
00027 #include <QThreadStorage>
00028 
00029 #include <cstdio>
00030 
00031 static QThreadStorage<QDebug *> debugs;
00032 static QMutex mutex;
00033 static char *prefix = 0;
00034 
00035 QDebug &
00036 getSVDebug()
00037 {
00038     mutex.lock();
00039 
00040     QDebug *debug = 0;
00041 
00042     QString pfx = ResourceFinder().getUserResourcePrefix();
00043     QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
00044 
00045     if (!prefix) {
00046         prefix = new char[20];
00047         sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
00049         if (!logdir.exists()) logdir.mkpath(logdir.path());
00050     }
00051 
00052     if (!debugs.hasLocalData()) {
00053         QFile *logFile = new QFile(logdir.path() + "/sv-debug.log");
00054         if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
00055             QDebug(QtDebugMsg) << (const char *)prefix
00056                                << "Opened debug log file "
00057                                << logFile->fileName();
00058             debug = new QDebug(logFile);
00059         } else {
00060             QDebug(QtWarningMsg) << (const char *)prefix
00061                                  << "Failed to open debug log file "
00062                                  << logFile->fileName()
00063                                  << " for writing, using console debug instead";
00064             delete logFile;
00065             logFile = 0;
00066             debug = new QDebug(QtDebugMsg);
00067         }
00068         debugs.setLocalData(debug);
00069         *debug << endl << (const char *)prefix << "Log started at "
00070                << QDateTime::currentDateTime().toString();
00071     } else {
00072         debug = debugs.localData();
00073     }
00074 
00075     mutex.unlock();
00076 
00077     QDebug &dref = *debug;
00078     dref << endl << (const char *)prefix;
00079 
00080     return dref;
00081 }
00082 
00083 QDebug &
00084 operator<<(QDebug &dbg, const std::string &s)
00085 {
00086     dbg << QString::fromUtf8(s.c_str());
00087     return dbg;
00088 }
00089 
00090 std::ostream &
00091 operator<<(std::ostream &target, const QString &str)
00092 {
00093     return target << str.toStdString();
00094 }
00095 
00096 std::ostream &
00097 operator<<(std::ostream &target, const QUrl &u)
00098 {
00099     return target << "<" << u.toString().toStdString() << ">";
00100 }
00101