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

Koehne Kai Kai.Koehne at digia.com
Tue Oct 15 10:10:28 CEST 2013


> -----Original Message-----
> From: Mark [mailto:markg85 at gmail.com]
> Sent: Monday, October 14, 2013 5:40 PM
> To: Koehne Kai
> Cc: development at qt-project.org
> Subject: Re: [Development] Difficulties using Qt's new categorized logging.
> How to install filters?
> [...]
> Hi Kai,
> 
> Thank you very much for your explanation! Would you also be able to
> provide an example for installFilter? Since that is the one where i can't figure
> out how it's supposed to work.

Sure. 

So what you can do is define your own category filter:

QLoggingCategory::CategoryFilter oldCategoryFilter;

void myCategoryFilter(QLoggingCategory *category)
{
    if (qstrcmp(category->categoryName(), "qt.driver.usb") == 0) {
        category->setEnabled(QtDebugMsg, true);
    } else {
        // let the default filter decide based on text rules
        *oldCategoryFilter(category);
    }
}

And install  it:

oldCategoryFilter = QLoggingCategory::installFilter(myCategoryFilter);


inside installFilter() myCategoryFilter will be called for every QLoggingCategory object already available. It will also be called whenever a new QLoggingCategory object is created, and must be reentrant (might be called from multiple threads concurrently).


For this specific example you'd be arguably better of just defining a rule by setFilterRules. installFilter is mainly useful if you're e.g. defining your own logging framework, and say enable/disable categories based on some configuration coming from a file, or network ... . It can also be used to e.g. log all categories that are created during an application run :)

I'm currently trying to improve the documentation based on your questions, please feel free to review:

https://codereview.qt-project.org/#change,68114

Regards

Kai

-- 
   Kai Köhne, Senior Software Engineer - Digia, Qt
   Digia Germany GmbH, Rudower Chaussee 13, D-12489 Berlin
   Geschäftsführer: Mika Pälsi, Juha Varelius, Anja Wasenius
   Sitz der Gesellschaft: Berlin. USt-IdNr: DE 286 306 868
   Registergericht: Amtsgericht Charlottenburg, HRB 144331 B



More information about the Development mailing list