Skip to content

Commit d78fb44

Browse files
author
Kai Koehne
committed
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>
1 parent 377ef06 commit d78fb44

File tree

6 files changed

+95
-24
lines changed

6 files changed

+95
-24
lines changed

src/corelib/global/qlogging.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ Q_CORE_EXPORT bool qt_logging_to_console()
174174
The class provides information about the source code location a qDebug(), qWarning(),
175175
qCritical() or qFatal() message was generated.
176176
177+
\note By default, this information is recorded only in debug builds. You can overwrite
178+
this explicitly by defining \c QT_MESSAGELOGCONTEXT or \c{QT_NO_MESSAGELOGCONTEXT}.
179+
177180
\sa QMessageLogger, QtMessageHandler, qInstallMessageHandler()
178181
*/
179182

@@ -185,8 +188,9 @@ Q_CORE_EXPORT bool qt_logging_to_console()
185188
186189
QMessageLogger is used to generate messages for the Qt logging framework. Usually one uses
187190
it through qDebug(), qWarning(), qCritical, or qFatal() functions,
188-
which are actually macros that expand to QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()
189-
et al.
191+
which are actually macros: For example qDebug() expands to
192+
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()
193+
for debug builds, and QMessageLogger(0, 0, 0).debug() for release builds.
190194
191195
One example of direct use is to forward errors that stem from a scripting language, e.g. QML:
192196

src/corelib/global/qlogging.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,28 @@ class Q_CORE_EXPORT QMessageLogger
124124
QMessageLogContext context;
125125
};
126126

127-
/*
128-
qDebug, qWarning, qCritical, qFatal are redefined to automatically include context information
129-
*/
130-
#define qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug
131-
#define qWarning QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).warning
132-
#define qCritical QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).critical
133-
#define qFatal QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).fatal
127+
#if !defined(QT_MESSAGELOGCONTEXT) && !defined(QT_NO_MESSAGELOGCONTEXT)
128+
# if defined(QT_NO_DEBUG)
129+
# define QT_NO_MESSAGELOGCONTEXT
130+
# else
131+
# define QT_MESSAGELOGCONTEXT
132+
# endif
133+
#endif
134+
135+
#ifdef QT_MESSAGELOGCONTEXT
136+
#define QT_MESSAGELOG_FILE __FILE__
137+
#define QT_MESSAGELOG_LINE __LINE__
138+
#define QT_MESSAGELOG_FUNC Q_FUNC_INFO
139+
#else
140+
#define QT_MESSAGELOG_FILE 0
141+
#define QT_MESSAGELOG_LINE 0
142+
#define QT_MESSAGELOG_FUNC 0
143+
#endif
144+
145+
#define qDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).debug
146+
#define qWarning QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
147+
#define qCritical QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).critical
148+
#define qFatal QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal
134149

135150
#define QT_NO_QDEBUG_MACRO while (false) QMessageLogger().noDebug
136151
#define QT_NO_QWARNING_MACRO while (false) QMessageLogger().noDebug

src/corelib/io/qloggingcategory.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,16 @@ class Q_CORE_EXPORT QLoggingCategory
110110
static const QLoggingCategory category(__VA_ARGS__); \
111111
return category; \
112112
}
113+
113114
#define qCDebug(category, ...) \
114115
for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
115-
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).debug(__VA_ARGS__)
116+
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug(__VA_ARGS__)
116117
#define qCWarning(category, ...) \
117118
for (bool qt_category_enabled = category().isWarningEnabled(); qt_category_enabled; qt_category_enabled = false) \
118-
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).warning(__VA_ARGS__)
119+
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).warning(__VA_ARGS__)
119120
#define qCCritical(category, ...) \
120121
for (bool qt_category_enabled = category().isCriticalEnabled(); qt_category_enabled; qt_category_enabled = false) \
121-
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, category().categoryName()).critical(__VA_ARGS__)
122+
QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).critical(__VA_ARGS__)
122123

123124
#else // defined(Q_COMPILER_VARIADIC_MACROS) || defined(Q_MOC_RUN)
124125

tests/auto/corelib/global/qlogging/app/app.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG += console
1010

1111
SOURCES += main.cpp
1212
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
13+
DEFINES += QT_MESSAGELOGCONTEXT
1314

1415
gcc:!mingw: QMAKE_LFLAGS += -rdynamic
1516

tests/auto/corelib/global/qlogging/test/test.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ TARGET = ../tst_qlogging
55
QT = core testlib
66
SOURCES = ../tst_qlogging.cpp
77

