diff --git a/krusader/krdebuglogger.h b/krusader/krdebuglogger.h --- a/krusader/krdebuglogger.h +++ b/krusader/krdebuglogger.h @@ -23,47 +23,71 @@ #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: + explicit KrDebugLogger(); + ~KrDebugLogger() = default; + + //! 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 + //! The indentation that is presently used, it represents how many spaces are going to be used to indent + int indentation = 1; + //! The quantity of spaces that are going be added to the indentation when increasing it + const int indentationIncrease = 4; + //! Indicates if debug messages are going to be less detailed, which will be useful e.g. when comparing traces + bool briefMode = false; +}; +// --------------------------------------------------------------------------------------- + +//! A class 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 &); + explicit KrDebugFnLogger(const QString &argFunction, int line, KrDebugLogger &argKrDebugLogger); + //! This desstructor is used inside the KRFUNC macro. For more details: the description of the KRFUNC macro can be seen + ~KrDebugFnLogger(); + +private: + //! The name of a function which is going to be written about + QString function; + //! The KrDebugLogger that manages aspects that are common to KrDebugFnLogger objects + KrDebugLogger &krDebugLogger; }; -#ifdef QT_DEBUG +// --------------------------------------------------------------------------------------- + +//! An object that manages debug messages in a convenient way +extern KrDebugLogger krDebugLogger; -//! 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,53 @@ #include "krdebuglogger.h" -int KrDebugLogger::indentation = 1; -const int KrDebugLogger::indentationIncrease = 3; -const QString KrDebugLogger::logFile = QDir::tempPath() + "/krdebug"; +KrDebugLogger krDebugLogger; -KrDebugLogger::KrDebugLogger(const QString &argFunction, int line) : function(argFunction) +KrDebugLogger::KrDebugLogger() { - QFile file; - QTextStream stream; - prepareWriting(file, stream); - stream << QString("┏"); // Indicates that a function has been started - stream << function << "(" << line << ")" << endl; - indentation += indentationIncrease; + // Sets the level of detail/verbosity + const QByteArray krDebugBrief = qgetenv("KRDEBUG_BRIEF").toLower(); + briefMode = (krDebugBrief == "true" || krDebugBrief == "yes" || krDebugBrief == "on" || krDebugBrief == "1"); +} + +QString KrDebugLogger::indentationEtc(const QString &argFunction, int line, const QString &fnStartOrEnd) const +{ + 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) { - 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 << " "; + // Shows that a function has been started + qDebug().nospace().noquote() << krDebugLogger.indentationEtc(function, line, "┏"); + + krDebugLogger.increaseIndentation(); } + +KrDebugFnLogger::~KrDebugFnLogger() +{ + krDebugLogger.decreaseIndentation(); + // Shows that a function is going to finish + qDebug().nospace().noquote() << krDebugLogger.indentationEtc(function, 0, "┗"); +} +