maxLibQt
AppDebugMessageHandler Class Reference

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< QByteArraydynamicPropertyNames () const const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, 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 QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () 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)
 
QThreadthread () const const
 

Static Public Member Functions

static AppDebugMessageHandlerinstance ()
 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
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 

Detailed Description

Custom debug/message handler class to work in conjunction with qDebug() family of functions.

Version
2.0.0

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):

addOutputDevice(myQFile);

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:

myQFile.setProperty("level", 2); // only warning and higher

Definition at line 137 of file AppDebugMessageHandler.h.

Property Documentation

◆ appDebugOutputLevel

int AppDebugMessageHandler::appDebugOutputLevel
readwrite

Minimum severity level of messages to print. 0 = debug+; 1 = info+; 2 = warning+; 3 = critical+; 4 = fatal only.

Access functions:
appDebugOutputLevel(void), setAppDebugOutputLevel()

Definition at line 141 of file AppDebugMessageHandler.h.

◆ showFunctionDeclarations

bool AppDebugMessageHandler::showFunctionDeclarations
readwrite

Enables or disables showing full function declarations (with return and attribute types) in the default message pattern.

Access functions:
showFunctionDeclarations(void), setShowFunctionDeclarations()

Definition at line 145 of file AppDebugMessageHandler.h.

◆ showSourcePath

bool AppDebugMessageHandler::showSourcePath
readwrite

Enables or disabled showing the file path in the default message pattern.

Access functions:
showSourcePath(void), setShowSourcePath()
See also
setSourceBasePath()

Definition at line 143 of file AppDebugMessageHandler.h.

◆ showTimestamp

bool AppDebugMessageHandler::showTimestamp
readwrite

Enables or disables showing timestamps in the default message pattern.

Access functions:
showTimestamp(void), setShowTimestamp()
See also
timestampFormat

Definition at line 147 of file AppDebugMessageHandler.h.

◆ timestampFormat

QString AppDebugMessageHandler::timestampFormat
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.

Access functions:
timestampFormat(void), setTimestampFormat()
See also
showTimestamp

Definition at line 149 of file AppDebugMessageHandler.h.

Member Function Documentation

◆ addOutputDevice()

void AppDebugMessageHandler::addOutputDevice ( QIODevice device)

Add a new I/O stream for receiving messages.

Definition at line 102 of file AppDebugMessageHandler.cpp.

◆ appDebugOutputLevel()

quint8 AppDebugMessageHandler::appDebugOutputLevel ( void  ) const
inline
See also
appDebugOutputLevel

Definition at line 157 of file AppDebugMessageHandler.h.

◆ defaultMessagePattern()

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.

◆ installAppMessageHandler()

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.

◆ instance()

AppDebugMessageHandler * AppDebugMessageHandler::instance ( )
static

Get the singleton instance of this object. The instance is created automatically if necessary.

Definition at line 50 of file AppDebugMessageHandler.cpp.

◆ messageHandler()

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.

◆ messageOutput

void AppDebugMessageHandler::messageOutput ( quint8  level,
const QString msg 
)
signal

Notifies when a new log massage is available.

◆ messagePattern()

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.

◆ removeOutputDevice()

void AppDebugMessageHandler::removeOutputDevice ( QIODevice device)

Remove a previously-added I/O stream.

Definition at line 109 of file AppDebugMessageHandler.cpp.

◆ setAppDebugOutputLevel()

void AppDebugMessageHandler::setAppDebugOutputLevel ( quint8  appDebugOutputLevel)
See also
appDebugOutputLevel

Definition at line 56 of file AppDebugMessageHandler.cpp.

◆ setMessagePattern()

void AppDebugMessageHandler::setMessagePattern ( const QString pattern = QString())

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.

◆ setShowFunctionDeclarations()

void AppDebugMessageHandler::setShowFunctionDeclarations ( bool  showFunctionDeclarations)
See also
showFunctionDeclarations

Definition at line 75 of file AppDebugMessageHandler.cpp.

◆ setShowSourcePath()

void AppDebugMessageHandler::setShowSourcePath ( bool  showSourcePath)
See also
showSourcePath

Definition at line 62 of file AppDebugMessageHandler.cpp.

◆ setShowTimestamp()

