summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <[email protected]>2024-06-05 09:11:57 +0200
committerUlf Hermann <[email protected]>2024-06-20 10:51:54 +0200
commitfa4bd30caa079a3b1e5eac1bb4f17365f456b8f9 (patch)
tree608f0d4ced9a7c30e45472a6397010d89944e8df
parent2b1312bc954b72f59d0c1d7b1192e2f029016012 (diff)
Hide logging categories in Qt libs in a private namespace
Place Qt logging category symbols in a QtPrivateLogging namespace. This makes sure that the symbols cannot clash for static builds. Since Qt-internal logging categories are now different from user logging categories, we need to provide a special macro to declare them in headers that may be included in user (or QPA) code. This causes compile errors in some cases: 1. You cannot have inline logging categories in a header anymore. That has always been a bad idea, though. You don't want the static member to be replicated at each usage site. 2. You actually need the new export macro to declare Qt-internal logging categories for usage in QPA plugins or autotests. 3. You actually need to use the STATIC and EXPORTED variants of the logging category declarations rather than just prepending 'static' or the export macro now. There was a reason for introducing them. 4. If you manually define the logging category function, but use a macro to declare it, you need to adapt. There is one case of this in qtdeclarative, but you really shouldn't rely on the internals of the macro to infer what function you need to define. 5. If you have logging categories in multiple namespaces, you need to define the logging categories before 'using' any of the namespaces. Otherwise the compiler will not know which QtPrivateLogging namespace you mean. GCC before version 10 does not like weak overloads of "using" declarations. Therefore, we repeat the "using" declaration for these compilers. We need the weak overloads where we can have them in order to deprecate non-static logging categories that weren't forward-declared in the next step. Fixes: QTBUG-67692 Change-Id: Idb26dadf709378f82046fa5b568f276848134282 Reviewed-by: Thiago Macieira <[email protected]>
-rw-r--r--src/corelib/io/qloggingcategory.h38
-rw-r--r--src/corelib/io/qloggingregistry_p.h22
2 files changed, 57 insertions, 3 deletions
diff --git a/src/corelib/io/qloggingcategory.h b/src/corelib/io/qloggingcategory.h
index 1b6ce688cb4..1b2731fa1e0 100644
--- a/src/corelib/io/qloggingcategory.h
+++ b/src/corelib/io/qloggingcategory.h
@@ -104,8 +104,43 @@ template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnable
} // unnamed namespace
#define QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(name, export_macro) \
- export_macro Q_DECLARE_LOGGING_CATEGORY(name)
+ namespace QtPrivateLogging { export_macro const QLoggingCategory &name(); } \
+ using QtPrivateLogging::name;
+
+#if QT_BUILDING_QT
+#define Q_DECLARE_LOGGING_CATEGORY(name) \
+ namespace QtPrivateLogging { const QLoggingCategory &name(); } \
+ using QtPrivateLogging::name;
+
+#define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro) \
+ namespace QtPrivateLogging { export_macro const QLoggingCategory &name(); } \
+ using QtPrivateLogging::name;
+
+#define Q_LOGGING_CATEGORY_IMPL(name, ...) \
+ const QLoggingCategory &name() \
+ { \
+ static const QLoggingCategory category(__VA_ARGS__); \
+ return category; \
+ }
+#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU < 1000
+// GCC <10 thinks the "using" declaration from QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY
+// or Q_DECLARE_LOGGING_CATEGORY conflicts with any weak overload created as part of the definition.
+// So let's make it happy and repeat the "using" instead.
+#define Q_LOGGING_CATEGORY(name, ...) \
+ namespace QtPrivateLogging { Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__) } \
+ using QtPrivateLogging::name;
+#else
+#define Q_LOGGING_CATEGORY(name, ...) \
+ namespace QtPrivateLogging { Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__) } \
+ Q_WEAK_OVERLOAD \
+ const QLoggingCategory &name() { return QtPrivateLogging::name(); }
+#endif
+
+#define Q_STATIC_LOGGING_CATEGORY(name, ...) \
+ static Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__)
+
+#else
#define Q_DECLARE_LOGGING_CATEGORY(name) \
const QLoggingCategory &name();
@@ -121,6 +156,7 @@ template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnable
#define Q_STATIC_LOGGING_CATEGORY(name, ...) \
static Q_LOGGING_CATEGORY(name, __VA_ARGS__)
+#endif
#define QT_MESSAGE_LOGGER_COMMON(category, level) \
for (QLoggingCategoryMacroHolder<level> qt_category((category)()); qt_category; qt_category.control = false) \
diff --git a/src/corelib/io/qloggingregistry_p.h b/src/corelib/io/qloggingregistry_p.h
index 42738891f7f..18683294f28 100644
--- a/src/corelib/io/qloggingregistry_p.h
+++ b/src/corelib/io/qloggingregistry_p.h
@@ -29,7 +29,7 @@ class tst_QLoggingRegistry;
QT_BEGIN_NAMESPACE
-#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
+#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName) \
const QLoggingCategory &name() \
{ \
static constexpr char cname[] = categoryName; \
@@ -39,8 +39,26 @@ QT_BEGIN_NAMESPACE
return category; \
}
+#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU < 1000
+// GCC <10 thinks the "using" declaration from QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY
+// or Q_DECLARE_LOGGING_CATEGORY conflicts with any weak overload created as part of the definition.
+// So let's make it happy and repeat the "using" instead.
+#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
+ namespace QtPrivateLogging { \
+ Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName) \
+ } \
+ using QtPrivateLogging::name;
+#else
+#define Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
+ namespace QtPrivateLogging { \
+ Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName) \
+ } \
+ Q_WEAK_OVERLOAD \
+ const QLoggingCategory &name() { return QtPrivateLogging::name(); }
+#endif
+
#define Q_STATIC_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName) \
- static Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE(name, env, categoryName)
+ static Q_LOGGING_CATEGORY_WITH_ENV_OVERRIDE_IMPL(name, env, categoryName)
class Q_AUTOTEST_EXPORT QLoggingRule
{