[Development] QLog ( Work on qDebug and friends)

wolfgang.beck at nokia.com wolfgang.beck at nokia.com
Fri Feb 10 00:44:39 CET 2012


Hi Kai,

The controlling of the category will be done using a configuration file.
QLog contains a private class that creates a file watcher for the configuration file.
So not only if you start the application but during runtime as well the category filtering can be changed dynamically by changing the config file.
No recompiling needed with this solution.

Cheers,
 WB


-----Original Message-----
From: Koehne Kai (Nokia-MP/Berlin) 
Sent: Thursday, February 09, 2012 7:05 PM
To: Beck Wolfgang (Nokia-MP/Brisbane); development at qt-project.org
Subject: RE: QLog ( Work on qDebug and friends)

> -----Original Message-----
> From: Beck Wolfgang (Nokia-MP/Brisbane)
> Sent: Thursday, February 09, 2012 8:11 AM
> To: Koehne Kai (Nokia-MP/Berlin); development at qt-project.org
> Subject: RE: QLog ( Work on qDebug and friends)
> 
> Hi Kai,


Hi Wolfgang,

> I think this was a very good idea so I've tried it but I've run into 
> one important problem.
> If I use this trick I have to call the overloaded function in QMessageLogger:
> e.g.
> 1. void debug(QMessageCategory category, const char *format, ...); and 2.
> QDebug debug(QMessageCategory category);
> 
> The first function is ok but the 2. one needs to return a QDebug object.
> In this case I have to do a lots of overhead e.g. function call and 
> creating object after I can check if the category should be logged or not.
>
> So better having a other named macro which I can do
> 
> #define qLog(category) \
>    If(!logging_enable);    \
> Else QMessageCategory(....).debug()

I see. You'll indeed need something like that if you want to define which category to log or not with the help of the macro expander.

You might still get half-way there performance wise though by giving QDebug a boolean "active", and check for it in the various operator<<, e.g.

  inline QDebug &operator<<(qint64 t)        { if (!active) return *this; stream->ts << QString::number(t); return maybeSpace(); }

If everything is inlined, and the various overloads of operator<< that people can implement do the check too, this should mostly come down to just in a couple of if (false) ... being executed.

Anyhow, a more basic question I have is whether we want the filtering by category be done at runtime (e.g. in a central message handler), or at compile time (like your solution seems to be based on). Maybe there's place for both, but I thought the idea for the Qt libraries was to avoid the need for recompiles ...

Regards

Kai Koehne

> Cheers,
>  WB
> 
> 
> -----Original Message-----
> From: Koehne Kai (Nokia-MP/Berlin)
> Sent: Tuesday, February 07, 2012 5:27 PM
> To: Beck Wolfgang (Nokia-MP/Brisbane); development at qt-project.org
> Subject: RE: QLog ( Work on qDebug and friends)
> 
> Hi Wolfgang,
> 
> how about making the category a distinct type instead?
> 
> struct QMessageCategory {
>   explicit QMessageCategory(const char *name); };
> 
> class QMessageLogger {
>    void debug(const char *format, ...);
>    void debug(QMessageCategory category, const char *format, ...); }
> 
> 
> ...
> QDebugCategory debugCategory("MyApp"); // You'll typically do this in 
> one place ...
> qDebug(debugCategory, "hi there");
> 
> 
> Anyhow, you probably don't want to set the category explicitly for 
> every call in e.g. QtCore, so you can also pass it implicitly via a DEFINE:
> 
> class QMessageLogger {
>     QMessageLogger(const char *file, int line, const char *function, 
> const char
> *defaultCategory) {
>     }
> }
> 
> #define Q_DEBUG_CATEGORY "" // empty default #define qDebug 
> QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO, 
> Q_DEBUG_CATEGORY).debug
> 
> 
> And QtCore is then compiled with
> DEFINES+="Q_DEBUG_CATEGORY=QtCore". IMO both approaches are
> complementary, passing an explicit category would overwrite the 
> Q_DEBUG_CATEGORY define.
> 
> Regards
> 
> Kai
> 
> ________________________________________
> From: development-bounces+kai.koehne=nokia.com at qt-project.org
> [development-bounces+kai.koehne=nokia.com at qt-project.org] on behalf of 
> Beck Wolfgang (Nokia-MP/Brisbane)
> Sent: Tuesday, February 07, 2012 7:54 AM
> To: development at qt-project.org
> Subject: [Development] QLog ( Work on qDebug and friends)
> 
> I'm working to integrade category log with QMessageLogger & Co and 
> unfortunatelly we can not use
> qDebg(<category>) << "my message" because there is already a 
> debug(const char *msg, ...) function combining with the macro #define 
> qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug.
> 
> So I use a new function and macro:
>     QDebug debug();
>     QDebug debugCategory(const char *msg);
>     QDebug warning();
>     QDebug warningCategory(const char *msg);
>     QDebug critical();
>     QDebug criticalCategory(const char *msg);
>     QDebug fatalCategory(const char *msg);
> 
> #define qDebugCat(category) QMessageLogger(__FILE__, __LINE__,
> Q_FUNC_INFO).debugCategory(#category)
> #define qWarningCat(category) QMessageLogger(__FILE__, __LINE__,
> Q_FUNC_INFO).warningCategory(#category)
> #define qCriticalCat(category) QMessageLogger(__FILE__, __LINE__,
> Q_FUNC_INFO).criticalCategory(#category)
> #define qFatalCat(category) QMessageLogger(__FILE__, __LINE__,
> Q_FUNC_INFO).fatalCategory(#category)
> 
> Any problems with this naming conventions???
> 
> Cheers,
>  WB
> 
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list