QT实现一个简单的日志打印系统

效果演示

在这里插入图片描述使用方法:直接将logsystem.cpp文件和logsystem.h文件添加到工程目录中,然后包含即可使用。
在这里插入图片描述直接调用打印接口
在这里插入图片描述在这里插入图片描述然后在程序exe文件夹下会生成一个文件名叫log_debug.txt文件。

源文件.C

#include "logsystem.h"

LogSystem::LogSystem()
{
}

void LogSystem::error(QString msg)
{
    if(!Write_debug_information("error",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

void LogSystem::debug(QString msg)
{
    if(!Write_debug_information("debug",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

void LogSystem::warning(QString msg)
{
    if(!Write_debug_information("warning",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

void LogSystem::info(QString msg)
{
    if(!Write_debug_information("info",msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

bool LogSystem::Write_debug_information(QString level, QString message)
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm");
    QString fileName = QCoreApplication::applicationDirPath() + "/debug_log.txt";// 获取程序当前运行目录
    QTextStream txtOutput(&file);
    QString write_str;

    file.setFileName(fileName);
    if (!file.open(QIODevice::ReadWrite | QIODevice::Append))   /* 以append的方式打开 */
    {
        return false;
    }
    if (level == "DEBUG" || level == "debug")
    {
        write_str = QString("[%1][%2]:%3").arg("DEBUG").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }
    else if(level == "WARNING" || level == "warning")
    {
        write_str = QString("[%1][%2]:%3").arg("WARNING").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }
    else if(level == "ERROR" || level == "error")
    {
        write_str = QString("[%1][%2]:%3").arg("ERROR").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }
    else if(level == "INFO" || level == "info")
    {
        write_str = QString("[%1][%2]:%3").arg("INFO").arg(current_date).arg(message);
        txtOutput << write_str <<endl;
//        file.write(write_str.toLocal8Bit());
    }else
    {
        file.close();/* 关闭文件 */
        return false;
    }
    file.close();/* 关闭文件 */
    return true;
}

源文件.h文件

#ifndef LOGSYSTEM_H
#define LOGSYSTEM_H

#include <QFile>
#include <QString>
#include <QTime>
#include <QDebug>
#include <QTextStream>
#include <QCoreApplication>
#include <QMessageBox>

class LogSystem
{
public:
    LogSystem();
    QFile file;
    QString fileName;
    
private:
    bool Write_debug_information(QString level, QString info);
    
public slots:   
    void info(QString msg);
    void error(QString msg);
    void warning(QString msg);
    void debug(QString msg);
};

#endif // LOGSYSTEM_H

2021-11-21更新

.cpp文件

#include "logsystem.h"

/***
 * 构造函数:
 * 初始化日志系统目录
 ***/
LogSystem::LogSystem()
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyyMMdd");
    QDir dir;
    if (!dir.exists(fileAddress + current_date))
    {
        dir.mkdir(fileAddress + current_date);
        qDebug() << "[INFO][:" +current_date_time.toString("yyyy.MM.dd hh:ss") +  "]:新建LOG文件夹" << endl;
    }
    fileName = fileAddress + current_date + fileName;
}

/***
 * 打印错误信息并附带系统时间
 ***/
void LogSystem::error(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("error",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印调试信息并附带系统时间
 ***/
void LogSystem::debug(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("debug",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印警告信息并附带系统时间
 ***/
void LogSystem::warning(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("warning",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印提示信息并附带系统时间
 ***/
void LogSystem::info(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("info",true,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印错误信息
 ***/
void LogSystem::error_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("error",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印调试信息
 ***/
void LogSystem::debug_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("debug",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印警告信息
 ***/
void LogSystem::warning_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("warning",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 打印提示信息
 ***/
void LogSystem::info_notime(QString msg)
{
    if(openFlag == false) return;
    if(!Write_debug_information("info",false,msg))
    {
        QMessageBox::information(NULL, "information", "log system error!");
    }
}

/***
 * 设置日志信息文件名字
 ***/
void LogSystem::setFileName(QString name)
{
    this->fileName = name;
}

/***
 * 设置是否开启日子系统的qdebug信息输出
 ***/
void LogSystem::setQdebugFlag(bool flag)
{
    this->qDebugFlag = flag;
}
/***
 * 打开日志系统
 ***/
void LogSystem::start()
{
    openFlag = true;
}
/***
 * 关闭日志系统
 ***/
void LogSystem::stop()
{
    openFlag = false;
}

/***
 * 写入信息到日志信息文本文件中
 ***/
bool LogSystem::Write_debug_information(QString level, bool time, QString message)
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy-MM-dd hh:mm");

    QTextStream txtOutput(&file);
    QString write_str;

    file.setFileName(fileName);
    if (!file.open(QIODevice::ReadWrite | QIODevice::Append))   /* 以append的方式打开 */
    {
        return false;
    }
    if (time == true){
        if (level == "DEBUG" || level == "debug")
        {
            write_str = QString("[%1][%2]:%3").arg("DEBUG").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "WARNING" || level == "warning")
        {
            write_str = QString("[%1][%2]:%3").arg("WARNING").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "ERROR" || level == "error")
        {
            write_str = QString("[%1][%2]:%3").arg("ERROR").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "INFO" || level == "info")
        {
            write_str = QString("[%1][%2]:%3").arg("INFO").arg(current_date).arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }else
        {
            file.close();/* 关闭文件 */
            return false;
        }
    }else{
        if (level == "DEBUG" || level == "debug")
        {
            write_str = QString("[%1]:%2").arg("DEBUG_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "WARNING" || level == "warning")
        {
            write_str = QString("[%1]:%2").arg("WARNING_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "ERROR" || level == "error")
        {
            write_str = QString("[%1]:%2").arg("ERROR_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }
        else if(level == "INFO" || level == "info")
        {
            write_str = QString("[%1]:%2").arg("INFO_NOTIME").arg(message);
            txtOutput << write_str <<endl;
            if(qDebugFlag) qDebug() << write_str <<endl;
        }else
        {
            file.close();/* 关闭文件 */
            return false;
        }
    }
    file.close();/* 关闭文件 */
    return true;
}

.h文件

#ifndef LOGSYSTEM_H
#define LOGSYSTEM_H

#include <QFile>
#include <QString>
#include <QTime>
#include <QDebug>
#include <QTextStream>
#include <QCoreApplication>
#include <QMessageBox>
#include <QDir>

class LogSystem
{
public:
    LogSystem();
    
public slots:   
    void info(QString msg);
    void error(QString msg);
    void warning(QString msg);
    void debug(QString msg);
    void info_notime(QString msg);
    void error_notime(QString msg);
    void warning_notime(QString msg);
    void debug_notime(QString msg);
    void setFileName(QString name);
    void setQdebugFlag(bool flag);
    void start();
    void stop();

private:
    bool Write_debug_information(QString level, bool time, QString info);
    QFile file;
    QString fileName = "/debug_log.txt";        // 文件名
    QString fileAddress = "./LOG-";             // 文件存储位置
    bool qDebugFlag = true;                     // 是否在写入日志的通知打印信息到程序输出
    bool openFlag = false;                      // 启动标志位
};

#endif // LOGSYSTEM_H

调用

LogSystem *log = new LogSystem();
log.setQdebugFlag(true);
log.start();
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

凉开水白菜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值