File indexing completed on 2025-01-18 10:07:28
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 void init(const char *category, QtMsgType severityLevel);
0042
0043 Q_DECL_UNUSED_MEMBER void *d;
0044 const char *name;
0045
0046 struct AtomicBools {
0047 QBasicAtomicInteger<bool> enabledDebug;
0048 QBasicAtomicInteger<bool> enabledWarning;
0049 QBasicAtomicInteger<bool> enabledCritical;
0050 QBasicAtomicInteger<bool> enabledInfo;
0051 };
0052 union {
0053 AtomicBools bools;
0054 QBasicAtomicInt enabled;
0055 };
0056 Q_DECL_UNUSED_MEMBER bool placeholder[4];
0057 };
0058
0059 namespace {
0060 template <QtMsgType Which> struct QLoggingCategoryMacroHolder
0061 {
0062 static const bool IsOutputEnabled;
0063 const QLoggingCategory *category = nullptr;
0064 bool control = false;
0065 explicit QLoggingCategoryMacroHolder(const QLoggingCategory &cat)
0066 {
0067 if (IsOutputEnabled)
0068 init(cat);
0069 }
0070 void init(const QLoggingCategory &cat) noexcept
0071 {
0072 category = &cat;
0073
0074
0075
0076 if constexpr (Which == QtDebugMsg) {
0077 control = cat.isDebugEnabled();
0078 } else if constexpr (Which == QtInfoMsg) {
0079 control = cat.isInfoEnabled();
0080 } else if constexpr (Which == QtWarningMsg) {
0081 control = cat.isWarningEnabled();
0082 } else if constexpr (Which == QtCriticalMsg) {
0083 control = cat.isCriticalEnabled();
0084 } else if constexpr (Which == QtFatalMsg) {
0085 control = true;
0086 } else {
0087 static_assert(QtPrivate::value_dependent_false<Which>(), "Unknown Qt message type");
0088 }
0089 }
0090 const char *name() const { return category->categoryName(); }
0091 explicit operator bool() const { return Q_UNLIKELY(control); }
0092 };
0093
0094 template <QtMsgType Which> const bool QLoggingCategoryMacroHolder<Which>::IsOutputEnabled = true;
0095 #if defined(QT_NO_DEBUG_OUTPUT)
0096 template <> const bool QLoggingCategoryMacroHolder<QtDebugMsg>::IsOutputEnabled = false;
0097 #endif
0098 #if defined(QT_NO_INFO_OUTPUT)
0099 template <> const bool QLoggingCategoryMacroHolder<QtInfoMsg>::IsOutputEnabled = false;
0100 #endif
0101 #if defined(QT_NO_WARNING_OUTPUT)
0102 template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnabled = false;
0103 #endif
0104 }
0105
0106 #define Q_DECLARE_LOGGING_CATEGORY(name) \
0107 const QLoggingCategory &name();
0108
0109 #define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro) \
0110 export_macro Q_DECLARE_LOGGING_CATEGORY(name)
0111
0112 #define Q_LOGGING_CATEGORY(name, ...) \
0113 const QLoggingCategory &name() \
0114 { \
0115 static const QLoggingCategory category(__VA_ARGS__); \
0116 return category; \
0117 }
0118
0119 #define QT_MESSAGE_LOGGER_COMMON(category, level) \
0120 for (QLoggingCategoryMacroHolder<level> qt_category((category)()); qt_category; qt_category.control = false) \
0121 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, qt_category.name())
0122
0123 #define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug(__VA_ARGS__)
0124 #define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__)
0125 #define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__)
0126 #define qCCritical(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtCriticalMsg).critical(__VA_ARGS__)
0127 #define qCFatal(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtFatalMsg).fatal(__VA_ARGS__)
0128
0129 QT_END_NAMESPACE
0130
0131 #endif