void testMessagePattern() {
QString QT_MESSAGE_PATTERN=
"[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}]"
": %{file}:%{line} - %{message}";
qSetMessagePattern(QT_MESSAGE_PATTERN);
qDebug() << "Hello Pattern";
qDebug() << QString("黄彪").toUtf8().toHex();
}
[D]: /Users/Biao/Dropbox/workspace/Qt/Test/main.cpp:26 - Hello Pattern
[D]: /Users/Biao/Dropbox/workspace/Qt/Test/main.cpp:28 - "e9bb84e5bdaa"
void qSetMessagePattern(const QString & pattern)
Changes the output of the default message handler.
Allows to tweak the output of qDebug(), qWarning(), qCritical() and qFatal().
Following placeholders are supported:
You can also use conditionals on the type of the message using %{if-debug}, %{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.
Example:
QT_MESSAGE_PATTERN="[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}"
The default pattern is "%{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.
qSetMessagePattern() has no effect if a custom message handler is installed.
This function was introduced in QtCore 5.0.
See also qInstallMessageHandler() and Debugging Techniques.