Chris@57: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@57: Chris@57: /* Chris@57: EasyMercurial Chris@57: Chris@57: Based on HgExplorer by Jari Korhonen Chris@57: Copyright (c) 2010 Jari Korhonen Chris@644: Copyright (c) 2013 Chris Cannam Chris@644: Copyright (c) 2013 Queen Mary, University of London Chris@57: Chris@57: This program is free software; you can redistribute it and/or Chris@57: modify it under the terms of the GNU General Public License as Chris@57: published by the Free Software Foundation; either version 2 of the Chris@57: License, or (at your option) any later version. See the file Chris@57: COPYING included with this distribution for more information. Chris@57: */ Chris@57: Chris@57: #include "debug.h" Chris@57: Chris@57: #include Chris@57: #include Chris@57: #include Chris@57: #include Chris@57: #include Chris@57: #include Chris@57: #include Chris@62: #include Chris@663: #include Chris@57: Chris@57: #include Chris@57: Chris@57: QDebug & Chris@57: getEasyHgDebug() Chris@57: { Chris@57: static QFile *logFile = 0; Chris@57: static QDebug *debug = 0; Chris@57: static QMutex mutex; Chris@57: static char *prefix; Chris@57: mutex.lock(); Chris@57: if (!debug) { Chris@57: prefix = new char[20]; Chris@60: sprintf(prefix, "[%lu]", (unsigned long)QCoreApplication::applicationPid()); Chris@477: QString logFileName = QDir::homePath() + "/.easyhg.log"; // the fallback Chris@708: #if QT_VERSION >= 0x050400 Chris@663: QString logDir = QStandardPaths::writableLocation Chris@689: (QStandardPaths::AppDataLocation); Chris@708: #else Chris@708: QString logDir = QStandardPaths::writableLocation Chris@708: (QStandardPaths::DataLocation); Chris@708: #endif Chris@477: if (logDir != "" && Chris@477: (QDir(logDir).exists() || Chris@477: QDir().mkpath(logDir))) { Chris@477: logFileName = logDir + "/debug.log"; Chris@477: } Chris@477: logFile = new QFile(logFileName); Chris@172: if (logFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) { Chris@57: QDebug(QtDebugMsg) << (const char *)prefix Chris@57: << "Opened debug log file " Chris@57: << logFile->fileName(); Chris@57: debug = new QDebug(logFile); Chris@57: } else { Chris@57: QDebug(QtWarningMsg) << (const char *)prefix Chris@57: << "Failed to open debug log file " Chris@57: << logFile->fileName() Chris@57: << " for writing, using console debug instead"; Chris@57: debug = new QDebug(QtDebugMsg); Chris@57: delete logFile; Chris@57: logFile = 0; Chris@57: } Chris@62: *debug << endl << (const char *)prefix << "Log started at " Chris@62: << QDateTime::currentDateTime().toString(); Chris@57: } Chris@57: mutex.unlock(); Chris@57: Chris@57: QDebug &dref = *debug; Chris@57: return dref << endl << (const char *)prefix; Chris@57: } Chris@57: Chris@57: QDebug & Chris@57: operator<<(QDebug &dbg, const std::string &s) Chris@57: { Chris@57: dbg << QString::fromUtf8(s.c_str()); Chris@57: return dbg; Chris@57: } Chris@57: Chris@57: std::ostream & Chris@57: operator<<(std::ostream &target, const QString &str) Chris@57: { Chris@57: return target << str.toLocal8Bit().data(); Chris@57: } Chris@57: Chris@57: std::ostream & Chris@57: operator<<(std::ostream &target, const QUrl &u) Chris@57: { Chris@57: return target << "<" << u.toString() << ">"; Chris@57: } Chris@57: