Logging with Qt
Logging messages is an important part of our app and sometimes we can’t find the right way to do so. It depends on how we want to work with that but of course that Qt provide us with some nice options.
Redirect QDebug to a file
Some times we prefer to analyse the our application debug messages in a separated file. It is useful when we want to work with Qt-Creator in full code window and we have two monitors (one for coding and another for application execution and debug file). To redirect the output we have to type in Qt:
Then, in main function we have to add the following line:
qInstallMessageHandler(customMessageHandler);
Redirect QDebug in our custom class
If we want to redirect the output of our custom class to QDebug, it is necessary to declare the operator << of QDebug type as a friend method:
friend QDebug operator << (QDebug d, const Protocol &p);