1.头文件
#ifndef QLOGGER_H
#define QLOGGER_H
#include <QObject>
#include <QMutex>
#include <QTimer>
class QLogger : public QObject
{
Q_OBJECT
public:
static void log(QString text); // 写日志
signals:
void sigWork(QString text);
private:
QLogger(QObject* parent = nullptr);
void init(); // 创建日志目录
bool deleteLog(QString path); // 删除日志
bool deleteDir(QString path); // 删除文件夹
private slots:
void workSlot(QString text);
void clearLogSlot(); // 定时清除日志
private:
static QLogger* m_pLogger;
static QMutex m_mutex;
QTimer* m_pTimer; // 清除日志
};
#endif // QLOGGER_H
2.源文件
#include "qlogger.h"
#include <QCoreApplication>
#include <QDate>
#include <QDateTime>
#include <QDir>
#include <QTextStream>
#include <QThread>
#include <QSettings>
#include <QDebug>
QLogger* QLogger::m_pLogger = nullptr;
QMutex QLogger::m_mutex;
const QString LOG_PATH = "/log/"; // 日志路径
const QString LOG_INI = "log.ini"; // 日志文件
const int LOG_DAY = 15;
const int TIMEOUT = 3600 * 24;
QLogger::QLogger(QObject* parent) :
QObject(parent)
{
QThread* thread = new QThread;
moveToThread(thread);
thread->start();
// 定时查询清理日志
m_pTimer = new QTimer();
connect(m_pTimer, &QTimer::timeout, this, &QLogger::clearLogSlot);
m_pTimer->start(TIMEOUT);
// 初始化
init();
// 写日志
connect(this, &QLogger::sigWork, this, &QLogger::workSlot);
}
void QLogger::init()
{
// 创建日志文件夹
QString path = QCoreApplication::applicationDirPath() + LOG_PATH;
QDir dir(path);
if(!dir.exists()) {
dir.mkpath(path);
}
QSettings* settings = new QSettings(path + LOG_INI, QSettings::IniFormat);
auto startTime = settings->value("start").toString();
auto currentTime = QDateTime::currentDateTime();
if(startTime.isEmpty()) {
settings->setValue("start", currentTime.toString("yyyy-MM-dd hh:mm:ss"));
} else {
auto start = QDateTime::fromString(startTime, "yyyy-MM-dd hh:mm:ss");
auto days = start.daysTo(currentTime);
if(days > LOG_DAY) {
deleteLog(path);
settings->setValue("start", currentTime.toString("yyyy-MM-dd hh:mm:ss"));
}
}
settings->deleteLater();
#ifdef _DEBUG
qDebug() << __func__ << QThread::currentThread();
#endif
}
bool QLogger::deleteLog(QString path)
{
if (path.isEmpty()) {
return false;
}
QDir dir(path);
if(!dir.exists()) {
return false;
}
auto dirs = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
for(auto& it : dirs) {
deleteDir(it.absoluteFilePath());
}
return true;
}
bool QLogger::deleteDir(QString path)
{
if (path.isEmpty()) {
return false;
}
QDir dir(path);
if(!dir.exists()) {
return true;
}
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
QFileInfoList fileList = dir.entryInfoList();
foreach (QFileInfo file, fileList) {
if (file.isFile()){
file.dir().remove(file.fileName());
} else{
deleteDir(file.absoluteFilePath());
}
}
return dir.rmdir(dir.absolutePath());
}
void QLogger::workSlot(QString text)
{
QMutexLocker lock(&m_mutex);
// 日志文件夹
QString path = QCoreApplication::applicationDirPath() + LOG_PATH;
auto date = QDate::currentDate();
path += date.toString("yyyy-MM");
QDir dir(path);
if(!dir.exists()) {
dir.mkpath(path);
}
QString fileName = path + "/" + date.toString("dd") + ".log";
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
return;
}
QTextStream stream(&file);
QString currentTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
stream << "[" + currentTime + "] " << text << endl;
file.close();
#ifdef _DEBUG
qDebug() << __func__ << QThread::currentThread();
#endif
}
void QLogger::clearLogSlot()
{
init();
#ifdef _DEBUG
qDebug() << __func__ << QThread::currentThread();
#endif
}
void QLogger::log(QString text)
{
QMutexLocker lock(&m_mutex);
if(m_pLogger == nullptr) {
m_pLogger = new QLogger();
}
emit m_pLogger->sigWork(text);
#ifdef _DEBUG
qDebug() << __func__ << QThread::currentThread();
#endif
}