diff options
-rw-r--r-- | src/corelib/global/qlogging.cpp | 8 | ||||
-rw-r--r-- | src/corelib/global/qlogging.h | 29 | ||||
-rw-r--r-- | src/corelib/io/qloggingcategory.h | 7 | ||||
-rw-r--r-- | tests/auto/corelib/global/qlogging/app/app.pro | 1 | ||||
-rw-r--r-- | tests/auto/corelib/global/qlogging/test/test.pro | 1 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 73 |
6 files changed, 95 insertions, 24 deletions
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 575f484dd3..2a4f2dd4d6 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -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: diff --git a/src/corelib/global/qlogging.h b/src/corelib/global/qlogging.h index b952f999cb..f9bbf7fcce 100644 --- a/src/corelib/global/qlogging.h +++ b/src/corelib/global/qlogging.h @@ -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 diff --git a/src/corelib/io/qloggingcategory.h b/src/corelib/io/qloggingcategory.h index cd3a7bf789..c7e242af08 100644 --- a/src/corelib/io/qloggingcategory.h +++ b/src/corelib/io/qloggingcategory.h @@ -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) diff --git a/tests/auto/corelib/global/qlogging/app/app.pro b/tests/auto/corelib/global/qlogging/app/app.pro index b11e792a4c..a7f6e68448 100644 --- a/tests/auto/corelib/global/qlogging/app/app.pro +++ b/tests/auto/corelib/global/qlogging/app/app.pro @@ -10,6 +10,7 @@ CONFIG += console SOURCES += main.cpp DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 +DEFINES += QT_MESSAGELOGCONTEXT gcc:!mingw: QMAKE_LFLAGS += -rdynamic diff --git a/tests/auto/corelib/global/qlogging/test/test.pro b/tests/auto/corelib/global/qlogging/test/test.pro index 788a2064cd..d4dce4a0c3 100644 --- a/tests/auto/corelib/global/qlogging/test/test.pro +++ b/tests/auto/corelib/global/qlogging/test/test.pro @@ -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 diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index f1c99e5dab..015a13775d 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -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); |