[Development] Work on qDebug and friends

Todd.Rose at nokia.com Todd.Rose at nokia.com
Tue Jan 24 17:25:34 CET 2012


> Hi,
> 
> I've been working on a patch to the Qt logging framework:
> http://codereview.qt-project.org/#change,13433,patchset=14 . Basically it
> redefines qDebug() and friends as a macro so that we can automatically
> capture the source file, line, and function a particular message comes from:
> 
>   qDebug("Hello world")
> 
> actually is changed by the precompiler to:
> 
>   QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO):debug("Hello
> World")
> 
> The additional information can then be processed in a custom message
> handler, that also got a new signature:
> 
> void myMessageHandler(QtMsgType type, const QMessageLogContext
> &context, const char *msg) {
>   switch (type) {
>     case QtDebugMsg:
>         printf("DBG: %s (%s:%i in %s)\n", msg, context.file, context.line,
> context.function);  }
>         break;
>    // ...
> }
> 
> int main(int argc, char **argv)
> {
>   qInstallMessageHandler(myMessageHandler);
> }
> 
> 
> 
> The patch is almost ready to go in ... Anyhow, the question is whether there's
> additional things we need to have in QtCore to make the logging framework
> of Qt first-class? See my second mail regarding debug areas ...
> 
> --
> Kai Koehne


The "additional things we need" question is pretty subjective, but there are probably quite a few things that would be "nice to have".  I usually just copy all my logging/tracing/profiling macros around from project to project.  There are certain things that you pretty much always need in any non-trivial app:

- capture log stream and write to file (and manage log file sizes and rotation strategy)
- add current time and current thread id to each log message
- an easy and consistent way to compile out expensive verbose debugging code (not just the qDebug() call, but also the code that computes the arguments that are passed)
- macros similar to qDebug but that are used to provide simple profiling (timing of function/code-block durations)
- support for utf8 in the log message

I would also like to see more effort put into the part of QtCreator that parses log messages and displays hyperlinks to source files.  The current regex code there needs to be loosened up a bit and be more forgiving as far as where in the message the __FILE__:__LINE__ portion is.  This is a really nice feature when it works, and one of the best uses of the __FILE__ and __LINE__ info, but at the moment it doesn't always work correctly because running your app in different contexts (desktop/simulator/on-device) will end up with different stuff prepended to your log message before it gets to the app output window.

Most of the above is achievable by writing custom message handlers and custom macros.  I guess the question is whether that's good enough or whether you want to provide more functionality out-of-the-box.

BR,
-Todd








More information about the Development mailing list