void AppDebugMessageHandler::setShowTimestamp ( bool  showTimestamp)
See also
showTimestamp

Definition at line 82 of file AppDebugMessageHandler.cpp.

◆ setSourceBasePath()

void AppDebugMessageHandler::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).

Definition at line 69 of file AppDebugMessageHandler.cpp.

◆ setTimestampFormat()

void AppDebugMessageHandler::setTimestampFormat ( const QString timeFormat)
See also
timestampFormat

Definition at line 89 of file AppDebugMessageHandler.cpp.

◆ shortTypeNames()

QHash< int, QString > & AppDebugMessageHandler::shortTypeNames ( )
static

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:

{QtDebugMsg, QStringLiteral("DBG")},
{QtWarningMsg, QStringLiteral("WRN")},
{QtCriticalMsg, QStringLiteral("CRT")},
{QtFatalMsg, QStringLiteral("FTL")},
{QtInfoMsg, QStringLiteral("INF")}
};

Definition at line 246 of file AppDebugMessageHandler.cpp.

◆ showFunctionDeclarations()

bool AppDebugMessageHandler::showFunctionDeclarations ( void  ) const
inline
See also
showFunctionDeclarations

Definition at line 165 of file AppDebugMessageHandler.h.

◆ showSourcePath()

bool AppDebugMessageHandler::showSourcePath ( void  ) const
inline
See also
showSourcePath

Definition at line 160 of file AppDebugMessageHandler.h.

◆ showTimestamp()

bool AppDebugMessageHandler::showTimestamp ( void  ) const
inline
See also
showTimestamp

Definition at line 168 of file AppDebugMessageHandler.h.

◆ timestampFormat()

QString AppDebugMessageHandler::timestampFormat ( void  ) const
inline
See also
timestampFormat

Definition at line 171 of file AppDebugMessageHandler.h.

Friends And Related Function Documentation

◆ APP_DBG_HANDLER_DEFAULT_LEVEL

#define APP_DBG_HANDLER_DEFAULT_LEVEL   0
related

Default log level.

See also
AppDebugMessageHandler::appDebugOutputLevel

Definition at line 45 of file AppDebugMessageHandler.h.

◆ APP_DBG_HANDLER_ENABLE

#define APP_DBG_HANDLER_ENABLE   1
related

Enable/disable this custom handler handler entirely.

Definition at line 40 of file AppDebugMessageHandler.h.

◆ APP_DBG_HANDLER_SHOW_FUNCTION_DECL

#define APP_DBG_HANDLER_SHOW_FUNCTION_DECL   0
related

Show full function declaration with return and attribute types instead of just the Class::Name.

See also
AppDebugMessageHandler::showFunctionDeclarations

Definition at line 57 of file AppDebugMessageHandler.h.

◆ APP_DBG_HANDLER_SHOW_SRC_PATH

#define APP_DBG_HANDLER_SHOW_SRC_PATH   0
related

Include source file path/name in output (filtered with APP_DBG_HANDLER_SRC_PATH).

See also
AppDebugMessageHandler::showSourcePath

Definition at line 51 of file AppDebugMessageHandler.h.

◆ APP_DBG_HANDLER_SHOW_TIMESTAMP

#define APP_DBG_HANDLER_SHOW_TIMESTAMP   0
related

Include timestamp in default message pattern.

See also
AppDebugMessageHandler::showTimestamp

Definition at line 62 of file AppDebugMessageHandler.h.

◆ APP_DBG_HANDLER_SRC_PATH

#define APP_DBG_HANDLER_SRC_PATH   ".*src"
related

base path for source file name filter, everything after this is kept; RegEx, non-greedy

See also
AppDebugMessageHandler::setSourceBasePath()

Definition at line 74 of file AppDebugMessageHandler.h.

◆ APP_DBG_HANDLER_TIMESTAMP_FORMAT

#define APP_DBG_HANDLER_TIMESTAMP_FORMAT   "process"
related

Default timestamp format (as per Qt message pattern docs for %{time ...} options).

See also
AppDebugMessageHandler::timestampFormat

Definition at line 68 of file AppDebugMessageHandler.h.

◆ g_appDebugMessageHandler()

void g_appDebugMessageHandler ( QtMsgType  ,
const QMessageLogContext ,
const QString  
)
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.