annotate base/Debug.cpp @ 1172:59ae7e04f7e9

Merge
author Chris Cannam
date Fri, 04 Mar 2016 12:29:35 +0000
parents c1e43c8d2527
children 8f076d02569a
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@1061 19 #include <QMutex>
Chris@1061 20 #include <QDir>
Chris@685 21 #include <QUrl>
Chris@685 22 #include <QCoreApplication>
Chris@685 23
Chris@1061 24 #ifndef NDEBUG
Chris@685 25
Chris@1061 26 static SVDebug *debug = 0;
Chris@950 27 static QMutex mutex;
Chris@950 28
Chris@1061 29 SVDebug &getSVDebug() {
Chris@1061 30 mutex.lock();
Chris@1061 31 if (!debug) {
Chris@1061 32 debug = new SVDebug();
Chris@1061 33 }
Chris@1061 34 mutex.unlock();
Chris@1061 35 return *debug;
Chris@1061 36 }
Chris@1061 37
Chris@1061 38 SVDebug::SVDebug() :
Chris@1061 39 m_prefix(0),
Chris@1061 40 m_ok(false),
Chris@1061 41 m_eol(false)
Chris@685 42 {
Chris@951 43 QString pfx = ResourceFinder().getUserResourcePrefix();
Chris@951 44 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
Chris@951 45
Chris@1061 46 m_prefix = strdup(QString("[%1]")
Chris@1061 47 .arg(QCoreApplication::applicationPid())
Chris@1061 48 .toLatin1().data());
Chris@951 49
Chris@1058 50 //!!! what to do if mkpath fails?
Chris@1058 51 if (!logdir.exists()) logdir.mkpath(logdir.path());
Chris@1058 52
Chris@1061 53 QString fileName = logdir.path() + "/sv-debug.log";
Chris@1061 54
Chris@1061 55 m_stream.open(fileName.toLocal8Bit().data(), std::ios_base::out);
Chris@1061 56
Chris@1061 57 if (!m_stream) {
Chris@1061 58 QDebug(QtWarningMsg) << (const char *)m_prefix
Chris@1061 59 << "Failed to open debug log file "
Chris@1061 60 << fileName << " for writing";
Chris@951 61 } else {
Chris@1061 62 cerr << m_prefix << ": Log file is " << fileName << endl;
Chris@1061 63 m_ok = true;
Chris@685 64 }
Chris@1061 65 }
Chris@685 66
Chris@1061 67 SVDebug::~SVDebug()
Chris@1061 68 {
Chris@1061 69 m_stream.close();
Chris@685 70 }
Chris@685 71
Chris@685 72 QDebug &
Chris@685 73 operator<<(QDebug &dbg, const std::string &s)
Chris@685 74 {
Chris@685 75 dbg << QString::fromUtf8(s.c_str());
Chris@685 76 return dbg;
Chris@685 77 }
Chris@685 78
Chris@1061 79 #endif
Chris@1061 80
Chris@685 81 std::ostream &
Chris@685 82 operator<<(std::ostream &target, const QString &str)
Chris@685 83 {
Chris@846 84 return target << str.toStdString();
Chris@685 85 }
Chris@685 86
Chris@685 87 std::ostream &
Chris@685 88 operator<<(std::ostream &target, const QUrl &u)
Chris@685 89 {
Chris@846 90 return target << "<" << u.toString().toStdString() << ">";
Chris@685 91 }
Chris@685 92