pub unsafe fn q_set_message_pattern(
    message_pattern: impl CastInto<Ref<QString>>
)
Expand description

Changes the output of the default message handler.

Calls C++ function: void qSetMessagePattern(const QString& messagePattern).

C++ documentation:

Changes the output of the default message handler.

Allows to tweak the output of qDebug(), qInfo(), qWarning(), qCritical(), and qFatal(). The category logging output of qCDebug(), qCInfo(), qCWarning(), and qCCritical() is formatted, too.

Following placeholders are supported:

PlaceholderDescription
%{appname}QCoreApplication::applicationName()
%{category}Logging category
%{file}Path to source file
%{function}Function
%{line}Line in source file
%{message}The actual message
%{pid}QCoreApplication::applicationPid()
%{threadid}The system-wide ID of current thread (if it can be obtained)
%{qthreadptr}A pointer to the current QThread (result of QThread::currentThread())
%{type}"debug", "warning", "critical" or "fatal"
%{time process}time of the message, in seconds since the process started (the token "process" is literal)
%{time boot}the time of the message, in seconds since the system boot if that can be determined (the token "boot" is literal). If the time since boot could not be obtained, the output is indeterminate (see QElapsedTimer::msecsSinceReference()).
%{time [format]}system time when the message occurred, formatted by passing the format to QDateTime::toString(). If the format is not specified, the format of Qt::ISODate is used.
%{backtrace [depth=N] [separator="..."]}A backtrace with the number of frames specified by the optional depth parameter (defaults to 5), and separated by the optional separator parameter (defaults to "|"). This expansion is available only on some platforms (currently only platfoms using glibc). Names are only known for exported functions. If you want to see the name of every function in your application, use QMAKE_LFLAGS += -rdynamic. When reading backtraces, take into account that frames might be missing due to inlining or tail call optimization.

You can also use conditionals on the type of the message using %{if-debug}, %{if-info} %{if-warning}, %{if-critical} or %{if-fatal} followed by an %{endif}. What is inside the %{if-*} and %{endif} will only be printed if the type matches.

Finally, text inside %{if-category} ... %{endif} is only printed if the category is not the default one.

Example:

QT_MESSAGE_PATTERN=“[%{time yyyyMMdd h:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}”

The default pattern is "%{if-category}%{category}: %{endif}%{message}".

The pattern can also be changed at runtime by setting the QT_MESSAGE_PATTERN environment variable; if both qSetMessagePattern() is called and QT_MESSAGE_PATTERN is set, the environment variable takes precedence.

Custom message handlers can use qFormatLogMessage() to take pattern into account.

This function was introduced in Qt 5.0.

See also qInstallMessageHandler(), Debugging Techniques, and QLoggingCategory.