annotate debug.cpp @ 57:f583e44d9d31

* Update copyrights; add debug header
author Chris Cannam
date Tue, 16 Nov 2010 13:57:30 +0000
parents
children 73073f34a5ee
rev   line source
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@57 27
Chris@57 28 #include <cstdio>
Chris@57 29
Chris@57 30 QDebug &
Chris@57 31 getEasyHgDebug()
Chris@57 32 {
Chris@57 33 static QFile *logFile = 0;
Chris@57 34 static QDebug *debug = 0;
Chris@57 35 static QMutex mutex;
Chris@57 36 static char *prefix;
Chris@57 37 mutex.lock();
Chris@57 38 if (!debug) {
Chris@57 39 prefix = new char[20];
Chris@57 40 sprintf(prefix, "[%lu]", (unsigned long)getpid());
Chris@57 41 logFile = new QFile(QDir::homePath() + "/.easyhg.log");
Chris@57 42 if (logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
Chris@57 43 QDebug(QtDebugMsg) << (const char *)prefix
Chris@57 44 << "Opened debug log file "
Chris@57 45 << logFile->fileName();
Chris@57 46 debug = new QDebug(logFile);
Chris@57 47 } else {
Chris@57 48 QDebug(QtWarningMsg) << (const char *)prefix
Chris@57 49 << "Failed to open debug log file "
Chris@57 50 << logFile->fileName()
Chris@57 51 << " for writing, using console debug instead";
Chris@57 52 debug = new QDebug(QtDebugMsg);
Chris@57 53 delete logFile;
Chris@57 54 logFile = 0;
Chris@57 55 }
Chris@57 56 }
Chris@57 57 mutex.unlock();
Chris@57 58
Chris@57 59 QDebug &dref = *debug;
Chris@57 60 return dref << endl << (const char *)prefix;
Chris@57 61 }
Chris@57 62
Chris@57 63 QDebug &
Chris@57 64 operator<<(QDebug &dbg, const std::string &s)
Chris@57 65 {
Chris@57 66 dbg << QString::fromUtf8(s.c_str());
Chris@57 67 return dbg;
Chris@57 68 }
Chris@57 69
Chris@57 70 std::ostream &
Chris@57 71 operator<<(std::ostream &target, const QString &str)
Chris@57 72 {
Chris@57 73 return target << str.toLocal8Bit().data();
Chris@57 74 }
Chris@57 75
Chris@57 76 std::ostream &
Chris@57 77 operator<<(std::ostream &target, const QUrl &u)
Chris@57 78 {
Chris@57 79 return target << "<" << u.toString() << ">";
Chris@57 80 }
Chris@57 81