Tuesday, November 24, 2009

Persistent logging (for debug purposes)

Many of us, Qt developers, resort to qDebug() (and the companion qWarning(), qCritical() and qFatal() functions) to write out debugging and tracing information along the flow of our application.

Most of the time, it's just a question of sending the desired message (a QtMsgType value) to the output stream provided by qDebug():
qDebug() << "Date: " << QDate::currentDate();
Moreover, the streaming operator << can be extended to provide support for custom types, for instance:
QDebug operator<<(QDebug dbg, const Circle &c)
{
    dbg.nospace() << "Radius: " << c.radius();
    return dbg.space();
}

Make it persistent
What if we need to get a copy of all the debug messages thrown by the application during a debugging session?