diff options
Diffstat (limited to 'src/corelib/io/qloggingcategory.h')
-rw-r--r-- | src/corelib/io/qloggingcategory.h | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/src/corelib/io/qloggingcategory.h b/src/corelib/io/qloggingcategory.h index 2314c388b0..5a66bb2e68 100644 --- a/src/corelib/io/qloggingcategory.h +++ b/src/corelib/io/qloggingcategory.h @@ -106,13 +106,25 @@ private: Q_DECL_UNUSED_MEMBER bool placeholder[4]; // reserved for future use }; +namespace { // allow different TUs to have different QT_NO_xxx_OUTPUT template <QtMsgType Which> struct QLoggingCategoryMacroHolder { - const QLoggingCategory &category; - bool control; + static const bool IsOutputEnabled; + const QLoggingCategory *category = nullptr; + bool control = false; QLoggingCategoryMacroHolder(const QLoggingCategory &cat) - : category(cat) { + if (IsOutputEnabled) + init(cat); + } + QLoggingCategoryMacroHolder(QMessageLogger::CategoryFunction catfunc) + { + if (IsOutputEnabled) + init(catfunc()); + } + void init(const QLoggingCategory &cat) noexcept + { + category = &cat; // same as: // control = cat.isEnabled(Which); // but without an out-of-line call @@ -126,10 +138,22 @@ template <QtMsgType Which> struct QLoggingCategoryMacroHolder control = cat.isCriticalEnabled(); } } - operator const char *() const { return category.categoryName(); } + operator const char *() const { return category->categoryName(); } operator bool() const { return Q_UNLIKELY(control); } }; +template <QtMsgType Which> const bool QLoggingCategoryMacroHolder<Which>::IsOutputEnabled = true; +#if defined(QT_NO_DEBUG_OUTPUT) +template <> const bool QLoggingCategoryMacroHolder<QtDebugMsg>::IsOutputEnabled = false; +#endif +#if defined(QT_NO_INFO_OUTPUT) +template <> const bool QLoggingCategoryMacroHolder<QtInfoMsg>::IsOutputEnabled = false; +#endif +#if defined(QT_NO_WARNING_OUTPUT) +template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnabled = false; +#endif +} // unnamed namespace + #define Q_DECLARE_LOGGING_CATEGORY(name) \ extern const QLoggingCategory &name(); @@ -140,29 +164,13 @@ template <QtMsgType Which> struct QLoggingCategoryMacroHolder return category; \ } - #define QT_MESSAGE_LOGGER_COMMON(category, level) \ - for (QLoggingCategoryMacroHolder<level> qt_category(category()); qt_category; qt_category.control = false) \ + for (QLoggingCategoryMacroHolder<level> qt_category(category); qt_category; qt_category.control = false) \ QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, qt_category) -#if !defined(QT_NO_DEBUG_OUTPUT) -# define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug(__VA_ARGS__) -#else -# define qCDebug(category, ...) QT_NO_QDEBUG_MACRO() -#endif - -#if !defined(QT_NO_INFO_OUTPUT) -# define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__) -#else -# define qCInfo(category, ...) QT_NO_QDEBUG_MACRO() -#endif - -#if !defined(QT_NO_WARNING_OUTPUT) -# define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__) -#else -# define qCWarning(category, ...) QT_NO_QDEBUG_MACRO() -#endif - +#define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug(__VA_ARGS__) +#define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__) +#define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__) #define qCCritical(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtCriticalMsg).critical(__VA_ARGS__) QT_END_NAMESPACE |