Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does qDebug work in Release builds?

Tags:

Coming from MFC, I treated qDebug() much like TRACE(), assuming that it is removed from Release builds by the preprocessor (in MFC it's done using #define TRACE 1 ? (void*) 0 : AfxTrace).

To my surprise, however, qDebug() is executed in Release builds as well. How do I change this? And also, why is this so, what was the reasoning of the developers of Qt behind this decision?

like image 875
sashoalm Avatar asked Nov 21 '12 13:11

sashoalm


2 Answers

qDebug is also preprocessor-controlled, but it has its own special macro, QT_NO_DEBUG_OUTPUT. If you add that to your Release build defines, it will be removed.

like image 176
Angew is no longer proud of SO Avatar answered Sep 30 '22 01:09

Angew is no longer proud of SO


QDebug is "output stream for debugging information". It has it default behaviour witch is printing to stdout/stderr depending on message type. You can customize qDebug() behaviour easily by installing own message handler. For example you can test at runtime (not compile time) if you want to print debugs. Take a look at this code sample:

#include <QDebug>  void noMessageOutput(QtMsgType type, const char *msg) {      Q_UNUSED(type);      Q_UNUSED(msg); }  int main(int argc, char * argv[]) {     QApplication app(argc, argv);      if ( ! app.arguments().contains(QLatin1String("--with-debug") ) {         qInstallMsgHandler(noMessageOutput);     } } 

It will hide whole qDebug output if there is no parameter specified at runtime. You get more control than just "show debug/don't show debug"

Also you can completly disable QDebug with QT_NO_DEBUG_OUTPUT define if you're concerned about performance lost with qDebug present within code.

like image 36
Kamil Klimek Avatar answered Sep 30 '22 00:09

Kamil Klimek