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>
This commit is contained in:
Thiago Macieira 2021-09-22 20:38:08 -07:00
parent d20b78a569
commit 04ee5795cc

View File

@ -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