Logging: Disable tracking of debug source info for release builds

Tracking the file, line, function means the information has to be stored
in the binaries, enlarging the size. It also might be a surprise to some
commercial customers that their internal file & function names are
'leaked'. Therefore we enable it for debug builds only.

[ChangeLog][QtCore][Logging] File, line, function information are not
recorded anymore for logging statements in release builds. Set
QT_MESSAGELOGCONTEXT explicitly to enable recording in all configurations.

Change-Id: I454bdb42bcf5b5a8de6507f29f2a61109dca9b91
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@digia.com>
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
This commit is contained in:
Kai Koehne 2014-09-24 09:55:02 +02:00
parent 377ef06aef
commit d78fb442d7
6 changed files with 95 additions and 24 deletions

View File

@ -174,6 +174,9 @@ Q_CORE_EXPORT bool qt_logging_to_console()
The class provides information about the source code location a qDebug(), qWarning(),
qCritical() or qFatal() message was generated.
\note By default, this information is recorded only in debug builds. You can overwrite
this explicitly by defining \c QT_MESSAGELOGCONTEXT or \c{QT_NO_MESSAGELOGCONTEXT}.
\sa QMessageLogger, QtMessageHandler, qInstallMessageHandler()
*/
@ -185,8 +188,9 @@ Q_CORE_EXPORT bool qt_logging_to_console()
QMessageLogger is used to generate messages for the Qt logging framework. Usually one uses
it through qDebug(), qWarning(), qCritical, or qFatal() functions,
which are actually macros that expand to QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()
et al.
which are actually macros: For example qDebug() expands to
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()
for debug builds, and QMessageLogger(0, 0, 0).debug() for release builds.
One example of direct use is to forward errors that stem from a scripting language, e.g. QML:

View File

@ -124,13 +124,28 @@ private:
QMessageLogContext context;
};
/*
qDebug, qWarning, qCritical, qFatal are redefined to automatically include context information
*/
#define qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug
#define qWarning QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).warning
#define qCritical QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).critical
#define qFatal QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).fatal
#if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
# if defined(QT_NO_DEBUG)
# define QT_NO_MESSAGELOGCONTEXT
# else
# define QT_MESSAGELOGCONTEXT
# endif
#endif
#ifdef QT_MESSAGELOGCONTEXT
#define QT_MESSAGELOG_FILE __FILE__
#define QT_MESSAGELOG_LINE __LINE__
#define QT_MESSAGELOG_FUNC Q_FUNC_INFO
#else
#define QT_MESSAGELOG_FILE 0
#define QT_MESSAGELOG_LINE 0
#define QT_MESSAGELOG_FUNC 0
#endif
#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
#define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
#define QT_NO_QWARNING_MACRO while (false) QMessageLogger().noDebug

View File

@ -110,15 +110,16 @@ private:
static const QLoggingCategory category(__VA_ARGS__); \
return category; \
}
#define qCDebug(category, ...) \
for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).debug(__VA_ARGS__)
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
#define qCWarning(category, ...) \
for (bool qt_category_enabled = category().isWarningEnabled(); qt_category_enabled; qt_category_enabled = false) \
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).warning(__VA_ARGS__)
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).warning(__VA_ARGS__)
#define qCCritical(category, ...) \
for (bool qt_category_enabled = category().isCriticalEnabled(); qt_category_enabled; qt_category_enabled = false) \
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).critical(__VA_ARGS__)
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).critical(__VA_ARGS__)
#else // defined(Q_COMPILER_VARIADIC_MACROS) || defined(Q_MOC_RUN)

View File

@ -10,6 +10,7 @@ CONFIG += console
SOURCES += main.cpp
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
DEFINES += QT_MESSAGELOGCONTEXT
gcc:!mingw: QMAKE_LFLAGS += -rdynamic

View File

