QErrorMessage: use QStringBuilder

Extract Method msgType2i18nString() and use it to build 'rich' in a
single QStringBuilder expression.

Replace the switch over QtMsgType with an array of QT_TRANSLATE_NOOP'ed
strings. That introduces a dependency on the order and amount of enum
values, so add static and dynamic asserts to catch any change.

Saves memory allocations, as well as nearly 300B in text size on GCC 7
optimized Linux AMD64 builds.

Change-Id: I48cc916cba283e482a90ca4ae28aa17b26a4e5ab
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Marc Mutz 2017-02-21 09:10:47 +01:00
parent eec388865f
commit 7291a24416
1 changed files with 24 additions and 21 deletions

View File

@ -161,32 +161,35 @@ static void deleteStaticcQErrorMessage() // post-routine
static bool metFatal = false;
static QString msgType2i18nString(QtMsgType t)
{
Q_STATIC_ASSERT(QtDebugMsg == 0);
Q_STATIC_ASSERT(QtWarningMsg == 1);
Q_STATIC_ASSERT(QtCriticalMsg == 2);
Q_STATIC_ASSERT(QtFatalMsg == 3);
Q_STATIC_ASSERT(QtInfoMsg == 4);
// adjust the array below if any of the above fire...
const char * const messages[] = {
QT_TRANSLATE_NOOP("QErrorMessage", "Debug Message:"),
QT_TRANSLATE_NOOP("QErrorMessage", "Warning:"),
QT_TRANSLATE_NOOP("QErrorMessage", "Critical Error:"),
QT_TRANSLATE_NOOP("QErrorMessage", "Fatal Error:"),
QT_TRANSLATE_NOOP("QErrorMessage", "Information:"),
};
Q_ASSERT(size_t(t) < sizeof messages / sizeof *messages);
return QCoreApplication::translate("QErrorMessage", messages[t]);
}
static void jump(QtMsgType t, const QMessageLogContext & /*context*/, const QString &m)
{
if (!qtMessageHandler)
return;
QString rich;
switch (t) {
case QtDebugMsg:
rich = QErrorMessage::tr("Debug Message:");
break;
case QtWarningMsg:
rich = QErrorMessage::tr("Warning:");
break;
case QtCriticalMsg:
rich = QErrorMessage::tr("Critical Error:");
break;
case QtFatalMsg:
rich = QErrorMessage::tr("Fatal Error:");
break;
case QtInfoMsg:
rich = QErrorMessage::tr("Information:");
break;
}
rich = QString::fromLatin1("<p><b>%1</b></p>").arg(rich);
rich += Qt::convertFromPlainText(m, Qt::WhiteSpaceNormal);
QString rich = QLatin1String("<p><b>") + msgType2i18nString(t) + QLatin1String("</b></p>")
+ Qt::convertFromPlainText(m, Qt::WhiteSpaceNormal);
// ### work around text engine quirk
if (rich.endsWith(QLatin1String("</p>")))