maxLibQt
AppDebugMessageHandler.h
Go to the documentation of this file.
1 /*
2  AppDebugMessageHandler
3  https://github.com/mpaperno/maxLibQt
4 
5  COPYRIGHT: (c)2017 Maxim Paperno; All Right Reserved.
6  Contact: http://www.WorldDesign.com/contact
7 
8  LICENSE:
9 
10  Commercial License Usage
11  Licensees holding valid commercial licenses may use this file in
12  accordance with the terms contained in a written agreement between
13  you and the copyright holder.
14 
15  GNU General Public License Usage
16  Alternatively, this file may be used under the terms of the GNU
17  General Public License as published by the Free Software Foundation,
18  either version 3 of the License, or (at your option) any later version.
19 
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
26 */
27 
28 #ifndef APPDEBUGMESSAGEHANDLER_H
29 #define APPDEBUGMESSAGEHANDLER_H
30 
31 #include <QtCore>
32 #include <QDebug>
33 #include <QIODevice>
34 #include <QObject>
35 #include <QRegularExpression>
36 #include <QReadWriteLock>
37 
39 #ifndef APP_DBG_HANDLER_ENABLE
40  #define APP_DBG_HANDLER_ENABLE 1
41 #endif
42 
44 #ifndef APP_DBG_HANDLER_DEFAULT_LEVEL
45  #define APP_DBG_HANDLER_DEFAULT_LEVEL 0
46 #endif
47 
50 #ifndef APP_DBG_HANDLER_SHOW_SRC_PATH
51  #define APP_DBG_HANDLER_SHOW_SRC_PATH 0
52 #endif
53 
56 #ifndef APP_DBG_HANDLER_SHOW_FUNCTION_DECL
57  #define APP_DBG_HANDLER_SHOW_FUNCTION_DECL 0
58 #endif
59 
61 #ifndef APP_DBG_HANDLER_SHOW_TIMESTAMP
62  #define APP_DBG_HANDLER_SHOW_TIMESTAMP 0
63 #endif
64 
67 #ifndef APP_DBG_HANDLER_TIMESTAMP_FORMAT
68  #define APP_DBG_HANDLER_TIMESTAMP_FORMAT "process"
69 #endif
70 
73 #ifndef APP_DBG_HANDLER_SRC_PATH
74  #define APP_DBG_HANDLER_SRC_PATH ".*src"
75 #endif
76 
77 // Make sure to include this header if using qInfo()
78 #if (QT_VERSION < QT_VERSION_CHECK(5, 5, 0))
79  #if defined(QT_NO_INFO_OUTPUT)
80  #define qInfo QT_NO_QDEBUG_MACRO
81  #else
82  #define qInfo qDebug
83  #endif
84  #define QtInfoMsg QtMsgType(4)
85 #endif
86 
138 {
139  Q_OBJECT
143  Q_PROPERTY(bool showSourcePath READ showSourcePath WRITE setShowSourcePath)
147  Q_PROPERTY(bool showTimestamp READ showTimestamp WRITE setShowTimestamp)
150 
151  public:
156 
157  inline quint8 appDebugOutputLevel() const { return m_appDebugOutputLevel; }
159 
160  inline bool showSourcePath() const { return m_showSourcePath; }
161  void setShowSourcePath(bool showSourcePath);
162  void setSourceBasePath(const QString &path = QString());
164 
165  inline bool showFunctionDeclarations() const { return m_showFunctionDeclarations; }
167 
168  inline bool showTimestamp() const { return m_showTimestamp; }
169  void setShowTimestamp(bool showTimestamp);
170 
171  inline QString timestampFormat() const { return m_tsFormat; }
172  void setTimestampFormat(const QString &timeFormat);
173 
175  void addOutputDevice(QIODevice *device);
177  void removeOutputDevice(QIODevice *device);
178 
180  QString messagePattern() const;
183 
192  void setMessagePattern(const QString &pattern = QString());
193 
195  void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
196 
211 
212 #if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0))
213  static QHash<int, QString> &fullTypeNames();
214 #endif
215 
216  signals:
218  void messageOutput(quint8 level, const QString &msg);
219 
220  private:
221  explicit AppDebugMessageHandler();
222  Q_DISABLE_COPY(AppDebugMessageHandler)
223 
224  QtMessageHandler m_defaultHandler;
225  QRegularExpression m_srcPathFilter;
226  quint8 m_appDebugOutputLevel;
227  QVector<QIODevice *> m_outputDevices;
228  bool m_showSourcePath;
229  bool m_showFunctionDeclarations;
230  bool m_showTimestamp;
231  QString m_tsFormat;
232  QString m_msgPattern;
233  mutable QString m_defaultPattern;
234  QReadWriteLock m_mutex;
235 #if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0))
236  QRegularExpression m_functionFilter;
237 #endif
238 
239 };
240 
245 void g_appDebugMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
246 
247 #endif // APPDEBUGMESSAGEHANDLER_H
void setShowTimestamp(bool showTimestamp)
static AppDebugMessageHandler * instance()
Get the singleton instance of this object. The instance is created automatically if necessary.
void messageOutput(quint8 level, const QString &msg)
Notifies when a new log massage is available.
void setTimestampFormat(const QString &timeFormat)
void installAppMessageHandler()
This function must be called to initialize this custom message handler. E.g. AppDebugMessageHandler::...
static QHash< int, QString > & shortTypeNames()
Returns a reference to the mapping of QtMsgType enum values to abbreviated names.
QString defaultMessagePattern() const
Returns the default message pattern. This format will take into account any properties set previously...
void setSourceBasePath(const QString &path=QString())
Set the base path for source file name filter, everything after this is kept. The string could be lit...
void addOutputDevice(QIODevice *device)
Add a new I/O stream for receiving messages.
void g_appDebugMessageHandler(QtMsgType, const QMessageLogContext &, const QString &)
QString messagePattern() const
Returns the current message pattern in use. This will be either the one set specifically with setMess...
void setAppDebugOutputLevel(quint8 appDebugOutputLevel)
void setShowSourcePath(bool showSourcePath)
void setMessagePattern(const QString &pattern=QString())
Specifies a debug message pattern to use instead of the default.
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Handle a debug message. This is typically called by the installed global message handler callback ( g...
void setShowFunctionDeclarations(bool showFunctionDeclarations)
Custom debug/message handler class to work in conjunction with qDebug() family of functions.
void removeOutputDevice(QIODevice *device)
Remove a previously-added I/O stream.