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
4#ifndef QLOGGINGCATEGORY_H
5#define QLOGGINGCATEGORY_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qdebug.h>
9
10QT_BEGIN_NAMESPACE
11
12class Q_CORE_EXPORT QLoggingCategory
13{
14 Q_DISABLE_COPY(QLoggingCategory)
15public:
16 explicit QLoggingCategory(const char *category, QtMsgType severityLevel = QtDebugMsg);
17 ~QLoggingCategory();
18
19 bool isEnabled(QtMsgType type) const;
20 void setEnabled(QtMsgType type, bool enable);
21
22 bool isDebugEnabled() const { return bools.enabledDebug.loadRelaxed(); }
23 bool isInfoEnabled() const { return bools.enabledInfo.loadRelaxed(); }
24 bool isWarningEnabled() const { return bools.enabledWarning.loadRelaxed(); }
25 bool isCriticalEnabled() const { return bools.enabledCritical.loadRelaxed(); }
26
27 const char *categoryName() const { return name; }
28
29 // allows usage of both factory method and variable in qCX macros
30 QLoggingCategory &operator()() { return *this; }
31 const QLoggingCategory &operator()() const { return *this; }
32
33 static QLoggingCategory *defaultCategory();
34
35 typedef void (*CategoryFilter)(QLoggingCategory*);
36 static CategoryFilter installFilter(CategoryFilter);
37
38 static void setFilterRules(const QString &rules);
39
40private:
41 Q_DECL_UNUSED_MEMBER void *d = nullptr; // reserved for future use
42 const char *name = nullptr;
43
44 struct AtomicBools {
45 QBasicAtomicInteger<bool> enabledDebug;
46 QBasicAtomicInteger<bool> enabledWarning;
47 QBasicAtomicInteger<bool> enabledCritical;
48 QBasicAtomicInteger<bool> enabledInfo;
49 };
50 union {
51 AtomicBools bools;
52 QBasicAtomicInt enabled;
53 };
54 Q_DECL_UNUSED_MEMBER bool placeholder[4]; // reserved for future use
55
56 QT_DEFINE_TAG_STRUCT(UnregisteredInitialization);
57 explicit constexpr QLoggingCategory(UnregisteredInitialization, const char *category) noexcept;
58 friend class QLoggingRegistry;
59};
60
61namespace { // allow different TUs to have different QT_NO_xxx_OUTPUT
62template <QtMsgType Which> struct QLoggingCategoryMacroHolder
63{
64 static const bool IsOutputEnabled;
65 const QLoggingCategory *category = nullptr;
66 bool control = false;
67 explicit QLoggingCategoryMacroHolder(const QLoggingCategory &cat)
68 {
69 if (IsOutputEnabled)
70 init(cat);
71 }
72 void init(const QLoggingCategory &cat) noexcept
73 {
74 category = &cat;
75 // same as:
76 // control = cat.isEnabled(Which);
77 // but without an out-of-line call
78 if constexpr (Which == QtDebugMsg) {
79 control = cat.isDebugEnabled();
80 } else if constexpr (Which == QtInfoMsg) {
81 control = cat.isInfoEnabled();
82 } else if constexpr (Which == QtWarningMsg) {
83 control = cat.isWarningEnabled();
84 } else if constexpr (Which == QtCriticalMsg) {
85 control = cat.isCriticalEnabled();
86 } else if constexpr (Which == QtFatalMsg) {
87 control = true;
88 } else {
89 static_assert(QtPrivate::value_dependent_false<Which>(), "Unknown Qt message type");
90 }
91 }
92 const char *name() const { return category->categoryName(); }
93 explicit operator bool() const { return Q_UNLIKELY(control); }
94};
95
96template <QtMsgType Which> const bool QLoggingCategoryMacroHolder<Which>::IsOutputEnabled = true;
97#if defined(QT_NO_DEBUG_OUTPUT)
98template <> const bool QLoggingCategoryMacroHolder<QtDebugMsg>::IsOutputEnabled = false;
99#endif
100#if defined(QT_NO_INFO_OUTPUT)
101template <> const bool QLoggingCategoryMacroHolder<QtInfoMsg>::IsOutputEnabled = false;
102#endif
103#if defined(QT_NO_WARNING_OUTPUT)
104template <> const bool QLoggingCategoryMacroHolder<QtWarningMsg>::IsOutputEnabled = false;
105#endif
106} // unnamed namespace
107
108#define QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(name, export_macro)
109 inline namespace QtPrivateLogging { export_macro const QLoggingCategory &name(); }
110
111#ifdef QT_BUILDING_QT
112#define Q_DECLARE_LOGGING_CATEGORY(name)
113 inline namespace QtPrivateLogging { const QLoggingCategory &name(); }
114
115#define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro)
116 inline namespace QtPrivateLogging {
117 Q_DECL_DEPRECATED_X("Use QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY in Qt")
118 export_macro const QLoggingCategory &name();
119 }
120
121#define Q_LOGGING_CATEGORY_IMPL(name, ...)
122 const QLoggingCategory &name()
123 {
124 static const QLoggingCategory category(__VA_ARGS__);
125 return category;
126 }
127
128#define Q_LOGGING_CATEGORY(name, ...)
129 inline namespace QtPrivateLogging { Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__) }
130 Q_WEAK_OVERLOAD
131 Q_DECL_DEPRECATED_X("Use Q_STATIC_LOGGING_CATEGORY or add "
132 "either Q_DECLARE_LOGGING_CATEGORY or "
133 "QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY in a header")
134 const QLoggingCategory &name() { return QtPrivateLogging::name(); }
135
136#define Q_STATIC_LOGGING_CATEGORY(name, ...)
137 static Q_LOGGING_CATEGORY_IMPL(name, __VA_ARGS__)
138
139#else
140#define Q_DECLARE_LOGGING_CATEGORY(name)
141 const QLoggingCategory &name();
142
143#define Q_DECLARE_EXPORTED_LOGGING_CATEGORY(name, export_macro)
144 export_macro Q_DECLARE_LOGGING_CATEGORY(name)
145
146#define Q_LOGGING_CATEGORY(name, ...)
147 const QLoggingCategory &name()
148 {
149 static const QLoggingCategory category(__VA_ARGS__);
150 return category;
151 }
152
153#define Q_STATIC_LOGGING_CATEGORY(name, ...)
154 static Q_LOGGING_CATEGORY(name, __VA_ARGS__)
155#endif
156
157#define QT_MESSAGE_LOGGER_COMMON(category, level)
158 for (QLoggingCategoryMacroHolder<level> qt_category((category)()); qt_category; qt_category.control = false)
159 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, qt_category.name())
160
161#define qCDebug(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug(__VA_ARGS__)
162#define qCInfo(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtInfoMsg).info(__VA_ARGS__)
163#define qCWarning(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtWarningMsg).warning(__VA_ARGS__)
164#define qCCritical(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtCriticalMsg).critical(__VA_ARGS__)
165#define qCFatal(category, ...) QT_MESSAGE_LOGGER_COMMON(category, QtFatalMsg).fatal(__VA_ARGS__)
166
167QT_END_NAMESPACE
168
169#endif // QLOGGINGCATEGORY_H
QByteArray & operator*() noexcept
Definition qbytearray.h:814
QByteArray::Base64DecodingStatus decodingStatus
Definition qbytearray.h:799
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:818
void swap(QByteArray::FromBase64Result &other) noexcept
Definition qbytearray.h:801
operator bool() const noexcept
\variable QByteArray::FromBase64Result::decoded
Definition qbytearray.h:807
const QByteArray & operator*() const noexcept
Returns the decoded byte array.
Definition qbytearray.h:815
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:829
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatastream.h:49
\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 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 const char timeTokenC[]
static bool isFatalCountDown(const char *varname, QBasicAtomicInt &n)
Definition qlogging.cpp:152
static const char qthreadptrTokenC[]
static const char fileTokenC[]
static const char ifDebugTokenC[]
static const char ifFatalTokenC[]
static const char categoryTokenC[]
static const char lineTokenC[]
static const char typeTokenC[]
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[]
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[]
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
Combined button and popup list for selecting options.
LRESULT QT_WIN_CALLBACK deviceNotificationWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
QDebug printAssociativeContainer(QDebug debug, const char *which, const AssociativeContainer &c)
Definition qdebug.h:386
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:368
QByteArray operator""_ba(const char *str, size_t size) noexcept
Definition qbytearray.h:864
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:720
QByteArray qUncompress(const QByteArray &data)
Definition qbytearray.h:789
QByteArray operator+(char a1, const QByteArray &a2)
Definition qbytearray.h:730
QByteArray operator+(QByteArray &&lhs, char rhs)
Definition qbytearray.h:726
QByteArray operator+(const QByteArray &a1, char a2)
Definition qbytearray.h:724
QByteArray operator+(const char *a1, const QByteArray &a2)
Definition qbytearray.h:728
QByteArray operator+(QByteArray &&lhs, const QByteArray &rhs)
Definition qbytearray.h:718
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:847
QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Definition qbytearray.h:716
QByteArray qCompress(const QByteArray &data, int compressionLevel=-1)
Definition qbytearray.h:787
#define QT5_NULL_STRINGS
Definition qbytearray.h:25
qsizetype erase(QByteArray &ba, const T &t)
Definition qbytearray.h:841
QByteArray operator+(QByteArray &&lhs, const char *rhs)
Definition qbytearray.h:722
#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:615
Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, quint64 value)
Definition qdebug.cpp:1415
Q_CORE_EXPORT void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, uint value)
Definition qdebug.cpp:1406
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2462
#define QT_MESSAGELOG_FUNC
Definition qlogging.h:161
Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(int code, const char *msg,...)
Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void qErrnoWarning(const char *msg,...)
#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:346
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:680
static constexpr bool Value
Definition qdebug.h:676