| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QLOGGINGCATEGORY_H |
| 6 | #define QLOGGINGCATEGORY_H |
| 7 | |
| 8 | #include <QtCore/qglobal.h> |
| 9 | #include <QtCore/qdebug.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | class Q_CORE_EXPORT QLoggingCategory |
| 14 | { |
| 15 | Q_DISABLE_COPY(QLoggingCategory) |
| 16 | public: |
| 17 | explicit QLoggingCategory(const char *category, QtMsgType severityLevel = QtDebugMsg); |
| 18 | ~QLoggingCategory(); |
| 19 | |
| 20 | bool isEnabled(QtMsgType type) const; |
| 21 | void setEnabled(QtMsgType type, bool enable); |
| 22 | |
| 23 | bool isDebugEnabled() const { return bools.enabledDebug.loadRelaxed(); } |
| 24 | bool isInfoEnabled() const { return bools.enabledInfo.loadRelaxed(); } |
| 25 | bool isWarningEnabled() const { return bools.enabledWarning.loadRelaxed(); } |
| 26 | bool isCriticalEnabled() const { return bools.enabledCritical.loadRelaxed(); } |
| 27 | |
| 28 | const char *categoryName() const { return name; } |
| 29 | |
| 30 | // allows usage of both factory method and variable in qCX macros |
| 31 | QLoggingCategory &operator()() { return *this; } |
| 32 | const QLoggingCategory &operator()() const { return *this; } |
| 33 | |
| 34 | static QLoggingCategory *defaultCategory(); |
| 35 | |
| 36 | typedef void (*CategoryFilter)(QLoggingCategory*); |
| 37 | static CategoryFilter installFilter(CategoryFilter); |
| 38 | |
| 39 | static void setFilterRules(const QString &rules); |
| 40 | |
| 41 | private: |
| 42 | Q_DECL_UNUSED_MEMBER void *d = nullptr; // reserved for future use |
| 43 | const char *name = nullptr; |
| 44 | |
| 45 | struct AtomicBools { |
| 46 | QBasicAtomicInteger<bool> enabledDebug; |
| 47 | QBasicAtomicInteger<bool> enabledWarning; |
| 48 | QBasicAtomicInteger<bool> enabledCritical; |
| 49 | QBasicAtomicInteger<bool> enabledInfo; |
| 50 | }; |
| 51 | union { |
| 52 | AtomicBools bools; |
| 53 | QBasicAtomicInt enabled; |
| 54 | }; |
| 55 | Q_DECL_UNUSED_MEMBER bool placeholder[4]; // reserved for future use |
| 56 | |
| 57 | QT_DEFINE_TAG_STRUCT(UnregisteredInitialization); |
| 58 | explicit constexpr QLoggingCategory(UnregisteredInitialization, const char *category) noexcept; |
| 59 | friend class QLoggingRegistry; |
| 60 | }; |
| 61 | |
| 62 | namespace { // allow different TUs to have different QT_NO_xxx_OUTPUT |
| 63 | template <QtMsgType Which> struct QLoggingCategoryMacroHolder |
| 64 | { |
| 65 | static const bool IsOutputEnabled; |
| 66 | const QLoggingCategory *category = nullptr; |
| 67 | bool control = false; |
| 68 | explicit QLoggingCategoryMacroHolder(const QLoggingCategory &cat) |
| 69 | { |
| 70 | if (IsOutputEnabled) |
| 71 | init(cat); |
| 72 | } |
| 73 | void init(const QLoggingCategory &cat) noexcept |
| 74 | { |
| 75 | category = &cat; |
| 76 | // same as: |
| 77 | // control = cat.isEnabled(Which); |
| 78 | // but without an out-of-line call |
| 79 | if constexpr (Which == QtDebugMsg) { |
| 80 | control = cat.isDebugEnabled(); |
| 81 | } else if constexpr (Which == QtInfoMsg) { |
| 82 | control = cat.isInfoEnabled(); |
| 83 | } else if constexpr (Which == QtWarningMsg) { |
| 84 | control = cat.isWarningEnabled(); |
| 85 | } else if constexpr (Which == QtCriticalMsg) { |
| 86 | control = cat.isCriticalEnabled(); |
| 87 | } else if constexpr (Which == QtFatalMsg) { |
| 88 | control = true; |
| 89 | } else { |
| 90 | static_assert(QtPrivate::value_dependent_false<Which>(), "Unknown Qt message type" ); |
| 91 | } |
| 92 | } |
| 93 | const char *name() const { return category->categoryName(); } |
| 94 | explicit operator bool() const { return Q_UNLIKELY(control); } |
| 95 | }; |
| 96 | |
| 97 | template <QtMsgType Which> const bool QLoggingCategoryMacroHolder<Which>::IsOutputEnabled = true; |
| 98 | #if defined(QT_NO_DEBUG_OUTPUT) |
| 99 | template <> const bool QLoggingCategoryMacroHolder<QtDebugMsg>::IsOutputEnabled = false; |
| 100 | #endif |
| 101 | #if defined(QT_NO_INFO_OUTPUT) |
| 102 | template <> const bool QLoggingCategoryMacroHolder<QtInfoMsg>::IsOutputEnabled = false; |
| 103 | #endif |
| 104 | #if defined(QT_NO_WARNING_OUTPUT) |
| 105 | template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnabled = false; |
| 106 | #endif |
| 107 | } // unnamed namespace |
| 108 | |
| 109 | #define QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(name, export_macro) \ |
| 110 | inline namespace QtPrivateLogging { export_macro const QLoggingCategory &name(); } |
| 111 | |
| 112 | #ifdef QT_BUILDING_QT |
| 113 | #define Q_DECLARE_LOGGING_CATEGORY(name) \ |
| 114 | inline namespace QtPrivateLogging { const QLoggingCategory &name(); } |
| 115 | |
| 116 | #define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro) \ |
| 117 | inline namespace QtPrivateLogging { \ |
| 118 | Q_DECL_DEPRECATED_X("Use QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY in Qt") \ |
| 119 | export_macro const QLoggingCategory &name(); \ |
| 120 | } |
| 121 | |
| 122 | #define Q_LOGGING_CATEGORY_IMPL(name, ...) \ |
| 123 | const QLoggingCategory &name() \ |
| 124 | { \ |
| 125 | static const QLoggingCategory category(__VA_ARGS__); \ |
| 126 | return category; \ |
| 127 | } |
| 128 | |
| 129 | #define Q_LOGGING_CATEGORY(name, ...) \ |
| 130 | inline namespace QtPrivateLogging { Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__) } \ |
| 131 | Q_WEAK_OVERLOAD \ |
| 132 | Q_DECL_DEPRECATED_X("Use Q_STATIC_LOGGING_CATEGORY or add " \ |
| 133 | "either Q_DECLARE_LOGGING_CATEGORY or " \ |
| 134 | "QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY in a header") \ |
| 135 | const QLoggingCategory &name() { return QtPrivateLogging::name(); } |
| 136 | |
| 137 | #define Q_STATIC_LOGGING_CATEGORY(name, ...) \ |
| 138 | static Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__) |
| 139 | |
| 140 | #else |
| 141 | #define Q_DECLARE_LOGGING_CATEGORY(name) \ |
| 142 | const QLoggingCategory &name(); |
| 143 | |
| 144 | #define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro) \ |
| 145 | export_macro Q_DECLARE_LOGGING_CATEGORY(name) |
| 146 | |
| 147 | #define Q_LOGGING_CATEGORY(name, ...) \ |
| 148 | const QLoggingCategory &name() \ |
| 149 | { \ |
| 150 | static const QLoggingCategory category(__VA_ARGS__); \ |
| 151 | return category; \ |
| 152 | } |
| 153 | |
| 154 | #define Q_STATIC_LOGGING_CATEGORY(name, ...) \ |
| 155 | static Q_LOGGING_CATEGORY(name, __VA_ARGS__) |
| 156 | #endif |
| 157 | |
| 158 | #define QT_MESSAGE_LOGGER_COMMON(category, level) \ |
| 159 | for (QLoggingCategoryMacroHolder<level> qt_category((category)()); qt_category; qt_category.control = false) \ |
| 160 | QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, qt_category.name()) |
| 161 | |
| 162 | #define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug(__VA_ARGS__) |
| 163 | #define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__) |
| 164 | #define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__) |
| 165 | #define qCCritical(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtCriticalMsg).critical(__VA_ARGS__) |
| 166 | #define qCFatal(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtFatalMsg).fatal(__VA_ARGS__) |
| 167 | |
| 168 | QT_END_NAMESPACE |
| 169 | |
| 170 | #endif // QLOGGINGCATEGORY_H |
| 171 | |