comparison base/Debug.cpp @ 685:99222d4bfc78 debug-output

Add Debug class
author Chris Cannam
date Thu, 12 May 2011 16:56:08 +0100
parents
children a299c4cec0f8
comparison
equal deleted inserted replaced
682:bd527db65d20 685:99222d4bfc78
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2010-2011 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "Debug.h"
17 #include "ResourceFinder.h"
18
19 #include <QString>
20 #include <QUrl>
21 #include <QMutex>
22 #include <QMutexLocker>
23 #include <QFile>
24 #include <QDir>
25 #include <QCoreApplication>
26 #include <QDateTime>
27
28 #include <cstdio>
29
30 QDebug &
31 getSVDebug()
32 {
33 static QFile *logFile = 0;
34 static QDebug *debug = 0;
35 static QMutex mutex;
36 static char *prefix;
37 mutex.lock();
38 if (!debug) {
39 prefix = new char[20];
40 sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid());
41 QString pfx = ResourceFinder().getUserResourcePrefix();
42 QDir logdir(QString("%1/%2").arg(pfx).arg("log"));
43 if (!logdir.exists()) logdir.mkpath(logdir.path());
44 logFile = new QFile(logdir.path() + "/debug.log");
45 if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
46 QDebug(QtDebugMsg) << (const char *)prefix
47 << "Opened debug log file "
48 << logFile->fileName();
49 debug = new QDebug(logFile);
50 } else {
51 QDebug(QtWarningMsg) << (const char *)prefix
52 << "Failed to open debug log file "
53 << logFile->fileName()
54 << " for writing, using console debug instead";
55 debug = new QDebug(QtDebugMsg);
56 delete logFile;
57 logFile = 0;
58 }
59 *debug << endl << (const char *)prefix << "Log started at "
60 << QDateTime::currentDateTime().toString();
61 }
62 mutex.unlock();
63
64 QDebug &dref = *debug;
65 return dref << endl << (const char *)prefix;
66 }
67
68 QDebug &
69 operator<<(QDebug &dbg, const std::string &s)
70 {
71 dbg << QString::fromUtf8(s.c_str());
72 return dbg;
73 }
74
75 std::ostream &
76 operator<<(std::ostream &target, const QString &str)
77 {
78 return target << str.toLocal8Bit().data();
79 }
80
81 std::ostream &
82 operator<<(std::ostream &target, const QUrl &u)
83 {
84 return target << "<" << u.toString() << ">";
85 }
86