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