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