summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2022-06-02 11:17:17 +0200
committerKai Koehne <kai.koehne@qt.io>2022-06-13 22:46:02 +0000
commit6ae2bfad411faf896fdca83a55db1e24a0b70e73 (patch)
tree0f44e341143ecc441b6e8708a1087d2e5b282230 /src/corelib/doc/snippets
parent1480588f44430e6bf005f298f62eafcd234af8da (diff)
Fix doc and example of QLoggingCategory::installCategory()
Snippet [22] was unused and the example using snippet [21] neglected to show how its oldCategoryFilter got initialized, which is what [22] does. But it turns out the example code was crashy in any case, as it left the oldCategoryFilter uninitialized (albeit probably null) until the call to installFilter() had returned, and installFilter() calls the new filter, so the new filter needs to check oldCategoryFilter before calling it. The doc also failed to explain why it's OK to not defer to the prior filter in these calls during installFilter(), so revise the doc and example so that the latter behaves sensibly and readers of the former are likely to use the function safely. This amends commit 8f0654ceb878b6c8a08c7f5b790027c26e007c13 which added the snippets and the use of one of them, but not the other. Fixes: QTBUG-49704 Pick-to: 6.4 6.3 6.2 5.15 Change-Id: Iddb8d97b0bef417d8f16e7910730cfa59ea3e715 Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
-rw-r--r--src/corelib/doc/snippets/qloggingcategory/main.cpp17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/corelib/doc/snippets/qloggingcategory/main.cpp b/src/corelib/doc/snippets/qloggingcategory/main.cpp
index b8483087c8..33aba3bf35 100644
--- a/src/corelib/doc/snippets/qloggingcategory/main.cpp
+++ b/src/corelib/doc/snippets/qloggingcategory/main.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QCoreApplication>
@@ -39,15 +39,20 @@ void myCategoryFilter(QLoggingCategory *);
//![20]
//![21]
-QLoggingCategory::CategoryFilter oldCategoryFilter;
+static QLoggingCategory::CategoryFilter oldCategoryFilter = nullptr;
void myCategoryFilter(QLoggingCategory *category)
{
- // configure driver.usb category here, otherwise forward to to default filter.
+ // For a category set up after this filter is installed, we first set it up
+ // with the old filter. This ensures that any driver.usb logging configured
+ // by the user is kept, aside from the one level we override; and any new
+ // categories we're not interested in get configured by the old filter.
+ if (oldCategoryFilter)
+ oldCategoryFilter(category);
+
+ // Tweak driver.usb's logging, over-riding the default filter:
if (qstrcmp(category->categoryName(), "driver.usb") == 0)
category->setEnabled(QtDebugMsg, true);
- else
- oldCategoryFilter(category);
}
//![21]
@@ -60,8 +65,6 @@ int main(int argc, char *argv[])
//![2]
//![22]
-
-// ...
oldCategoryFilter = QLoggingCategory::installFilter(myCategoryFilter);
//![22]