Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to control qdebug info not show in terminal?



  • I'm writing an qt console application, i want to let those debug , warning, etc message only write into DebugLog file and not show in in the terminal . what's the way to do this. I have already use following code to write those information into the txt file.

    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {
        static QMutex mutex;
        mutex.lock();
    
        QString text;
        switch (type)
        {
        case QtDebugMsg:
            text = QString("Debug:");
            break;
        case QtInfoMsg:
            text = QString("Info:");
            break;
    
        case QtWarningMsg:
            text = QString("Warning:");
            break;
    
        case QtCriticalMsg:
            text = QString("Critical:");
            break;
    
        case QtFatalMsg:
            text = QString("Fatal:");
        }
    
        QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
        QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
        QString current_date = QString("(%1)").arg(current_date_time);
        QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);
    
        QFile file("DebugLog.txt");
        file.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream text_stream(&file);
        text_stream << message << "\r\n";
        file.flush();
        file.close();
    
        mutex.unlock();
    }
    


  • Hi @SpartaWHY117

    you could use something like:

    #ifndef QT_NO_DEBUG
    //DEBUG code
    #endif
    

    -Michael


  • Moderators

    @SpartaWHY117

    this Stackoverflow topic may be old, but the solution is still valid.


Log in to reply