[Development] Difficulties using Qt's new categorized logging. How to install filters?

Koehne Kai Kai.Koehne at digia.com
Mon Oct 14 12:08:26 CEST 2013


> -----Original Message-----
> From: development-bounces+kai.koehne=digia.com at qt-project.org
> [mailto:development-bounces+kai.koehne=digia.com at qt-project.org] On
> Behalf Of Mark
> Sent: Saturday, October 12, 2013 3:41 PM
> To: development at qt-project.org
> Subject: [Development] Difficulties using Qt's new categorized logging. How
> to install filters?
> 
> Hi,
> 
> I just tried fiddling with the new Qt categorized logging and am hitting some
> difficulties. I've read the documentation [1] and had a discussion on irc (Qt-
> Labs) about how to install a global filter.
> 
> The documentation states:
> // in a header
> Q_DECLARE_LOGGING_CATEGORY(QT_DRIVER_USB)
> 
> // in one source file
> Q_LOGGING_CATEGORY(QT_DRIVER_USB, "qt.driver.usb")
> 
> // usbEntries() will only be called if QT_DRIVER_USB category is enabled
> qCDebug(QT_DRIVER_USB) << "devices: " << usbEntries();
> 
> So the above line will only log when QT_DRIVER_USB is enabled. How do i
> enable it? It's a new category "qt.driver.usb" so by that logic i'm inclined to
> think that i need to install a new filter for this category and enable debugging
> in there. That would be something like this:
> 
> QLoggingCategory usbCategory("qt.driver.usb");
> usbCategory.setEnabled(QtDebugMsg, true);
> 
> Now if i do:
> qCDebug(usbCategory) << "some logging";
> 
> 
> Then it works as expected. But that's not what i want and not global. How can
> i get the initial example to work - with QT_DRIVER_USB?
>
> I think it would be a good idea to update the documentation with this
> information as well. I tried following the testcase, but that didn't help me
> much in getting this working either.

Hi Mark,

Indeed the documentation could be more verbose about this ... I'll try to come up with a patch later on. Anyhow, the bottom line is that you should either set logging rules via 

http://doc-snapshot.qt-project.org/qt5-stable/qloggingcategory.html#setFilterRules

or - if you need more control - set your own filter for categories:

http://doc-snapshot.qt-project.org/qt5-stable/qloggingcategory.html#installFilter


So, for your case, you could e.g. in main.cpp just add a line

QLoggingCategory::setFilterRules(QStringLiteral("qt.driver.usb.debug = true"));

Regards

Kai



More information about the Development mailing list