Another porting question -- QLoggingCategory

Arjen Hiemstra ahiemstra at heimr.nl
Wed Aug 21 14:42:00 BST 2024


On Wednesday 21 August 2024 14:16:50 CEST Halla Rempt wrote:
> Hi,
> 
> We had setup logging in Krita as follows:
> 
> kis_debug.h:
> 
> extern const KRITAGLOBAL_EXPORT QLoggingCategory &_30009();
> extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41000();
> extern const KRITAGLOBAL_EXPORT QLoggingCategory &_41001();
> ...
> #define dbgResources qCDebug(_30009)
> #define dbgKrita qCDebug(_41000)
> #define dbgImage qCDebug(_41001)
> ...
> #define infoResources qCInfo(_30009)
> #define infoKrita qCInfo(_41000)
> #define infoImage qCInfo(_41001)
> 
> kis_debug.cpp
> 
> Q_LOGGING_CATEGORY(_30009, "krita.lib.resources", QtInfoMsg)
> Q_LOGGING_CATEGORY(_41000, "krita.general", QtInfoMsg)
> Q_LOGGING_CATEGORY(_41001, "krita.core", QtInfoMsg)
> 
> And we'd use this like:
> 
> dbgResources << "Skipping saving tag " << tag->name(false) << filename <<
> tag->resourceType();
> 
> This works fine with Qt5, but with Q6 we get errors:
> 
> 
> n file included from
> /usr/include/x86_64-linux-gnu/qt6/QtCore/QLoggingCategory:1, from
> /home/halla/dev/krita/libs/global/kis_debug.h:10, from
> /home/halla/dev/krita/libs/global/kis_algebra_2d.cpp:11:
> /usr/include/x86_64-linux-gnu/qt6/QtGui/qvectornd.h: In function ‘constexpr
> QVector2D operator/(QVector2D, float)’:
> /home/halla/dev/krita/libs/global/kis_debug.h:102:18: error: variable
> ‘qt_category’ of non-literal type
> ‘{anonymous}::QLoggingCategoryMacroHolder<QtCriticalMsg>’ in ‘constexpr’
> function 102 | #define errKrita qCCritical(_41000)
> 
>       |                  ^~~~~~~~~~
> 
> /usr/include/x86_64-linux-gnu/qt6/QtCore/qloggingcategory.h:60:35: note:
> ‘{anonymous}::QLoggingCategoryMacroHolder<QtCriticalMsg>’ is not literal
> because: 60 | template <QtMsgType Which> struct QLoggingCategoryMacroHolder
>       |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> The Qt code changed from
> 
> #if !defined(QT_NO_DEBUG_OUTPUT)
> #  define qCDebug(category, ...) \
>     for (bool qt_category_enabled = category().isDebugEnabled();
> qt_category_enabled; qt_category_enabled = false) \
> QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC,
> category().categoryName()).debug(__VA_ARGS__) #else
> #  define qCDebug(category, ...) QT_NO_QDEBUG_MACRO()
> #endif
> 
> in Qt5 to
> 
> #define QT_MESSAGE_LOGGER_COMMON(category, level) \
>     for (QLoggingCategoryMacroHolder<level> qt_category((category)());
> qt_category; qt_category.control = false) \
> QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC,
> qt_category.name())
> 
> #define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category,
> QtDebugMsg).debug(__VA_ARGS__)
> 
> And I cannot figure out how to avoid that error other than replacing all our
> defines with literal calls to qCDebug(), which is going to be a huge diff.
> 
> Does anyone have any idea?

The error has nothing to do with categorised logging, but is due to 
`kis_debug.h` redefining `QT_ASSERT()`. The change that happened is that Qt 6 
at some point added a `QT_ASSERT()` check to `QVector2D::operator/()` which is 
marked as `inline constexpr`. This works fine with Qt's version of 
`QT_ASSERT()` but your version adds logging which calls into non-constexpr 
functions which is not allowed in a constexpr function. Due to the inlining, 
the compiler uses whatever declaration that can be found at the callsite for 
the macro.

I don't think there is a way to make categorised logging work in a constexpr 
context. Personally I would recommend to not redefine macros like that and 
instead use a separate macro if you want one with logging.

- Arjen

> 
> Halla
> 
> 
> 
> 
> #define QT_MESSAGE_LOGGER_COMMON(category, level) \
>     for (QLoggingCategoryMacroHolder<level> qt_category((category)());
> qt_category; qt_category.control = false) \
> QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC,
> qt_category.name())
> 
> #define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category,
> QtDebugMsg).debug(__VA_ARGS__)






More information about the kde-devel mailing list