diff --git a/iso/iso.cpp b/iso/iso.cpp --- a/iso/iso.cpp +++ b/iso/iso.cpp @@ -73,6 +73,7 @@ 0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07 }; +KrDebugLogger krDebugLogger; kio_isoProtocol::kio_isoProtocol(const QByteArray &pool, const QByteArray &app) : SlaveBase("iso", pool, app) { diff --git a/iso/kiso.h b/iso/kiso.h --- a/iso/kiso.h +++ b/iso/kiso.h @@ -30,9 +30,9 @@ #include #include -#include "../krusader/krdebuglogger.h" #include "kisofile.h" #include "kisodirectory.h" +#include "../krusader/krdebuglogger.h" /** * @short A class for reading (optionally compressed) iso9660 files. @@ -121,4 +121,6 @@ KIsoPrivate * d; }; +extern KrDebugLogger krDebugLogger; + #endif diff --git a/krArc/krarc.cpp b/krArc/krarc.cpp --- a/krArc/krarc.cpp +++ b/krArc/krarc.cpp @@ -137,6 +137,8 @@ } // extern "C" +KrDebugLogger krDebugLogger; + #ifdef KRARC_ENABLED kio_krarcProtocol::kio_krarcProtocol(const QByteArray &pool_socket, const QByteArray &app_socket) : SlaveBase("kio_krarc", pool_socket, app_socket), archiveChanged(true), arcFile(0L), extArcReady(false), diff --git a/krusader/Archive/abstractthreadedjob.cpp b/krusader/Archive/abstractthreadedjob.cpp --- a/krusader/Archive/abstractthreadedjob.cpp +++ b/krusader/Archive/abstractthreadedjob.cpp @@ -39,8 +39,6 @@ #include "../krservices.h" #include "../FileSystem/filesystemprovider.h" -extern KRarcHandler arcHandler; - AbstractThreadedJob::AbstractThreadedJob() : KIO::Job(), _locker(), _waiter(), _stack(), _maxProgressValue(0), _currentProgress(0), _exiting(false), _jobThread(0) { diff --git a/krusader/Archive/packjob.cpp b/krusader/Archive/packjob.cpp --- a/krusader/Archive/packjob.cpp +++ b/krusader/Archive/packjob.cpp @@ -29,8 +29,6 @@ #include -extern KRarcHandler arcHandler; - PackJob::PackJob(const QUrl &srcUrl, const QUrl &destUrl, const QStringList & fileNames, const QString &type, const QMap &packProps) : AbstractThreadedJob() { startAbstractJobThread(new PackThread(srcUrl, destUrl, fileNames, type, packProps)); diff --git a/krusader/Search/krsearchmod.h b/krusader/Search/krsearchmod.h --- a/krusader/Search/krsearchmod.h +++ b/krusader/Search/krsearchmod.h @@ -22,6 +22,8 @@ #ifndef KRSEARCHMOD_H #define KRSEARCHMOD_H +#include "../krglobal.h" + // QtCore #include #include diff --git a/krusader/Search/krsearchmod.cpp b/krusader/Search/krsearchmod.cpp --- a/krusader/Search/krsearchmod.cpp +++ b/krusader/Search/krsearchmod.cpp @@ -43,8 +43,6 @@ static const QStringList TAR_TYPES = QStringList() << "tbz" << "tgz" << "tarz" << "tar" << "tlz"; -extern KRarcHandler arcHandler; - KRSearchMod::KRSearchMod(const KRQuery *query) : m_defaultFileSystem(nullptr), m_virtualFileSystem(nullptr), m_stopSearch(false) { diff --git a/krusader/krdebuglogger.h b/krusader/krdebuglogger.h --- a/krusader/krdebuglogger.h +++ b/krusader/krdebuglogger.h @@ -23,46 +23,62 @@ #define KRDEBUGLOGGER_H // QtCore -#include +#include #include +#include +#include #include #include -//! A class to manage some aspects of the writing of messages into the Krusader debug log file +//! Manages a system aimed to show debug messages class KrDebugLogger { +public: + KrDebugLogger(); + ~KrDebugLogger() {} + + //! Builds a QString that contains the corresponding indentation and other information that has to be written + /*! + \param argFunction The name of the function where this method is called. + \param line The line where this method is called. + \param fnStartOrEnd In the case of a function: This QString is used to indicate the user if the function is starting or ending. + \return The corresponding indentation (a group of spaces) and other information that has to be written in the same line. + */ + QString indentationEtc(const QString &argFunction, int line = 0, const QString &fnStartOrEnd = "") const; + //! Decreases the indentation that is going to be used next time + void decreaseIndentation(); + //! Increases the indentation that is going to be used next time + void increaseIndentation(); + private: - QString function; //! The name of a function which is going to be written about - static int indentation; //! The indentation that is presently used, it represents how many spaces are going to be used - const static int indentationIncrease; //! The quantity of spaces that are going be added to the indentation when increasing it - static const QString logFile; //! The name of the log file + int indentation = 1; //! The indentation that is presently used, it represents how many spaces are going to be used to indent + const int indentationIncrease = 4; //! The quantity of spaces that are going be added to the indentation when increasing it + + bool briefMode; //! Indicates if debug messages are going to be less detailed, which will be useful e.g. when comparing traces +}; + +// --------------------------------------------------------------------------------------- +//! A class that helps to manage: the automatic indentation of debug messages, and their writing when a function starts or ends +class KrDebugFnLogger +{ public: - //! This constructor is used inside the KRFUNC macro. For more details: the description of the KRFUNC macro can be seen - KrDebugLogger(const QString &argFunction, int line); - //! For more information: the description of the KRFUNC macro can be seen - ~KrDebugLogger(); - static void prepareWriting(QFile &, QTextStream &); + KrDebugFnLogger(const QString &argFunction, int line, KrDebugLogger &argKrDebugLogger); + ~KrDebugFnLogger(); + +private: + QString function; //! The name of a function which is going to be written about + KrDebugLogger &krDebugLogger; //! The KrDebugLogger that manages aspects that are common to those objects }; -#ifdef QT_DEBUG +// --------------------------------------------------------------------------------------- -//! Writes a function name, etc. in the Krusader debug log when entering the function and automatically before exiting from it +//! Writes a function name, etc. when entering the function and automatically before exiting from it #define KRFUNC \ - KrDebugLogger functionLogger(__FUNCTION__, __LINE__); + KrDebugFnLogger functionLogger(__FUNCTION__, __LINE__, krDebugLogger); -#define KRDEBUG(X...) do{ \ - QFile file; \ - QTextStream stream; \ - KrDebugLogger::prepareWriting(file, stream); \ - stream << __FUNCTION__ << "(" <<__LINE__<< "): "; \ - stream << X << endl; \ - } while(0); -#else -#define KRFUNC -#define KRDEBUG(X...) qDebug() << X -#endif +#define KRDEBUG(X...) \ + qDebug().nospace().noquote() << krDebugLogger.indentationEtc(__FUNCTION__, __LINE__) << ": " << X; #endif // KRDEBUGLOGGER_H - diff --git a/krusader/krdebuglogger.cpp b/krusader/krdebuglogger.cpp --- a/krusader/krdebuglogger.cpp +++ b/krusader/krdebuglogger.cpp @@ -21,38 +21,52 @@ #include "krdebuglogger.h" -int KrDebugLogger::indentation = 1; -const int KrDebugLogger::indentationIncrease = 3; -const QString KrDebugLogger::logFile = QDir::tempPath() + "/krdebug"; +KrDebugLogger::KrDebugLogger() +{ + // Sets the level of detail - verbosity + QByteArray krDebugBrief = qgetenv("KRDEBUG_BRIEF"); + briefMode = (krDebugBrief == "true" || krDebugBrief == "yes" || krDebugBrief == "on" || krDebugBrief == "1"); +} -KrDebugLogger::KrDebugLogger(const QString &argFunction, int line) : function(argFunction) +QString KrDebugLogger::indentationEtc(const QString &argFunction, int line, const QString &fnStartOrEnd) const { - QFile file; - QTextStream stream; - prepareWriting(file, stream); - stream << QString("┏"); // Indicates that a function has been started - stream << function << "(" << line << ")" << endl; - indentation += indentationIncrease; + QString result = QString(indentation - 1, ' ') % // Applies the indentation level to make logs clearer + fnStartOrEnd % argFunction; // Uses QStringBuilder to concatenate + if (!briefMode) + result = QString("Pid:%1 ").arg(getpid()) % + result % + (line != 0 ? QString("(%1)").arg(line) : ""); + return result; } -KrDebugLogger::~KrDebugLogger() +void KrDebugLogger::decreaseIndentation() { indentation -= indentationIncrease; - QFile file; - QTextStream stream; - prepareWriting(file, stream); - stream << QString("┗"); // Indicates that a function is going to finish - stream << function << endl; } -//! Prepares some elements before a writing into the krarc debug log file -void KrDebugLogger::prepareWriting(QFile &file, QTextStream &stream) +void KrDebugLogger::increaseIndentation() +{ + indentation += indentationIncrease; +} + +// --------------------------------------------------------------------------------------- +// Member functions of the KrDebugFnLogger class +// --------------------------------------------------------------------------------------- + +KrDebugFnLogger::KrDebugFnLogger(const QString &argFunction, int line, KrDebugLogger &argKrDebugLogger) : + function(argFunction), krDebugLogger(argKrDebugLogger) +{ + // Indicates that a function has been started + qDebug().nospace().noquote() << krDebugLogger.indentationEtc(function, line, "┏"); + + krDebugLogger.increaseIndentation(); + // This constructor is used inside the KRFUNC macro. For more details: the description of the KRFUNC macro can be seen +} + +KrDebugFnLogger::~KrDebugFnLogger() { - file.setFileName(logFile); - file.open(QIODevice::WriteOnly | QIODevice::Append); - stream.setDevice(&file); - stream << "Pid:" << (int)getpid(); - // Applies the indentation level to make logs clearer - for (int x = 0; x < indentation; ++x) - stream << " "; + krDebugLogger.decreaseIndentation(); + // Indicates that a function is going to finish + qDebug().nospace().noquote() << krDebugLogger.indentationEtc(function, 0, "┗"); + // For more information: the description of the KRFUNC macro can be seen } diff --git a/krusader/krglobal.h b/krusader/krglobal.h --- a/krusader/krglobal.h +++ b/krusader/krglobal.h @@ -22,6 +22,9 @@ #ifndef KRGLOBAL_H #define KRGLOBAL_H +#include "../Archive/krarchandler.h" +#include "../krusader/krdebuglogger.h" + // QtGui #include @@ -64,6 +67,9 @@ static int sCurrentConfigVersion; }; +extern KrDebugLogger krDebugLogger; +extern KRarcHandler arcHandler; + #define krConfig KrGlobal::config #define krMtMan (*(KrGlobal::mountMan)) #define krBookMan KrGlobal::bookman diff --git a/krusader/krglobal.cpp b/krusader/krglobal.cpp --- a/krusader/krglobal.cpp +++ b/krusader/krglobal.cpp @@ -43,6 +43,11 @@ const int KrGlobal::sConfigVersion; int KrGlobal::sCurrentConfigVersion; +//! An object that manages debug messages in a convenient way +KrDebugLogger krDebugLogger; +//! An object that manages archives in several parts of the source code +KRarcHandler arcHandler; + KrPanel *KrGlobal::activePanel() { return mainView->activePanel(); diff --git a/krusader/main.cpp b/krusader/main.cpp --- a/krusader/main.cpp +++ b/krusader/main.cpp @@ -45,8 +45,6 @@ #include #include -#include "../Archive/krarchandler.h" - #include "defaults.h" #include "krservices.h" #include "krslots.h" @@ -87,9 +85,6 @@ fprintf(stderr, "DBus Error: %s, %s\n", reply.error().name().toLocal8Bit().constData(), reply.error().message().toLocal8Bit().constData()); } -//! An object that manages archives in several parts of the source code. -KRarcHandler arcHandler; - int main(int argc, char *argv[]) { // set global log message format