QT 日志记录

在软件运行过程中,要记录下运行日志,方便后期开发人员追踪问题,qt可以通过下方函数实现,程序启动时在main函数中调用设置下即可

#include <QDateTime>
#include <QFile>
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>
void msgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{    
    static QMutex msgHandler_mutex;
    static QFile msgFile;
    QMutexLocker locker(&msgHandler_mutex);
    QByteArray localMsg = msg.toUtf8();
    if (localMsg.length() > 20000) { 
       localMsg = localMsg.mid(0, 20000) + "......"; 
   }
    
   QString ds = QDateTime::currentDateTime().toString("yyyyMMdd");
    if (!msgFile.isOpen()) {
        msgFile.setFileName(QString("%1/log-%2.txt").arg("E:/").arg(ds)); 
       msgFile.open(QIODevice::WriteOnly | QIODevice::Append); 
   } else { 
       QString fn = msgFile.fileName(); 
       if (!fn.contains(ds)) { 
            msgFile.close();  
            msgFile.setFileName(QString("%1/log-%2.txt").arg("E:/").arg(ds));
            msgFile.open(QIODevice::WriteOnly | QIODevice::Append);
        }
    }

QString strMsg; 
switch(type)
{
    case QtDebugMsg:
        //        localMsg.startsWith("qD") ? localMsg.remove(0, 3) : strMsg 
        QString("Debug:");
        break;
    case QtWarningMsg:
        strMsg = QString("Warning:");
        break;
    case QtCriticalMsg:
        strMsg = QString("Critical:");
        break;
    case QtFatalMsg:
        strMsg = QString("Fatal:");
        break;
    }
 
    QString strDateTime = QDateTime::currentDateTime().toString("HH:mm:ss.zzz");
    QString strMessage = strMsg + localMsg + QString("[%1]").arg(strDateTime);
    msgFile.write(strMessage.toUtf8() + "\r\n");
    msgFile.flush();
    fprintf(stderr, "%s\n", strMessage.toLocal8Bit().constData());
    fflush(stderr);
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt 中,您可以通过以下方法设置创建调试日志的路径: 1. 在 main 函数中设置日志路径: ``` #include <QCoreApplication> #include <QLoggingCategory> #include <QDebug> #include <QFile> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 设置日志路径 QString logPath = "debug.log"; QFile file(logPath); file.open(QIODevice::WriteOnly | QIODevice::Append); qInstallMessageHandler([](QtMsgType type, const QMessageLogContext &context, const QString &msg) { QTextStream out(&file); switch (type) { case QtDebugMsg: out << "Debug: " << msg << endl; break; case QtWarningMsg: out << "Warning: " << msg << endl; break; case QtCriticalMsg: out << "Critical: " << msg << endl; break; case QtFatalMsg: out << "Fatal: " << msg << endl; abort(); } }); qInfo() << "Hello, world!"; return a.exec(); } ``` 2. 使用 qInstallMessageHandler 函数设置日志路径: ``` #include <QCoreApplication> #include <QLoggingCategory> #include <QDebug> #include <QFile> // 日志处理函数 void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { static QFile file("debug.log"); if (!file.isOpen()) { file.open(QIODevice::WriteOnly | QIODevice::Append); } QTextStream out(&file); switch (type) { case QtDebugMsg: out << "Debug: " << msg << endl; break; case QtWarningMsg: out << "Warning: " << msg << endl; break; case QtCriticalMsg: out << "Critical: " << msg << endl; break; case QtFatalMsg: out << "Fatal: " << msg << endl; abort(); } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 设置日志处理函数 qInstallMessageHandler(messageHandler); qInfo() << "Hello, world!"; return a.exec(); } ``` 以上两种方法都会在程序运行时创建一个名为 debug.log 的文件,并将日志记录到该文件中。您可以将文件名更改为您想要的任何名称和路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北海以北@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值