Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QCoreApplication>
5#include <QLoggingCategory>
6
8// in a header
10
11// in one source file
12Q_LOGGING_CATEGORY(driverUsb, "driver.usb")
14
16Q_LOGGING_CATEGORY(driverUsbEvents, "driver.usb.events", QtWarningMsg)
18
19// Completely made up example, inspired by en.wikipedia.org/wiki/USB :)
20struct UsbEntry {
21 int id;
23};
24
26{
28 debug.nospace() << "" << entry.id << " (" << entry.classtype << ')';
29 return debug;
30}
31
32QList<UsbEntry> usbEntries() {
33 QList<UsbEntry> entries;
34 return entries;
35}
36
40
43
45{
46 // For a category set up after this filter is installed, we first set it up
47 // with the old filter. This ensures that any driver.usb logging configured
48 // by the user is kept, aside from the one level we override; and any new
49 // categories we're not interested in get configured by the old filter.
52
53 // Tweak driver.usb's logging, over-riding the default filter:
54 if (qstrcmp(category->categoryName(), "driver.usb") == 0)
56}
58
59int main(int argc, char *argv[])
60{
61 QCoreApplication a(argc, argv);
62
64 QLoggingCategory::setFilterRules(QStringLiteral("driver.usb.debug=true"));
66
70
72 qSetMessagePattern("%{category} %{message}");
74
76 // usbEntries() will only be called if driverUsb category is enabled
77 qCDebug(driverUsb) << "devices: " << usbEntries();
79
80 {
82 QLoggingCategory category("driver.usb");
83 qCDebug(category) << "a debug message";
85 }
86
88 QLoggingCategory category("driver.usb");
89 qCInfo(category) << "an informational message";
91
92 {
94 QLoggingCategory category("driver.usb");
95 qCWarning(category) << "a warning message";
97 }
98
99 {
101 QLoggingCategory category("driver.usb");
102 qCCritical(category) << "a critical message";
104 }
105
106 {
108 QLoggingCategory category("driver.usb");
109 qCDebug(category, "a debug message logged into category %s", category.categoryName());
111 }
112
113 {
115 QLoggingCategory category("driver.usb");
116 qCInfo(category, "an informational message logged into category %s", category.categoryName());
118 }
119
120 {
122 QLoggingCategory category("driver.usb");
123 qCWarning(category, "a warning message logged into category %s", category.categoryName());
125 }
126
127 {
129 QLoggingCategory category("driver.usb");
130 qCCritical(category, "a critical message logged into category %s", category.categoryName());
132 }
133
134 {
136 QLoggingCategory category("driver.usb");
137 qCFatal(category) << "a fatal message. Program will be terminated!";
139 }
140
141 {
143 QLoggingCategory category("driver.usb");
144 qCFatal(category, "a fatal message. Program will be terminated!");
146 }
147
148 return 0;
149}
150
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
static void setFilterRules(const QString &rules)
Configures which categories and message types should be enabled through a set of rules.
void(* CategoryFilter)(QLoggingCategory *)
This is a typedef for a pointer to a function with the following signature:
void setEnabled(QtMsgType type, bool enable)
Changes the message type type for the category to enable.
static CategoryFilter installFilter(CategoryFilter)
Take control of how logging categories are configured.
const char * categoryName() const
Returns the name of the category.
const QLoggingCategory & category()
[1]
int main()
[0]
void qSetMessagePattern(const QString &pattern)
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
@ QtWarningMsg
Definition qlogging.h:31
@ QtDebugMsg
Definition qlogging.h:30
#define Q_LOGGING_CATEGORY(name,...)
#define qCFatal(category,...)
#define qCInfo(category,...)
#define qCCritical(category,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint entry
#define QStringLiteral(str)
QDebug operator<<(QDebug &debug, const UsbEntry &entry)
Definition main.cpp:25
static QLoggingCategory::CategoryFilter oldCategoryFilter
[20]
Definition main.cpp:42
void myCategoryFilter(QLoggingCategory *)
[20]
Definition main.cpp:44
QList< UsbEntry > usbEntries()
Definition main.cpp:32
[1]
Definition main.cpp:20
int id
Definition main.cpp:21
int classtype
Definition main.cpp:22