8+
DEFINES += QT_MESSAGELOGCONTEXT
89
TEST_HELPER_INSTALLS = ../app/app
910
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

tests/auto/corelib/io/qdebug/tst_qdebug.cpp

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ class MessageHandlerSetter
116116
*/
117117
void tst_QDebug::warningWithoutDebug() const
118118
{
119+
QString file, function;
120+
int line = 0;
119121
MessageHandlerSetter mhs(myMessageHandler);
120122
{ qWarning() << "A qWarning() message"; }
121-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
123+
#ifndef QT_NO_MESSAGELOGCONTEXT
124+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
125+
#endif
122126
QCOMPARE(s_msgType, QtWarningMsg);
123127
QCOMPARE(s_msg, QString::fromLatin1("A qWarning() message"));
124128
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -131,9 +135,13 @@ void tst_QDebug::warningWithoutDebug() const
131135
*/
132136
void tst_QDebug::criticalWithoutDebug() const
133137
{
138+
QString file, function;
139+
int line = 0;
134140
MessageHandlerSetter mhs(myMessageHandler);
135141
{ qCritical() << "A qCritical() message"; }
136-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
142+
#ifndef QT_NO_MESSAGELOGCONTEXT
143+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
144+
#endif
137145
QCOMPARE(s_msgType, QtCriticalMsg);
138146
QCOMPARE(s_msg, QString::fromLatin1("A qCritical() message"));
139147
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -143,9 +151,13 @@ void tst_QDebug::criticalWithoutDebug() const
143151

144152
void tst_QDebug::debugWithBool() const
145153
{
154+
QString file, function;
155+
int line = 0;
146156
MessageHandlerSetter mhs(myMessageHandler);
147157
{ qDebug() << false << true; }
148-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
158+
#ifndef QT_NO_MESSAGELOGCONTEXT
159+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
160+
#endif
149161
QCOMPARE(s_msgType, QtDebugMsg);
150162
QCOMPARE(s_msg, QString::fromLatin1("false true"));
151163
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -291,6 +303,8 @@ void tst_QDebug::stateSaver() const
291303

292304
void tst_QDebug::veryLongWarningMessage() const
293305
{
306+
QString file, function;
307+
int line = 0;
294308
MessageHandlerSetter mhs(myMessageHandler);
295309
QString test;
296310
{
@@ -299,7 +313,9 @@ void tst_QDebug::veryLongWarningMessage() const
299313
test.append(part);
300314
qWarning("Test output:\n%s\nend", qPrintable(test));
301315
}
302-
QString file = __FILE__; int line = __LINE__ - 2; QString function = Q_FUNC_INFO;
316+
#ifndef QT_NO_MESSAGELOGCONTEXT
317+
file = __FILE__; line = __LINE__ - 3; function = Q_FUNC_INFO;
318+
#endif
303319
QCOMPARE(s_msgType, QtWarningMsg);
304320
QCOMPARE(s_msg, QString::fromLatin1("Test output:\n")+test+QString::fromLatin1("\nend"));
305321
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -309,13 +325,17 @@ void tst_QDebug::veryLongWarningMessage() const
309325

310326
void tst_QDebug::qDebugQChar() const
311327
{
328+
QString file, function;
329+
int line = 0;
312330
MessageHandlerSetter mhs(myMessageHandler);
313331
{
314332
QDebug d = qDebug();
315333
d << QChar('f');
316334
d.nospace().noquote() << QChar('o') << QChar('o');
317335
}
318-
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
336+
#ifndef QT_NO_MESSAGELOGCONTEXT
337+
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
338+
#endif
319339
QCOMPARE(s_msgType, QtDebugMsg);
320340
QCOMPARE(s_msg, QString::fromLatin1("'f' oo"));
321341
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -328,12 +348,16 @@ void tst_QDebug::qDebugQStringRef() const
328348
{
329349
/* Use a basic string. */
330350
{
351+
QString file, function;
352+
int line = 0;
331353
const QString in(QLatin1String("input"));
332354
const QStringRef inRef(&in);
333355

334356
MessageHandlerSetter mhs(myMessageHandler);
335357
{ qDebug() << inRef; }
336-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
358+
#ifndef QT_NO_MESSAGELOGCONTEXT
359+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
360+
#endif
337361
QCOMPARE(s_msgType, QtDebugMsg);
338362
QCOMPARE(s_msg, QString::fromLatin1("\"input\""));
339363
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -343,11 +367,16 @@ void tst_QDebug::qDebugQStringRef() const
343367

344368
/* Use a null QStringRef. */
345369
{
370+
QString file, function;
371+
int line = 0;
372+
346373
const QStringRef inRef;
347374

348375
MessageHandlerSetter mhs(myMessageHandler);
349376
{ qDebug() << inRef; }
350-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
377+
#ifndef QT_NO_MESSAGELOGCONTEXT
378+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
379+
#endif
351380
QCOMPARE(s_msgType, QtDebugMsg);
352381
QCOMPARE(s_msg, QString::fromLatin1("\"\""));
353382
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -358,13 +387,17 @@ void tst_QDebug::qDebugQStringRef() const
358387

359388
void tst_QDebug::qDebugQLatin1String() const
360389
{
390+
QString file, function;
391+
int line = 0;
361392
MessageHandlerSetter mhs(myMessageHandler);
362393
{
363394
QDebug d = qDebug();
364395
d << QLatin1String("foo") << QLatin1String("") << QLatin1String("barbaz", 3);
365396
d.nospace().noquote() << QLatin1String("baz");
366397
}
367-
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
398+
#ifndef QT_NO_MESSAGELOGCONTEXT
399+
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
400+
#endif
368401
QCOMPARE(s_msgType, QtDebugMsg);
369402
QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
370403
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -374,13 +407,17 @@ void tst_QDebug::qDebugQLatin1String() const
374407

375408
void tst_QDebug::qDebugQByteArray() const
376409
{
410+
QString file, function;
411+
int line = 0;
377412
MessageHandlerSetter mhs(myMessageHandler);
378413
{
379414
QDebug d = qDebug();
380415
d << QByteArrayLiteral("foo") << QByteArrayLiteral("") << QByteArray("barbaz", 3);
381416
d.nospace().noquote() << QByteArrayLiteral("baz");
382417
}
383-
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
418+
#ifndef QT_NO_MESSAGELOGCONTEXT
419+
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
420+
#endif
384421
QCOMPARE(s_msgType, QtDebugMsg);
385422
QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
386423
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -397,11 +434,15 @@ Q_DECLARE_FLAGS(TestFlags, TestEnum)
397434

398435
void tst_QDebug::qDebugQFlags() const
399436
{
437+
QString file, function;
438+
int line = 0;
400439
QFlags<TestEnum> flags(Flag1 | Flag2);
401440

402441
MessageHandlerSetter mhs(myMessageHandler);
403442
{ qDebug() << flags; }
404-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
443+
#ifndef QT_NO_MESSAGELOGCONTEXT
444+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
445+
#endif
405446
QCOMPARE(s_msgType, QtDebugMsg);
406447
QCOMPARE(s_msg, QString::fromLatin1("QFlags(0x1|0x10)"));
407448
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -412,9 +453,13 @@ void tst_QDebug::qDebugQFlags() const
412453

413454
void tst_QDebug::textStreamModifiers() const
414455
{
456+
QString file, function;
457+
int line = 0;
415458
MessageHandlerSetter mhs(myMessageHandler);
416459
{ qDebug() << hex << short(0xf) << int(0xf) << unsigned(0xf) << long(0xf) << qint64(0xf) << quint64(0xf); }
417-
QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO;
460+
#ifndef QT_NO_MESSAGELOGCONTEXT
461+
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
462+
#endif
418463
QCOMPARE(s_msgType, QtDebugMsg);
419464
QCOMPARE(s_msg, QString::fromLatin1("f f f f f f"));
420465
QCOMPARE(QString::fromLatin1(s_file), file);
@@ -424,13 +469,17 @@ void tst_QDebug::textStreamModifiers() const
424469

425470
void tst_QDebug::resetFormat() const
426471
{
472+
QString file, function;
473+
int line = 0;
427474
MessageHandlerSetter mhs(myMessageHandler);
428475
{
429476
QDebug d = qDebug();
430477
d.nospace().noquote() << hex << int(0xf);
431478
d.resetFormat() << int(0xf) << QStringLiteral("foo");
432479
}
433-
QString file = __FILE__; int line = __LINE__ - 4; QString function = Q_FUNC_INFO;
480+
#ifndef QT_NO_MESSAGELOGCONTEXT
481+
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
482+
#endif
434483
QCOMPARE(s_msgType, QtDebugMsg);
435484
QCOMPARE(s_msg, QString::fromLatin1("f15 \"foo\""));
436485
QCOMPARE(QString::fromLatin1(s_file), file);

0 commit comments

Comments
 (0)