Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qloggingcategory.h
Go to the documentation of this file.
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
11QT_BEGIN_NAMESPACE
12
13class Q_CORE_EXPORT QLoggingCategory
14{
15 Q_DISABLE_COPY(QLoggingCategory)
16public:
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
41private:
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
62namespace { // allow different TUs to have different QT_NO_xxx_OUTPUT
63template <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
97template <QtMsgType Which> const bool QLoggingCategoryMacroHolder<Which>::IsOutputEnabled = true;
98#if defined(QT_NO_DEBUG_OUTPUT)
99template <> const bool QLoggingCategoryMacroHolder<QtDebugMsg>::IsOutputEnabled = false;
100#endif
101#if defined(QT_NO_INFO_OUTPUT)
102template <> const bool QLoggingCategoryMacroHolder<QtInfoMsg>::IsOutputEnabled = false;
103#endif
104#if defined(QT_NO_WARNING_OUTPUT)
105template <> 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
168QT_END_NAMESPACE
169
170#endif // QLOGGINGCATEGORY_H
QByteArray & operator*() noexcept
Definition qbytearray.h:797
QByteArray::Base64DecodingStatus decodingStatus
Definition qbytearray.h:782
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:801
void swap(QByteArray::FromBase64Result &other) noexcept
Definition qbytearray.h:784
operator bool() const noexcept
\variable QByteArray::FromBase64Result::decoded
Definition qbytearray.h:790
const QByteArray & operator*() const noexcept
Returns the decoded byte array.
Definition qbytearray.h:798
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:812
\inmodule QtCore
Definition qbytearray.h:58
\inmodule QtCore\reentrant
Definition qdatastream.h:50
\inmodule QtCore
Definition qeventloop.h:59
int initFrom(const QMessageLogContext &logContext)
void populateBacktrace(int frameCount)
QInternalMessageLogContext(const QMessageLogContext &logContext, const QLoggingCategory &categoryOverride)
Definition qlogging_p.h:65
std::optional< BacktraceStorage > backtrace
Definition qlogging_p.h:57
static constexpr int DefaultBacktraceDepth
Definition qlogging_p.h:47
Definition qlist.h:80
\inmodule QtCore
Definition qlogging.h:43
constexpr QMessageLogContext(const char *fileName, int lineNumber, const char *functionName, const char *categoryName) noexcept
Definition qlogging.h:48
const char * category
Definition qlogging.h:55
constexpr QMessageLogContext() noexcept=default
const char * function
Definition qlogging.h:54
const char * file
Definition qlogging.h:53
\inmodule QtCore
Definition qlogging.h:73
QDebug debug(CategoryFunction catFunc) const
QDebug debug(const QLoggingCategory &cat) const
Logs a debug message into category cat using a QDebug stream.
Definition qlogging.cpp:525
void void void void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void Q_DECL_COLD_FUNCTION void QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION void QT_MESSAGE_LOGGER_NORETURN Q_DECL_COLD_FUNCTION void QDebug debug() const
Logs a debug message using a QDebug stream.
Definition qlogging.cpp:511
QDebug info(const QLoggingCategory &cat) const
Logs an informational message into the category cat using a QDebug stream.
Definition qlogging.cpp:614
QDebug info() const
Logs an informational message using a QDebug stream.
Definition qlogging.cpp:600
QNoDebug noDebug(...) const noexcept
QDebug info(CategoryFunction catFunc) const
static Q_CONSTINIT thread_local bool msgHandlerGrabbed
static const char ifCriticalTokenC[]
static bool grabMessageHandler()
void qt_message_output(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
static const char emptyTokenC[]
static Q_NEVER_INLINE void qt_message(QtMsgType msgType, const QMessageLogContext &context, const char *msg, va_list ap)
Definition qlogging.cpp:408
static void preformattedMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static bool systemHasStderr()
Returns true if writing to stderr is supported.
Definition qlogging.cpp:262
static const char endifTokenC[]
static bool isDefaultCategory(const char *category)
Definition qlogging.cpp:954
static const char messageTokenC[]
static bool qt_append_thread_name_to(QString &message)
Definition qlogging.cpp:247
static constexpr SystemMessageSink systemMessageSink
static void qt_maybe_message_fatal(QtMsgType, const QMessageLogContext &context, String &&message)
\inmodule QtCore \title Qt Logging Types
#define HANDLE_IF_TOKEN(LEVEL)
Q_DECLARE_TYPEINFO(QMessagePattern::BacktraceParams, Q_RELOCATABLE_TYPE)
static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &buf)
static const char timeTokenC[]
static bool isFatalCountDown(const char *varname, QBasicAtomicInt &n)
Definition qlogging.cpp:152
void qErrnoWarning(int code, const char *msg,...)
static const char qthreadptrTokenC[]
static const char fileTokenC[]
static const char ifDebugTokenC[]
static const char ifFatalTokenC[]
static const char categoryTokenC[]
static void stderr_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &formattedMessage)
static const char lineTokenC[]
static const char typeTokenC[]
static void ungrabMessageHandler()
static void copyInternalContext(QInternalMessageLogContext *self, const QMessageLogContext &logContext) noexcept
static const char ifCategoryTokenC[]
static int checked_var_value(const char *varname)
Definition qlogging.cpp:138
static const char threadnameTokenC[]
static const char pidTokenC[]
Q_TRACE_POINT(qtcore, qt_message_print, int type, const char *category, const char *function, const char *file, int line, const QString &message)
static const char threadidTokenC[]
static QString formatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
static const char backtraceTokenC[]
void qErrnoWarning(const char *msg,...)
static const char functionTokenC[]
#define IF_TOKEN(LEVEL)
static const char ifWarningTokenC[]
static const char appnameTokenC[]
static bool isFatal(QtMsgType msgType)
Definition qlogging.cpp:186
static const char ifInfoTokenC[]
QtMessageHandler qInstallMessageHandler(QtMessageHandler h)
static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message)
static bool stderrHasConsoleAttached()
Returns true if writing to stderr will end up in a console/terminal visible to the user.
Definition qlogging.cpp:287
void qSetMessagePattern(const QString &pattern)
QDebug printAssociativeContainer(QDebug debug, const char *which, const AssociativeContainer &c)
Definition qdebug.h:385
bool shouldLogToStderr()
Returns true if logging stderr should be ensured.
Definition qlogging.cpp:340
QDebug printSequentialContainer(QDebug debug, const char *which, const SequentialContainer &c)
Definition qdebug.h:367
QByteArray operator""_ba(const char *str, size_t size) noexcept
Definition qbytearray.h:847
Definition qcompare.h:76
QT_BEGIN_NAMESPACE Q_NORETURN void qAbort()
Definition qassert.cpp:24
QByteArray operator+(const QByteArray &a1, const char *a2)
Definition qbytearray.h:703
QByteArray qUncompress(const QByteArray &data)
Definition qbytearray.h:772
QByteArray operator+(char a1, const QByteArray &a2)
Definition qbytearray.h:713
QByteArray operator+(QByteArray &&lhs, char rhs)
Definition qbytearray.h:709
QByteArray operator+(const QByteArray &a1, char a2)
Definition qbytearray.h:707
QByteArray operator+(const char *a1, const QByteArray &a2)
Definition qbytearray.h:711
QByteArray operator+(QByteArray &&lhs, const QByteArray &rhs)
Definition qbytearray.h:701
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:830
QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Definition qbytearray.h:699
QByteArray qCompress(const QByteArray &data, int compressionLevel=-1)
Definition qbytearray.h:770
#define QT5_NULL_STRINGS
Definition qbytearray.h:26
qsizetype erase(QByteArray &ba, const T &t)
Definition qbytearray.h:824
QByteArray operator+(QByteArray &&lhs, const char *rhs)
Definition qbytearray.h:705
#define __has_builtin(x)
#define __has_include(x)
#define __has_cpp_attribute(x)
void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, Int value)
Definition qdebug.h:614
Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value)
Definition qdebug.cpp:1428
Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
Definition qdebug.cpp:1419
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2568
#define QT_MESSAGELOG_FUNC
Definition qlogging.h:161
#define QT_MESSAGELOG_FILE
Definition qlogging.h:159
#define QT_MESSAGE_LOGGER_NORETURN
Definition qlogging.h:69
#define QT_MESSAGELOG_LINE
Definition qlogging.h:160
Q_CORE_EXPORT void qSetMessagePattern(const QString &messagePattern)
#define QT_MESSAGELOGCONTEXT
Definition qlogging.h:154
QtMsgType
Definition qlogging.h:29
@ QtCriticalMsg
Definition qlogging.h:33
@ QtFatalMsg
Definition qlogging.h:34
@ QtDebugMsg
Definition qlogging.h:30
Q_CORE_EXPORT void qt_message_output(QtMsgType, const QMessageLogContext &context, const QString &message)
void(* QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &)
Definition qlogging.h:196
#define Q_LOGGING_CATEGORY(name,...)
#define QT_MESSAGE_LOGGER_COMMON(category, level)
#define Q_DECLARE_LOGGING_CATEGORY(name)
QMutex QBasicMutex
Definition qmutex.h:360
void setPattern(const QString &pattern)
std::unique_ptr< std::unique_ptr< const char[]>[]> literals
std::chrono::steady_clock::time_point appStartTime
std::unique_ptr< const char *[]> tokens
QList< QString > timeArgs
static QBasicMutex mutex
void setDefaultPattern()
static constexpr bool Value
Definition qdebug.h:679
static constexpr bool Value
Definition qdebug.h:675