comparison debug.cpp @ 57:f583e44d9d31

* Update copyrights; add debug header
author Chris Cannam
date Tue, 16 Nov 2010 13:57:30 +0000
parents
children 73073f34a5ee
comparison
equal deleted inserted replaced
56:1394c8cbf991 57:f583e44d9d31
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 EasyMercurial
5
6 Based on HgExplorer by Jari Korhonen
7 Copyright (c) 2010 Jari Korhonen
8 Copyright (c) 2010 Chris Cannam
9 Copyright (c) 2010 Queen Mary, University of London
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information.
16 */
17
18 #include "debug.h"
19
20 #include <QString>
21 #include <QUrl>
22 #include <QMutex>
23 #include <QMutexLocker>
24 #include <QFile>
25 #include <QDir>
26 #include <QCoreApplication>
27
28 #include <cstdio>
29
30 QDebug &
31 getEasyHgDebug()
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)getpid());
41 logFile = new QFile(QDir::homePath() + "/.easyhg.log");
42 if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
43 QDebug(QtDebugMsg) << (const char *)prefix
44 << "Opened debug log file "
45 << logFile->fileName();
46 debug = new QDebug(logFile);
47 } else {
48 QDebug(QtWarningMsg) << (const char *)prefix
49 << "Failed to open debug log file "
50 << logFile->fileName()
51 << " for writing, using console debug instead";
52 debug = new QDebug(QtDebugMsg);
53 delete logFile;
54 logFile = 0;
55 }
56 }
57 mutex.unlock();
58
59 QDebug &dref = *debug;
60 return dref << endl << (const char *)prefix;
61 }
62
63 QDebug &
64 operator<<(QDebug &dbg, const std::string &s)
65 {
66 dbg << QString::fromUtf8(s.c_str());
67 return dbg;
68 }
69
70 std::ostream &
71 operator<<(std::ostream &target, const QString &str)
72 {
73 return target << str.toLocal8Bit().data();
74 }
75
76 std::ostream &
77 operator<<(std::ostream &target, const QUrl &u)
78 {
79 return target << "<" << u.toString() << ">";
80 }
81