Changeset View
Changeset View
Standalone View
Standalone View
krusader/krdebuglogger.h
Context not available. | |||||
23 | #define KRDEBUGLOGGER_H | 23 | #define KRDEBUGLOGGER_H | ||
---|---|---|---|---|---|
24 | 24 | | |||
25 | // QtCore | 25 | // QtCore | ||
26 | #include <QFile> | | |||
27 | #include <QDebug> | 26 | #include <QDebug> | ||
28 | #include <QDir> | 27 | #include <QDir> | ||
28 | #include <QFile> | ||||
29 | #include <QStringBuilder> | ||||
29 | #include <QTextStream> | 30 | #include <QTextStream> | ||
30 | 31 | | |||
31 | #include <unistd.h> | 32 | #include <unistd.h> | ||
32 | 33 | | |||
33 | //! A class to manage some aspects of the writing of messages into the Krusader debug log file | 34 | //! Manages a system aimed to show debug messages | ||
34 | class KrDebugLogger | 35 | class KrDebugLogger | ||
35 | { | 36 | { | ||
37 | public: | ||||
38 | explicit KrDebugLogger(); | ||||
39 | ~KrDebugLogger() = default; | ||||
40 | | ||||
41 | //! Builds a QString that contains the corresponding indentation and other information that has to be written | ||||
42 | /*! | ||||
43 | \param argFunction The name of the function where this method is called. | ||||
44 | \param line The line where this method is called. | ||||
45 | \param fnStartOrEnd In the case of a function: This QString is used to indicate the user if the function is starting or ending. | ||||
46 | \return The corresponding indentation (a group of spaces) and other information that has to be written in the same line. | ||||
47 | */ | ||||
48 | QString indentationEtc(const QString &argFunction, int line = 0, const QString &fnStartOrEnd = "") const; | ||||
49 | //! Decreases the indentation that is going to be used next time | ||||
50 | void decreaseIndentation(); | ||||
51 | //! Increases the indentation that is going to be used next time | ||||
52 | void increaseIndentation(); | ||||
36 | private: | 53 | private: | ||
37 | QString function; //! The name of a function which is going to be written about | 54 | //! The indentation that is presently used, it represents how many spaces are going to be used to indent | ||
38 | static int indentation; //! The indentation that is presently used, it represents how many spaces are going to be used | 55 | int indentation = 1; | ||
39 | const static int indentationIncrease; //! The quantity of spaces that are going be added to the indentation when increasing it | 56 | //! The quantity of spaces that are going be added to the indentation when increasing it | ||
40 | static const QString logFile; //! The name of the log file | 57 | const int indentationIncrease = 4; | ||
58 | //! Indicates if debug messages are going to be less detailed, which will be useful e.g. when comparing traces | ||||
59 | bool briefMode = false; | ||||
60 | }; | ||||
41 | 61 | | |||
62 | // --------------------------------------------------------------------------------------- | ||||
63 | | ||||
64 | //! A class to manage the automatic indentation of debug messages, and their writing when a function starts or ends | ||||
65 | class KrDebugFnLogger | ||||
66 | { | ||||
42 | public: | 67 | public: | ||
43 | //! This constructor is used inside the KRFUNC macro. For more details: the description of the KRFUNC macro can be seen | 68 | //! This constructor is used inside the KRFUNC macro. For more details: the description of the KRFUNC macro can be seen | ||
44 | KrDebugLogger(const QString &argFunction, int line); | 69 | explicit KrDebugFnLogger(const QString &argFunction, int line, KrDebugLogger &argKrDebugLogger); | ||
45 | //! For more information: the description of the KRFUNC macro can be seen | 70 | //! This desstructor is used inside the KRFUNC macro. For more details: the description of the KRFUNC macro can be seen | ||
46 | ~KrDebugLogger(); | 71 | ~KrDebugFnLogger(); | ||
47 | static void prepareWriting(QFile &, QTextStream &); | 72 | | ||
73 | private: | ||||
74 | //! The name of a function which is going to be written about | ||||
75 | QString function; | ||||
76 | //! The KrDebugLogger that manages aspects that are common to KrDebugFnLogger objects | ||||
77 | KrDebugLogger &krDebugLogger; | ||||
48 | }; | 78 | }; | ||
49 | 79 | | |||
50 | #ifdef QT_DEBUG | 80 | // --------------------------------------------------------------------------------------- | ||
81 | | ||||
82 | //! An object that manages debug messages in a convenient way | ||||
83 | extern KrDebugLogger krDebugLogger; | ||||
51 | 84 | | |||
52 | //! Writes a function name, etc. in the Krusader debug log when entering the function and automatically before exiting from it | 85 | //! Writes a function name, etc. when entering the function and automatically before exiting from it | ||
53 | #define KRFUNC \ | 86 | #define KRFUNC \ | ||
54 | KrDebugLogger functionLogger(__FUNCTION__, __LINE__); | 87 | KrDebugFnLogger functionLogger(__FUNCTION__, __LINE__, krDebugLogger); | ||
55 | 88 | | |||
56 | #define KRDEBUG(X...) do{ \ | 89 | #define KRDEBUG(X...) \ | ||
57 | QFile file; \ | 90 | qDebug().nospace().noquote() << krDebugLogger.indentationEtc(__FUNCTION__, __LINE__) << ": " << X; | ||
58 | QTextStream stream; \ | | |||
59 | KrDebugLogger::prepareWriting(file, stream); \ | | |||
60 | stream << __FUNCTION__ << "(" <<__LINE__<< "): "; \ | | |||
61 | stream << X << endl; \ | | |||
62 | } while(0); | | |||
63 | #else | | |||
64 | #define KRFUNC | | |||
65 | #define KRDEBUG(X...) qDebug() << X | | |||
66 | #endif | | |||
67 | 91 | | |||
68 | #endif // KRDEBUGLOGGER_H | 92 | #endif // KRDEBUGLOGGER_H | ||
69 | 93 | | |||
Context not available. |