@ -5,5 +5,6 @@ TARGET = ../tst_qlogging
QT = core testlib
SOURCES = ../tst_qlogging.cpp
DEFINES += QT_MESSAGELOGCONTEXT
TEST_HELPER_INSTALLS = ../app/app
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

View File

@ -116,9 +116,13 @@ private:
*/
void tst_QDebug::warningWithoutDebug() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{ qWarning() << "A qWarning() message"; }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtWarningMsg);
QCOMPARE(s_msg, QString::fromLatin1("A qWarning() message"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -131,9 +135,13 @@ void tst_QDebug::warningWithoutDebug() const
*/
void tst_QDebug::criticalWithoutDebug() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{ qCritical() << "A qCritical() message"; }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtCriticalMsg);
QCOMPARE(s_msg, QString::fromLatin1("A qCritical() message"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -143,9 +151,13 @@ void tst_QDebug::criticalWithoutDebug() const
void tst_QDebug::debugWithBool() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << false << true; }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("false true"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -291,6 +303,8 @@ void tst_QDebug::stateSaver() const
void tst_QDebug::veryLongWarningMessage() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
QString test;
{
@ -299,7 +313,9 @@ void tst_QDebug::veryLongWarningMessage() const
test.append(part);
qWarning("Test output:\n%s\nend", qPrintable(test));
}
QString file = __FILE__; int line = __LINE__ - 2; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 3; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtWarningMsg);
QCOMPARE(s_msg, QString::fromLatin1("Test output:\n")+test+QString::fromLatin1("\nend"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -309,13 +325,17 @@ void tst_QDebug::veryLongWarningMessage() const
void tst_QDebug::qDebugQChar() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
d << QChar('f');
d.nospace().noquote() << QChar('o') << QChar('o');
}
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("'f' oo"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -328,12 +348,16 @@ void tst_QDebug::qDebugQStringRef() const
{
/* Use a basic string. */
{
QString file, function;
int line = 0;
const QString in(QLatin1String("input"));
const QStringRef inRef(&in);
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << inRef; }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("\"input\""));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -343,11 +367,16 @@ void tst_QDebug::qDebugQStringRef() const
/* Use a null QStringRef. */
{
QString file, function;
int line = 0;
const QStringRef inRef;
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << inRef; }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("\"\""));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -358,13 +387,17 @@ void tst_QDebug::qDebugQStringRef() const
void tst_QDebug::qDebugQLatin1String() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
d << QLatin1String("foo") << QLatin1String("") << QLatin1String("barbaz", 3);
d.nospace().noquote() << QLatin1String("baz");
}
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -374,13 +407,17 @@ void tst_QDebug::qDebugQLatin1String() const
void tst_QDebug::qDebugQByteArray() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
d << QByteArrayLiteral("foo") << QByteArrayLiteral("") << QByteArray("barbaz", 3);
d.nospace().noquote() << QByteArrayLiteral("baz");
}
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -397,11 +434,15 @@ Q_DECLARE_FLAGS(TestFlags, TestEnum)
void tst_QDebug::qDebugQFlags() const
{
QString file, function;
int line = 0;
QFlags<TestEnum> flags(Flag1 | Flag2);
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << flags; }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("QFlags(0x1|0x10)"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -412,9 +453,13 @@ void tst_QDebug::qDebugQFlags() const
void tst_QDebug::textStreamModifiers() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << hex << short(0xf) << int(0xf) << unsigned(0xf) << long(0xf) << qint64(0xf) << quint64(0xf); }
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("f f f f f f"));
QCOMPARE(QString::fromLatin1(s_file), file);
@ -424,13 +469,17 @@ void tst_QDebug::textStreamModifiers() const
void tst_QDebug::resetFormat() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
d.nospace().noquote() << hex << int(0xf);
d.resetFormat() << int(0xf) << QStringLiteral("foo");
}
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("f15 \"foo\""));
QCOMPARE(QString::fromLatin1(s_file), file);