annotate base/Debug.cpp @ 902:16c48a3db2a7

More subrepos
author Chris Cannam
date Sat, 26 Apr 2014 23:05:32 +0100
parents 2010409af203
children 90e9a3f873cf
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@685 19 #include <QString>
Chris@685 20 #include <QUrl>
Chris@685 21 #include <QMutex>
Chris@685 22 #include <QMutexLocker>
Chris@685 23 #include <QFile>
Chris@685 24 #include <QDir>
Chris@685 25 #include <QCoreApplication>
Chris@685 26 #include <QDateTime>
Chris@685 27
Chris@685 28 #include <cstdio>
Chris@685 29
Chris@685 30 QDebug &
Chris@685 31 getSVDebug()
Chris@685 32 {
Chris@685 33 static QFile *logFile = 0;
Chris@685 34 static QDebug *debug = 0;
Chris@685 35 static QMutex mutex;
Chris@685 36 static char *prefix;
Chris@685 37 mutex.lock();
Chris@685 38 if (!debug) {
Chris@685 39 prefix = new char[20];
Chris@685 40 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
Chris@685 41 QString pfx = ResourceFinder().getUserResourcePrefix();
Chris@685 42 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
Chris@685 43 if (!logdir.exists()) logdir.mkpath(logdir.path());
Chris@685 44 logFile = new QFile(logdir.path() + "/debug.log");
Chris@685 45 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
Chris@685 46 QDebug(QtDebugMsg) << (const char *)prefix
Chris@685 47 << "Opened debug log file "
Chris@685 48 << logFile->fileName();
Chris@685 49 debug = new QDebug(logFile);
Chris@685 50 } else {
Chris@685 51 QDebug(QtWarningMsg) << (const char *)prefix
Chris@685 52 << "Failed to open debug log file "
Chris@685 53 << logFile->fileName()
Chris@685 54 << " for writing, using console debug instead";
Chris@685 55 debug = new QDebug(QtDebugMsg);
Chris@685 56 delete logFile;
Chris@685 57 logFile = 0;
Chris@685 58 }
Chris@685 59 *debug << endl << (const char *)prefix << "Log started at "
Chris@685 60 << QDateTime::currentDateTime().toString();
Chris@685 61 }
Chris@685 62 mutex.unlock();
Chris@685 63
Chris@685 64 QDebug &dref = *debug;
Chris@685 65 return dref << endl << (const char *)prefix;
Chris@685 66 }
Chris@685 67
Chris@685 68 QDebug &
Chris@685 69 operator<<(QDebug &dbg, const std::string &s)
Chris@685 70 {
Chris@685 71 dbg << QString::fromUtf8(s.c_str());
Chris@685 72 return dbg;
Chris@685 73 }
Chris@685 74
Chris@685 75 std::ostream &
Chris@685 76 operator<<(std::ostream &target, const QString &str)
Chris@685 77 {
Chris@846 78 return target << str.toStdString();
Chris@685 79 }
Chris@685 80
Chris@685 81 std::ostream &
Chris@685 82 operator<<(std::ostream &target, const QUrl &u)
Chris@685 83 {
Chris@846 84 return target << "<" << u.toString().toStdString() << ">";
Chris@685 85 }
Chris@685 86