diff options
author | Kai Koehne <kai.koehne@digia.com> | 2013-10-14 14:08:41 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-16 17:10:15 +0200 |
commit | 8f0654ceb878b6c8a08c7f5b790027c26e007c13 (patch) | |
tree | cf5e06a5d28129ba6d699720d9cf3c285c3b68af /src/corelib/doc | |
parent | 42f0a4f2c91800830021ac974678e46d52f23ae7 (diff) |
Give an example how to configure categorized logging
Change-Id: I178bb6e75fba246932b318c2c0c5afd243575a92
Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
Reviewed-by: hjk <hjk121@nokiamail.com>
Diffstat (limited to 'src/corelib/doc')
-rw-r--r-- | src/corelib/doc/snippets/qloggingcategory/main.cpp | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/src/corelib/doc/snippets/qloggingcategory/main.cpp b/src/corelib/doc/snippets/qloggingcategory/main.cpp index 27d64e8253..c1dad7f43a 100644 --- a/src/corelib/doc/snippets/qloggingcategory/main.cpp +++ b/src/corelib/doc/snippets/qloggingcategory/main.cpp @@ -67,24 +67,45 @@ QList<UsbEntry> usbEntries() { return entries; } -void main(int argc, char *argv[]) +//![20] +void myCategoryFilter(QLoggingCategory *); +//![20] + +//![21] +QLoggingCategory::CategoryFilter oldCategoryFilter; + +void myCategoryFilter(QLoggingCategory *category) +{ + // configure qt.driver.usb category here, otherwise forward to to default filter. + if (qstrcmp(category->categoryName(), "qt.driver.usb") == 0) + category->setEnabled(QtDebugMsg, true); + else + oldCategoryFilter(category); +} +//![21] + +int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //![2] - // don't run the expensive code if the string won't print - if (QT_DRIVER_USB().isDebugEnabled()) { - QStringList items; - foreach (const UsbEntry &entry, usbEntries()) - items << QString("%1 (%2)").arg(entry.id, entry.classtype); - qCDebug(QT_DRIVER_USB) << "devices: " << items; - } + QLoggingCategory::setFilterRules(QStringLiteral("qt.driver.usb.debug=true")); //![2] +//![22] + +// ... +oldCategoryFilter = QLoggingCategory::installFilter(myCategoryFilter); +//![22] + +//![3] + qSetMessagePattern("%{category} %{message}"); //![3] + +//![4] // usbEntries() will only be called if QT_DRIVER_USB category is enabled qCDebug(QT_DRIVER_USB) << "devices: " << usbEntries(); -//![3] +//![4] { //![10] @@ -106,8 +127,7 @@ void main(int argc, char *argv[]) qCCritical(category) << "a critical message"; //![12] } + + return 0; } -//![20] -void myCategoryFilter(QLoggingCategory *); -//![20] |