File indexing completed on 2026-05-18 08:32:29
0001
0002
0003
0004
0005 #ifndef QLOGGINGCATEGORY_H
0006 #define QLOGGINGCATEGORY_H
0007
0008 #include <QtCore/qglobal.h>
0009 #include <QtCore/qdebug.h>
0010
0011 QT_BEGIN_NAMESPACE
0012
0013 class Q_CORE_EXPORT QLoggingCategory
0014 {
0015 Q_DISABLE_COPY(QLoggingCategory)
0016 public:
0017 explicit QLoggingCategory(const char *category, QtMsgType severityLevel = QtDebugMsg);
0018 ~QLoggingCategory();
0019
0020 bool isEnabled(QtMsgType type) const;
0021 void setEnabled(QtMsgType type, bool enable);
0022
0023 bool isDebugEnabled() const { return bools.enabledDebug.loadRelaxed(); }
0024 bool isInfoEnabled() const { return bools.enabledInfo.loadRelaxed(); }
0025 bool isWarningEnabled() const { return bools.enabledWarning.loadRelaxed(); }
0026 bool isCriticalEnabled() const { return bools.enabledCritical.loadRelaxed(); }
0027
0028 const char *categoryName() const { return name; }
0029
0030
0031 QLoggingCategory &operator()() { return *this; }
0032 const QLoggingCategory &operator()() const { return *this; }
0033
0034 static QLoggingCategory *defaultCategory();
0035
0036 typedef void (*CategoryFilter)(QLoggingCategory*);
0037 static CategoryFilter installFilter(CategoryFilter);
0038
0039 static void setFilterRules(const QString &rules);
0040
0041 private:
0042 Q_DECL_UNUSED_MEMBER void *d = nullptr;
0043 const char *name = nullptr;
0044
0045 struct AtomicBools {
0046 QBasicAtomicInteger<bool> enabledDebug;
0047 QBasicAtomicInteger<bool> enabledWarning;
0048 QBasicAtomicInteger<bool> enabledCritical;
0049 QBasicAtomicInteger<bool> enabledInfo;
0050 };
0051 union {
0052 AtomicBools bools;
0053 QBasicAtomicInt enabled;
0054 };
0055 Q_DECL_UNUSED_MEMBER bool placeholder[4];
0056
0057 QT_DEFINE_TAG_STRUCT(UnregisteredInitialization);
0058 explicit constexpr QLoggingCategory(UnregisteredInitialization, const char *category) noexcept;
0059 friend class QLoggingRegistry;
0060 };
0061
0062 namespace {
0063 template <QtMsgType Which> struct QLoggingCategoryMacroHolder
0064 {
0065 static const bool IsOutputEnabled;
0066 const QLoggingCategory *category = nullptr;
0067 bool control = false;
0068 explicit QLoggingCategoryMacroHolder(const QLoggingCategory &cat)
0069 {
0070 if (IsOutputEnabled)
0071 init(cat);
0072 }
0073 void init(const QLoggingCategory &cat) noexcept
0074 {
0075 category = &cat;
0076
0077
0078
0079 if constexpr (Which == QtDebugMsg) {
0080 control = cat.isDebugEnabled();
0081 } else if constexpr (Which == QtInfoMsg) {
0082 control = cat.isInfoEnabled();
0083 } else if constexpr (Which == QtWarningMsg) {
0084 control = cat.isWarningEnabled();
0085 } else if constexpr (Which == QtCriticalMsg) {
0086 control = cat.isCriticalEnabled();
0087 } else if constexpr (Which == QtFatalMsg) {
0088 control = true;
0089 } else {
0090 static_assert(QtPrivate::value_dependent_false<Which>(), "Unknown Qt message type");
0091 }
0092 }
0093 const char *name() const { return category->categoryName(); }
0094 explicit operator bool() const { return Q_UNLIKELY(control); }
0095 };
0096
0097 template <QtMsgType Which> const bool QLoggingCategoryMacroHolder<Which>::IsOutputEnabled = true;
0098 #if defined(QT_NO_DEBUG_OUTPUT)
0099 template <> const bool QLoggingCategoryMacroHolder<QtDebugMsg>::IsOutputEnabled = false;
0100 #endif
0101 #if defined(QT_NO_INFO_OUTPUT)
0102 template <> const bool QLoggingCategoryMacroHolder<QtInfoMsg>::IsOutputEnabled = false;
0103 #endif
0104 #if defined(QT_NO_WARNING_OUTPUT)
0105 template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnabled = false;
0106 #endif
0107 }
0108
0109 #define QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(name, export_macro) \
0110 inline namespace QtPrivateLogging { export_macro const QLoggingCategory &name(); }
0111
0112 #ifdef QT_BUILDING_QT
0113 #define Q_DECLARE_LOGGING_CATEGORY(name) \
0114 inline namespace QtPrivateLogging { const QLoggingCategory &name(); }
0115
0116 #define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro) \
0117 inline namespace QtPrivateLogging { \
0118 Q_DECL_DEPRECATED_X("Use QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY in Qt") \
0119 export_macro const QLoggingCategory &name(); \
0120 }
0121
0122 #define Q_LOGGING_CATEGORY_IMPL(name, ...) \
0123 const QLoggingCategory &name() \
0124 { \
0125 static const QLoggingCategory category(__VA_ARGS__); \
0126 return category; \
0127 }
0128
0129 #define Q_LOGGING_CATEGORY(name, ...) \
0130 inline namespace QtPrivateLogging { Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__) } \
0131 Q_WEAK_OVERLOAD \
0132 Q_DECL_DEPRECATED_X("Use Q_STATIC_LOGGING_CATEGORY or add " \
0133 "either Q_DECLARE_LOGGING_CATEGORY or " \
0134 "QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY in a header") \
0135 const QLoggingCategory &name() { return QtPrivateLogging::name(); }
0136
0137 #define Q_STATIC_LOGGING_CATEGORY(name, ...) \
0138 static Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__)
0139
0140 #else
0141 #define Q_DECLARE_LOGGING_CATEGORY(name) \
0142 const QLoggingCategory &name();
0143
0144 #define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro) \
0145 export_macro Q_DECLARE_LOGGING_CATEGORY(name)
0146
0147 #define Q_LOGGING_CATEGORY(name, ...) \
0148 const QLoggingCategory &name() \
0149 { \
0150 static const QLoggingCategory category(__VA_ARGS__); \
0151 return category; \
0152 }
0153
0154 #define Q_STATIC_LOGGING_CATEGORY(name, ...) \
0155 static Q_LOGGING_CATEGORY(name, __VA_ARGS__)
0156 #endif
0157
0158 #define QT_MESSAGE_LOGGER_COMMON(category, level) \
0159 for (QLoggingCategoryMacroHolder<level> qt_category((category)()); qt_category; qt_category.control = false) \
0160 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, qt_category.name())
0161
0162 #define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug(__VA_ARGS__)
0163 #define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__)
0164 #define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__)
0165 #define qCCritical(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtCriticalMsg).critical(__VA_ARGS__)
0166 #define qCFatal(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtFatalMsg).fatal(__VA_ARGS__)
0167
0168 QT_END_NAMESPACE
0169
0170 #endif