欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt存储QDebug信息到文件

程序员文章站 2022-03-31 11:06:03
...

注册log handle

直接上代码吧,

#ifndef LOGHELPER_H
#define LOGHELPER_H

#include <QString>
#include <QFile>
#include <QTextStream>
#include <QDebug>

class logHelper
{
public:
    static logHelperInit()
    {
#ifndef QT_DEBUG
        logFile.setFileName("log.txt");
        logFile.open(QIODevice::WriteOnly | QIODevice::Append);
        if (logFile.size() > 1024*1024)
        {
            logFile.close();
            logFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
        }
        qInstallMessageHandler(logMsgOutput);
#endif
    }

#ifndef QT_DEBUG
    static void logMsgOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    {

        QFile logFile;
        //global function or class static function
        Q_UNUSED(context)
        //static QMutex mutex;
        //mutex.lock();

        QString text;
        switch((int)type)
        {
        case QtDebugMsg:
            text = QString("Debug:");
            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);
        QString message = QString("%1 %2 %3").arg(text).arg(msg).arg(current_date);

        //QFile file("log.txt");
        //file.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream text_stream(&logFile);
        text_stream << message << "\r\n";
        //file.flush();
        //file.close();

        //mutex.unlock();
    }
#endif
};

#endif // LOGHELPER_H

程序初始化时调用init函数。

相关标签: qdebug file