QT实现日志系统

功能:

可以把简单输出信息显示到QTextEdit上面。

可以把详细输出信息保存到文件中(为了避免多次打开文件进行读写,我用了双缓冲和线程)。

 

主要文件1:SysMessage.h

#ifndef SYSMESSAGE_H
#define SYSMESSAGE_H

#include 
#include 
class SysMessage : QThread
{
public:
    void run();
    void write_message();
    /*  Out message to file and UI.
     *
     */
    static void        open_sys_message(QTextEdit *e);
    static void        close_sys_message();
    static void        SysMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);

private:
    SysMessage();
    ~SysMessage();
    unsigned int message_buf_size;
    char        *message_buf_A;//take turns using buf
    char        *message_buf_B;
    char        *new_buf;
    char        *old_buf;
    bool         buf_A_B;//false: A   true:B

    unsigned int buf_offset;

    QString date;
};

#endif // SYSMESSAGE_H

主要文件2:SysMessage.cpp

#include "SysMessage.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
/* Global variable
 *
 */
static char *buf_tmp;
static SysMessage *message;
QTextEdit *edit_message;
/*****************************************************************************************
 *
 */
SysMessage::SysMessage()
{
    message_buf_size = 10240;
    message_buf_A = new char[10240];
    message_buf_B = new char[10240];
    buf_tmp       = new char[256];
    buf_offset = 0;
    new_buf = old_buf = message_buf_A;
    buf_A_B = false;
}

SysMessage::~SysMessage()
{
    delete message_buf_A;
    delete message_buf_B;
    delete buf_tmp;
}

void SysMessage::open_sys_message(QTextEdit *e)
{
    edit_message = e;
    if (message == nullptr) message = new SysMessage;
}

void SysMessage::close_sys_message()
{
    if (message != nullptr) {
        message->write_message();
        delete message;
    }
}
/*****************************************************************************************
 *
 */
void SysMessage::SysMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QDate   date = QDate::currentDate();
    QString str_date = QString("%1-%2-%3-%4").arg(date.year()).arg(date.month()).arg(date.day()).arg(QTime::currentTime().toString());
    edit_message->append(str_date+QString(": ")+msg);

    switch (type) {
    case QtDebugMsg:
        sprintf(buf_tmp, "[File:(%s:%d) Fun:(%s) Date:(%s)] Debug: %s\n",
            context.file, context.line, context.function, str_date.toLocal8Bit().constData(), msg.toLocal8Bit().constData());
        break;
    case QtInfoMsg:
        sprintf(buf_tmp, "[File:(%s:%d) Fun:(%s) Date:(%s)] Info: %s\n",
            context.file, context.line, context.function, str_date.toLocal8Bit().constData(), msg.toLocal8Bit().constData());
        break;
    case QtWarningMsg:
        sprintf(buf_tmp, "[File:(%s:%d) Fun:(%s) Date:(%s)] Warning: %s\n",
            context.file, context.line, context.function, str_date.toLocal8Bit().constData(), msg.toLocal8Bit().constData());
        break;
    case QtCriticalMsg:
        sprintf(buf_tmp, "[File:(%s:%d) Fun:(%s) Date:(%s)] Critical: %s\n",
            context.file, context.line, context.function, str_date.toLocal8Bit().constData(), msg.toLocal8Bit().constData());
        break;
    case QtFatalMsg:
        sprintf(buf_tmp, "[File:(%s:%d) Fun:(%s) Date:(%s)] Fatal: %s\n",
            context.file, context.line, context.function, str_date.toLocal8Bit().constData(), msg.toLocal8Bit().constData());
        break;//abort();
    }
    message->write_message();
}



/*****************************************************************************************
 *
 */
void SysMessage::run()
{
    QDate   date = QDate::currentDate();
    QString file_name = QString("C:\\Desktop\\%1-%2-%3%4").arg(date.year()).arg(date.month()).arg(date.day()).arg(QString(".log"));
    QFile message_file(file_name);
    if(!message_file.open(QIODevice::WriteOnly | QIODevice::Append)) {
        qDebug()<<"Open failed.";
        return;
    }
    message_file.write(old_buf);
    message_file.close();
}

void SysMessage::write_message()
{
    int len = strlen(buf_tmp);
    strcpy(new_buf + buf_offset, buf_tmp);
    buf_offset += len;
    if ((buf_offset + 256) > message_buf_size) {
        this->start(QThread::LowestPriority);
        buf_offset = 0;
        old_buf = new_buf;
        new_buf = (buf_A_B ? message_buf_B : message_buf_A);
        buf_A_B = !buf_A_B;
    }
}

主要文件3:mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include "SysMessage.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    SysMessage::open_sys_message(ui->textEdit);
    qInstallMessageHandler(SysMessage::SysMessageOutput);
}

MainWindow::~MainWindow()
{
    SysMessage::close_sys_message();
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    qDebug() << "消息";
}

界面输出结果:

QT实现日志系统_第1张图片

文件输出结果:

[File:(..\untitled\mainwindow.cpp:35) Fun:(void MainWindow::on_pushButton_clicked()) Date:(2019-12-11-17:23:34)] Debug: 消息
[File:(..\untitled\mainwindow.cpp:35) Fun:(void MainWindow::on_pushButton_clicked()) Date:(2019-12-11-17:23:35)] Debug: 消息
[File:(..\untitled\mainwindow.cpp:35) Fun:(void MainWindow::on_pushButton_clicked()) Date:(2019-12-11-17:23:35)] Debug: 消息
[File:(..\untitled\mainwindow.cpp:35) Fun:(void MainWindow::on_pushButton_clicked()) Date:(2019-12-11-17:23:35)] Debug: 消息
[File:(..\untitled\mainwindow.cpp:35) Fun:(void MainWindow::on_pushButton_clicked()) Date:(2019-12-11-17:23:35)] Debug: 消息
[File:(..\untitled\mainwindow.cpp:35) Fun:(void MainWindow::on_pushButton_clicked()) Date:(2019-12-11-17:23:35)] Debug: 消息

 

 

 

 

 

 

你可能感兴趣的:(QT学习)