diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-09-22 20:38:08 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2021-11-06 20:43:22 -0700 |
commit | 04ee5795cc31ee81fb0c27bf55d9e8f662995753 (patch) | |
tree | 53d969276318a667b481527b89f6fd01c4cb2055 /src/corelib/io/qloggingcategory.h | |
parent | d20b78a5698abf702e36e7bb9dc4a81720cc2c6d (diff) |
qC{Debug,Info,Warning,Critical}: Simplify #ifndef QT_NO_xxx_OUTPUT
Further simplifies the definition of the macros.
The class is placed in an unnamed namespace so two translation units can
still have different settings.
A difference is that the expanded no-output code is now using QDebug,
not QNoDebug, but it should get dead-code-eliminated either way.
Change-Id: I3eb1bd30e0124f89a052fffd16a7564f4450ec8a
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
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 |