[Qt]----打印阻截日志

本文介绍了一个基于Qt的应用日志系统实现,包括自定义消息处理函数、日志级别控制、日志文件管理和文件操作类。系统能够根据不同类型的消息生成特定的日志文件,并记录详细的信息,如时间戳、文件名、行号和消息内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.AppLog.cpp

#include "AppLog.h"
#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QTextStream>
#include "../FileAndDir/FileOperator.h"


void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    mutex.lock();

    QString text;
    bool isLog = true;
    switch(type)
    {
    case QtDebugMsg:
        text = QString("Debug:");
        isLog = true;
        break;

    case QtWarningMsg:
        text = QString("Warning:");
        isLog = false;
        break;

    case QtCriticalMsg:
        text = QString("Critical:");
        break;

    case QtFatalMsg:
        text = QString("Fatal:");
    }

    QString message = "";
    QString _logPath = AppLog::GetInstance()->getPath();
    QString _logName = AppLog::GetInstance()->getLogName(type);

    if (context.file != nullptr)
    {
        QString context_info;
        if(isLog){
            context_info = QString("File:(%1) - Line:(%2)").arg(context.file).arg(context.line);
        }else{
            context_info = QString("Line:(%1)").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);
        message = QString("%1 %2 %3 %4").arg(current_date).arg(text).arg(context_info).arg(msg);
    }
    else
    {
        message = msg;
    }

    QFile file(_logPath + "/" + _logName +".txt");
    if(file.exists())
    {
        file.open(QIODevice::WriteOnly | QIODevice::Append);
    }else
    {
        FileOperator::GetInstance()->creatFile(_logPath,_logName,".txt");
        file.open(QIODevice::WriteOnly | QIODevice::Append);
    }

    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();

    mutex.unlock();
}

AppLog::AppLog()
{
    logPath = "C:/QtLogData";
    bool ret = FileOperator::GetInstance()->creatDir("C:/","QtLogData");
    Q_UNUSED(ret);

    qInstallMessageHandler(outputMessage);
}

void AppLog::qDebug_Info(int type, QString strInfo)
{
    QMessageLogContext context;
    context.file = nullptr;

    outputMessage((QtMsgType)type, context, strInfo);
}

void AppLog::setLogPath(QString path)
{
    logPath = path;
    bool ret = FileOperator::GetInstance()->creatDir(logPath.left(logPath.lastIndexOf("/")),logPath.section('/', -1));
    Q_UNUSED(ret);
}

void AppLog::setLogName(QString name)
{
    logFileName = name;
}

QString AppLog::getLogName(QtMsgType type)
{
    switch (type) {
    case QtDebugMsg:
        return  "Debug";

    case QtWarningMsg:
        return "Warning";

    case QtCriticalMsg:
        return QString("Critical");

    case QtFatalMsg:
        return QString("Fatal");

    case QtInfoMsg:
        return QString("Log");
    }
    return "Log";
}

QString AppLog::getPath()
{
    return  logPath;
}


AppLog * AppLog::instance = nullptr;

AppLog *AppLog::GetInstance()
{
    if(nullptr == instance)
    {
        instance = new AppLog();
    }
    return instance;
}

2.AppLog.h

/*
** 事件规划:    qDebug_Info参数1
** 0    :   调试事件
** 1    :   警告事件
** 2    :   紧急(故障)事件
** 3    :   致命(严重)事件
** 4    :   系统(System)事件

*/
#include <QObject>


class AppLog : public QObject
{
    Q_OBJECT

public:
    AppLog();
    Q_INVOKABLE void qDebug_Info(int type, QString strInfo);


    Q_INVOKABLE void setLogPath(QString path);
    Q_INVOKABLE void setLogName(QString name);

    Q_INVOKABLE QString getLogName(QtMsgType type);
    Q_INVOKABLE QString getPath();

    static AppLog* GetInstance();
private:
    QString logPath;
    QString logFileName;
    static AppLog * instance;

};

其中FileOperator类是文件和文件夹操作的一些方法.用于检查文件夹或者文件是否为空.

用于Qt和QML打印混合阻截,同时在日志记录本地时间.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值