【qt】保存debug到log里

新建一个log.h

#ifndef LOG_H
#define LOG_H

#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QMutex>
#include <QDir>

//选择屏幕打印还是输出到文件可以根据这个宏控制或者控制函数调用位置都可以
//#define _DEBUG
//默认调试级别为warning,即小于warning级别的都不会写入日志文件
//只有release版本的时候,才会输出到日志,debug版本正常输出到终端。
namespace QT_LOG
{
   
//默认文件名为当前时间命名的log文件
static int m_logLevel = 1;
static QString m_logFile = QString("./log/%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmss"));
QMutex m_logMutex;

void createLOG(){
   
    QString logFolderPath = QDir::currentPath() + "/log";
    QDir logFolder(logFolderPath);
    if(!logFolder.exists()){
   
        logFolder.mkpath(logFolderPath);
    }
}

void debugMsgHandler(QtMsgType type , const QMessageLogContext &context , const QString &msg)
{
   
    //设置输出日志级别,小于该级别,将不会写入日志文件,默认是warning级别,即debug信息不会写入日志文件
    if (type < m_logLevel) {
   
        return;
    }

    QString log_info;
    switch (type)
    {
   
    case QtDebugMsg:
        log_info = QString("%1[Debug]:").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        break;

    case QtWarningMsg:
        return;
        log_info = QString("%1[Warning]:").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        break;

    case QtCriticalMsg:
        log_info = QString("%1[Critical]:").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        break;

    case QtFatalMsg:
        log_info = QString("%1[Fatal]:").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        abort();

    case QtInfoMsg:
        log_info = QString("%1[Info]:").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        break;
    }
    log_info += QString(context.file) + QString(context.line) + QString("%1").arg(msg);

    //为了线程安全
    m_logMutex.lock();

    createLOG();
    QFile outFile(m_logFile);
    if (outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
   
        QTextStream ts(&outFile);
        ts << log_info << Qt::endl;
    }

    outFile.close();
    m_logMutex.unlock();
}

//默认调试级别为warning及以上才会写入日志文件,默认log文件名为程序启动时间命名的log文件
void logInit(QString logFile = "", int logLevel = 0)
{
   
#ifndef _DEBUG  //实现debug版本的时候,输出到终端;release版本的时候输出到日志文件
    if ((logLevel < 0) || (logLevel > 3)) {
   
        m_logLevel = 1;
    }
    else {
   
        m_logLevel = logLevel;
    }

    if (!logFile.isEmpty()) {
   
        m_logFile = logFile;
    }

    qInstallMessageHandler(debugMsgHandler);
#endif
}
}

#endif // LOG_H

在main.cpp使用

#include "widget.h"
#include <QApplication>
#include "log.h" //导入接口

int main(int argc, char *argv[])
{
   
    QApplication a(argc, argv);

    QT_LOG::logInit(); //使用日志保存功能

    Widget w;
    w.show();
    return a.exec();
}

相关推荐

  1. qt保存debuglog

    2023-12-29 07:44:03       38 阅读
  2. qt ios 将图片和视频保存手机相册

    2023-12-29 07:44:03       37 阅读
  3. 【已解决】MFC打开目录并保存编辑框

    2023-12-29 07:44:03       83 阅读
  4. export QT_DEBUG_PLUGINS=1

    2023-12-29 07:44:03       7 阅读
  5. QT教程】QT6 Debug技巧

    2023-12-29 07:44:03       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 07:44:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 07:44:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 07:44:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 07:44:03       20 阅读

热门阅读

  1. docker 部署 个人网页版 wps office

    2023-12-29 07:44:03       39 阅读
  2. 【Delphi 基础知识 3】每个单元的功能

    2023-12-29 07:44:03       38 阅读
  3. 【芯片DFX】Arm调试架构篇

    2023-12-29 07:44:03       33 阅读
  4. 微信小程序控制元素显示隐藏

    2023-12-29 07:44:03       35 阅读
  5. Mac电脑CMake安装和配置

    2023-12-29 07:44:03       40 阅读
  6. MySQL实战

    2023-12-29 07:44:03       34 阅读
  7. 为什么Python很糟糕

    2023-12-29 07:44:03       37 阅读
  8. .NET Core HttpClient请求异常分析

    2023-12-29 07:44:03       34 阅读