maxLibQt
|
Custom debug/message handler class to work in conjunction with qDebug()
family of functions.
More...
#include "AppDebugMessageHandler.h"
Inherits QObject.
Properties | |
int | appDebugOutputLevel |
Minimum severity level of messages to print. 0 = debug+; 1 = info+; 2 = warning+; 3 = critical+; 4 = fatal only. More... | |
bool | showFunctionDeclarations |
Enables or disables showing full function declarations (with return and attribute types) in the default message pattern. More... | |
bool | showSourcePath |
Enables or disabled showing the file path in the default message pattern. More... | |
bool | showTimestamp |
Enables or disables showing timestamps in the default message pattern. More... | |
QString | timestampFormat |
Set the timestamp format, as per Qt message pattern docs for %{time ...} options. E.g. one of "process", "boot", or a QDateTime::toString() format. More... | |
Properties inherited from QObject | |
objectName | |
Public Member Functions | |
void | addOutputDevice (QIODevice *device) |
Add a new I/O stream for receiving messages. More... | |
quint8 | appDebugOutputLevel () const |
QString | defaultMessagePattern () const |
Returns the default message pattern. This format will take into account any properties set previously, such as showSourcePath , showFunctionDeclarations , showTimestamp and timestampFormat . More... | |
void | installAppMessageHandler () |
This function must be called to initialize this custom message handler. E.g. AppDebugMessageHandler::instance()->installAppMessageHandler() More... | |
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_appDebugMessageHandler() ). More... | |
QString | messagePattern () const |
Returns the current message pattern in use. This will be either the one set specifically with setMessagePattern() or the defaultMessagePattern() otherwise. More... | |
void | removeOutputDevice (QIODevice *device) |
Remove a previously-added I/O stream. More... | |
void | setAppDebugOutputLevel (quint8 appDebugOutputLevel) |
void | setMessagePattern (const QString &pattern=QString()) |
Specifies a debug message pattern to use instead of the default. More... | |
void | setShowFunctionDeclarations (bool showFunctionDeclarations) |
void | setShowSourcePath (bool showSourcePath) |
void | setShowTimestamp (bool showTimestamp) |
void | setSourceBasePath (const QString &path=QString()) |
Set the base path for source file name filter, everything after this is kept. The string could be literal or a RegEx pattern (evaluated as non-greedy). More... | |
void | setTimestampFormat (const QString &timeFormat) |
bool | showFunctionDeclarations () const |
bool | showSourcePath () const |
bool | showTimestamp () const |
QString | timestampFormat () const |
Public Member Functions inherited from QObject | |
QObject (QObject *parent) | |
virtual | ~QObject () |
bool | blockSignals (bool block) |
const QObjectList & | children () const const |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const |
void | deleteLater () |
void | destroyed (QObject *obj) |
bool | disconnect (const char *signal, const QObject *receiver, const char *method) const const |
bool | disconnect (const QObject *receiver, const char *method) const const |
void | dumpObjectInfo () const const |
void | dumpObjectInfo () |
void | dumpObjectTree () const const |
void | dumpObjectTree () |
QList< QByteArray > | dynamicPropertyNames () const const |
virtual bool | event (QEvent *e) |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
T | findChild (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegExp ®Exp, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QString &name, Qt::FindChildOptions options) const const |
QList< T > | findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const |
bool | inherits (const char *className) const const |
void | installEventFilter (QObject *filterObj) |
bool | isWidgetType () const const |
bool | isWindowType () const const |
void | killTimer (int id) |
virtual const QMetaObject * | metaObject () const const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const const |
void | objectNameChanged (const QString &objectName) |
QObject * | parent () const const |
QVariant | property (const char *name) const const |
virtual int | qt_metacall (QMetaObject::Call, int, void **) |
virtual void * | qt_metacast (const char *) |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setParent (QObject *parent) |
bool | setProperty (const char *name, const QVariant &value) |
bool | signalsBlocked () const const |
int | startTimer (int interval, Qt::TimerType timerType) |
int | startTimer (std::chrono::milliseconds time, Qt::TimerType timerType) |
QThread * | thread () const const |
Static Public Member Functions | |
static AppDebugMessageHandler * | instance () |
Get the singleton instance of this object. The instance is created automatically if necessary. More... | |
static QHash< int, QString > & | shortTypeNames () |
Returns a reference to the mapping of QtMsgType enum values to abbreviated names. More... | |
Static Public Member Functions inherited from QObject | |
QMetaObject::Connection | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, Functor functor) |
QMetaObject::Connection | connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type) |
bool | disconnect (const QMetaObject::Connection &connection) |
bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method) |
bool | disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method) |
bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method) |
QString | tr (const char *sourceText, const char *disambiguation, int n) |
QString | trUtf8 (const char *sourceText, const char *disambiguation, int n) |
Signals | |
void | messageOutput (quint8 level, const QString &msg) |
Notifies when a new log massage is available. More... | |
Related Functions | |
(Note that these are not member functions.) | |
#define | APP_DBG_HANDLER_DEFAULT_LEVEL 0 |
Default log level. More... | |
#define | APP_DBG_HANDLER_ENABLE 1 |
Enable/disable this custom handler handler entirely. More... | |
#define | APP_DBG_HANDLER_SHOW_FUNCTION_DECL 0 |
Show full function declaration with return and attribute types instead of just the Class::Name. More... | |
#define | APP_DBG_HANDLER_SHOW_SRC_PATH 0 |
Include source file path/name in output (filtered with APP_DBG_HANDLER_SRC_PATH). More... | |
#define | APP_DBG_HANDLER_SHOW_TIMESTAMP 0 |
Include timestamp in default message pattern. More... | |
#define | APP_DBG_HANDLER_SRC_PATH ".*src" |
base path for source file name filter, everything after this is kept; RegEx, non-greedy More... | |
#define | APP_DBG_HANDLER_TIMESTAMP_FORMAT "process" |
Default timestamp format (as per Qt message pattern docs for %{time ...} options). More... | |
void | g_appDebugMessageHandler (QtMsgType, const QMessageLogContext &, const QString &) |
Additional Inherited Members | |
Protected Member Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
bool | isSignalConnected (const QMetaMethod &signal) const const |
int | receivers (const char *signal) const const |
QObject * | sender () const const |
int | senderSignalIndex () const const |
virtual void | timerEvent (QTimerEvent *event) |
Custom debug/message handler class to work in conjunction with qDebug()
family of functions.
AppDebugMessageHandler helps with formatting all stderr output generated by the application in a consistent manner. For example the function and line from which the debug message is called is always printed. It can also be used by other event listeners to intercept messages they may be interested in (connect to messageOutput()
signal or add a QIODevice
to receive the output).
You can set a minimum logging level for all messages by setting the appDebugOutputLevel property or APP_DBG_HANDLER_DEFAULT_LEVEL
macro.
There are several properties which control the default output message formatting. Optionally, a custom pattern can be set with setMessagePattern()
. This type of custom pattern allows for additional placeholder values which are not available with the default Qt handler. See setMessagePattern()
for details.
Note that the message format can still be overridden by setting QT_MESSAGE_PATTERN environment variable at run-time. see: https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePattern
Include this header to use qInfo() safely with Qt < v5.5 (qInfo becomes alias for qDebug).
This is an app-wide "global" thread-safe singleton class, use it with AppDebugMessageHandler::instance()
. For example, at start of application:
To connect to a signal:
To add a new I/O stream for receiving messages (eg. a log file):
When adding a QIODevice in this way, you may optionally set a minimum severity level below which you do not want messages written to this device. Use the Qt property system to set a properly named level on the QIODevice. For example:
Definition at line 137 of file AppDebugMessageHandler.h.
|
readwrite |
Minimum severity level of messages to print. 0 = debug+; 1 = info+; 2 = warning+; 3 = critical+; 4 = fatal only.
Definition at line 141 of file AppDebugMessageHandler.h.
|
readwrite |
Enables or disables showing full function declarations (with return and attribute types) in the default message pattern.
Definition at line 145 of file AppDebugMessageHandler.h.
|
readwrite |
Enables or disabled showing the file path in the default message pattern.
Definition at line 143 of file AppDebugMessageHandler.h.
|
readwrite |
Enables or disables showing timestamps in the default message pattern.
Definition at line 147 of file AppDebugMessageHandler.h.
|
readwrite |
Set the timestamp format, as per Qt message pattern docs for %{time
...} options. E.g. one of "process", "boot", or a QDateTime::toString()
format.
Definition at line 149 of file AppDebugMessageHandler.h.
void AppDebugMessageHandler::addOutputDevice | ( | QIODevice * | device | ) |
Add a new I/O stream for receiving messages.
Definition at line 102 of file AppDebugMessageHandler.cpp.
|
inline |
Definition at line 157 of file AppDebugMessageHandler.h.
QString AppDebugMessageHandler::defaultMessagePattern | ( | ) | const |
Returns the default message pattern. This format will take into account any properties set previously, such as showSourcePath
, showFunctionDeclarations
, showTimestamp
and timestampFormat
.
Definition at line 126 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::installAppMessageHandler | ( | ) |
This function must be called to initialize this custom message handler. E.g. AppDebugMessageHandler::instance()->installAppMessageHandler()
Definition at line 160 of file AppDebugMessageHandler.cpp.
|
static |
Get the singleton instance of this object. The instance is created automatically if necessary.
Definition at line 50 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::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_appDebugMessageHandler()
).
Definition at line 169 of file AppDebugMessageHandler.cpp.
|
signal |
Notifies when a new log massage is available.
QString AppDebugMessageHandler::messagePattern | ( | ) | const |
Returns the current message pattern in use. This will be either the one set specifically with setMessagePattern()
or the defaultMessagePattern()
otherwise.
Definition at line 155 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::removeOutputDevice | ( | QIODevice * | device | ) |
Remove a previously-added I/O stream.
Definition at line 109 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::setAppDebugOutputLevel | ( | quint8 | appDebugOutputLevel | ) |
Definition at line 56 of file AppDebugMessageHandler.cpp.
Specifies a debug message pattern to use instead of the default.
The pattern is the same as for qSetMessagePattern()
but extended with the following attributes:
%{short-type}
: Short type name (eg. "W" for warning, "D" for debug, etc) (as returned by shortTypeNames()
) %{full-function}
: Full function declaration including return and attribute types (if any)Call this function with a blank QString
to reset the message pattern back to default.
Definition at line 96 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::setShowFunctionDeclarations | ( | bool | showFunctionDeclarations | ) |
Definition at line 75 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::setShowSourcePath | ( | bool | showSourcePath | ) |
Definition at line 62 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::setShowTimestamp | ( | bool | showTimestamp | ) |
Definition at line 82 of file AppDebugMessageHandler.cpp.
Set the base path for source file name filter, everything after this is kept. The string could be literal or a RegEx pattern (evaluated as non-greedy).
Definition at line 69 of file AppDebugMessageHandler.cpp.
void AppDebugMessageHandler::setTimestampFormat | ( | const QString & | timeFormat | ) |
Definition at line 89 of file AppDebugMessageHandler.cpp.
Returns a reference to the mapping of QtMsgType
enum values to abbreviated names.
New names can be specified by setting this reference to a new value. For example:
Definition at line 246 of file AppDebugMessageHandler.cpp.
|
inline |
Definition at line 165 of file AppDebugMessageHandler.h.
|
inline |
Definition at line 160 of file AppDebugMessageHandler.h.
|
inline |
Definition at line 168 of file AppDebugMessageHandler.h.
|
inline |
Definition at line 171 of file AppDebugMessageHandler.h.
|
related |
Default log level.
Definition at line 45 of file AppDebugMessageHandler.h.
|
related |
Enable/disable this custom handler handler entirely.
Definition at line 40 of file AppDebugMessageHandler.h.
|
related |
Show full function declaration with return and attribute types instead of just the Class::Name.
Definition at line 57 of file AppDebugMessageHandler.h.
|
related |
Include source file path/name in output (filtered with APP_DBG_HANDLER_SRC_PATH).
Definition at line 51 of file AppDebugMessageHandler.h.
|
related |
Include timestamp in default message pattern.
Definition at line 62 of file AppDebugMessageHandler.h.
|
related |
base path for source file name filter, everything after this is kept; RegEx, non-greedy
Definition at line 74 of file AppDebugMessageHandler.h.
|
related |
Default timestamp format (as per Qt message pattern docs for %{time ...} options).
Definition at line 68 of file AppDebugMessageHandler.h.
|
related |
Message handler which is installed using qInstallMessageHandler. This needs to be global. Use AppDebugMessageHandler::instance()->installAppMessageHandler(); instead of installing this function directly (it will verify this handler is enabled, etc).
Definition at line 274 of file AppDebugMessageHandler.cpp.