QT 日志

一、方法一

#include <QCoreApplication>
#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QTextStream>
#include <loghandle.h>
#include <QDebug>

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg){
    static QMutex mutex;
    mutex.lock();
    QString text = "";
    switch (type) {
    case QtDebugMsg:
        text = QString("Debug:");
        break;
    case QtWarningMsg:
        text = QString("Warning:");
        break;
    case QtCriticalMsg:
        text = QString("Critical:");
        break;
    case QtFatalMsg:
        text = QString("Fatal:");
        break;
    default:
        break;
    }
    QString context_info = QString("QFile:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString current_data = QString("(%1)").arg(current_dateTime);
    QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_data);
    QFile file("log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();
    mutex.unlock();
}

void space_line(){
//    qDebug() << "===========================";
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qInstallMessageHandler(outputMessage);
    atexit(space_line);
    qDebug() << "hello qt!";

    return a.exec();
}

方法二、

以类的形式。

.h文件

#ifndef LOGHANDLE_H
#define LOGHANDLE_H

#include <QObject>
#include <QMutex>
#include <QTextStream>
#include <QFile>
#include <QDateTime>
#include <QTimer>
#include <QDir>
static QMutex mutex;
class LogHandle :public QObject
{
    Q_OBJECT
public:
    LogHandle();
    ~LogHandle();
    void checkLogFilesSize();   // 检查日志文件的大小

private slots:
    void autoDeleteLog();   // 自动删除日志

private:
    QTimer *timer = nullptr;
};

#endif // LOGHANDLE_H

 

.cpp文件

 #include "loghandle.h"
#include <QDebug>


void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    mutex.lock();
    QString text = "";
    switch (type) {
    case QtDebugMsg:
        text = QString("Debug:");
        break;
    case QtWarningMsg:
        text = QString("Warning:");
        break;
    case QtCriticalMsg:
        text = QString("Critical:");
        break;
    case QtFatalMsg:
        text = QString("Fatal:");
        break;
    default:
        break;
    }
    QString context_info = QString("QFile:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_dateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString current_data = QString("(%1)").arg(current_dateTime);
    QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_data);
    QFile file("today.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();
    mutex.unlock();
}

LogHandle::LogHandle()
{
    qInstallMessageHandler(myMessageOutput);
    timer = new QTimer;
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(autoDeleteLog()));
    timer->start(5000);
}

LogHandle::~LogHandle()
{

}
void LogHandle::autoDeleteLog()
{
//    QDateTime now = QDateTime::currentDateTime();
//    QDateTime time1 = now.addDays(-30);
//    QDateTime time2;
//    QDir dir("taday.txt");
//    QFileInfoList fileList = dir.entryInfoList();
//    foreach (QFileInfo f, fileList ) {
//        // "."和".."跳过
//        if (f.baseName() == "")
//            continue;

//        time2 = QDateTime::fromString(f.baseName(), "yyyy-MM-dd");
//        if (time2 < time1) { // 只要日志时间小于前30天的时间就删除
//            dir.remove(f.absoluteFilePath());
//        }
//    }
}

void LogHandle::checkLogFilesSize()
{
    qDebug() << "wadakjfaskd";
}

.main文件

#include <QCoreApplication>
#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QTextStream>
#include <loghandle.h>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    LogHandle log;
    qDebug() << "hello qt!";
    log.checkLogFilesSize();

    return a.exec();
}

 

posted on 2021-09-14 16:57  缘随风烬  阅读(330)  评论(0编辑  收藏  举报