Skip to content

Commit

Permalink
[日志异步处理模块重构与功能增强]: 对LogAsynchronous项目进行了一系列重构和功能增强,提升了日志处理的性能和灵活性。
Browse files Browse the repository at this point in the history
- 引入了`ROLLSIZE`宏定义,明确了日志文件滚动的尺寸为1GB。
- 更新了`kRollPerSeconds_`常量为`g_kRollPerSeconds`,以更清晰地表示每天滚动一次的常量。
- 增加了日志文件名生成的静态函数`getFileName`,支持根据当前时间生成文件名。
- 移除了`FileUtilPrivate`结构体,替换为`FileUtil::FileUtilPrivate`类,增强了封装性。
- 优化了`FileUtil`构造函数,简化了目录生成的逻辑,移除了`autoDelFileDays`参数,改为通过`LogAsync`单例管理。
- 重构了`rollFile`函数,使其更加简洁,并整合了自动删除旧文件的逻辑。
- 更新了`FileUtil`的头文件,移除了一些私有成员函数的声明,因为它们现在是静态函数。
- 对`LogAsync`类进行了扩展,增加了日志路径、自动删除文件和保留天数的设置功能。
- 重构了`LogAsync`的单例实现,使用局部静态变量代替了静态成员变量。
- 增加了对日志输出方向的控制,支持标准输出、文件输出或两者同时。
- 优化了`messageHandler`函数,改进了日志消息的格式化和输出。
- 更新了`main.cpp`和`mainwindow.cpp`,以使用改进后的`LogAsync`功能。
  • Loading branch information
RealChuan committed May 27, 2024
1 parent e3b029d commit 259b83a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 103 deletions.
101 changes: 43 additions & 58 deletions LogAsynchronous/fileutil.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "fileutil.h"
#include "logasync.h"

#include <QCoreApplication>
#include <QDateTime>
Expand All @@ -8,49 +9,63 @@
#include <QTextStream>
#include <QTimer>

#define ROLLSIZE 1000 * 1000 * 1000
#define ROLLSIZE (1000 * 1000 * 1000)

const static int kRollPerSeconds_ = 60 * 60 * 24;
const static int g_kRollPerSeconds = 60 * 60 * 24;

auto generateDirectorys(const QString &directory) -> bool
static auto getFileName(qint64 seconds) -> QString
{
QDir sourceDir(directory);
if (sourceDir.exists()) {
return true;
auto data = QDateTime::fromSecsSinceEpoch(seconds).toString("yyyy-MM-dd-hh-mm-ss");
auto filename = QString("%1/%2.%3.%4.%5.log")
.arg(LogAsync::instance()->logPath(),
qAppName(),
data,
QSysInfo::machineHostName(),
QString::number(qApp->applicationPid()));
return filename;
}

static void autoDelFile()
{
auto *instance = LogAsync::instance();
const QString path(instance->logPath());
QDir dir(path);
if (!dir.exists()) {
return;
}

QString tempDir;
QStringList directorys = directory.split("/");
for (int i = 0; i < directorys.count(); i++) {
QString path = directorys[i];
tempDir += path + "/";
const QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Time);
const QDateTime cur = QDateTime::currentDateTime();
const QDateTime pre = cur.addDays(-instance->autoDelFileDays());

QDir dir(tempDir);
if (!dir.exists() && !dir.mkdir(tempDir)) {
return false;
for (const QFileInfo &info : std::as_const(list)) {
if (info.lastModified() <= pre) {
dir.remove(info.fileName());
}
}

return true;
}

struct FileUtilPrivate
class FileUtil::FileUtilPrivate
{
public:
explicit FileUtilPrivate(FileUtil *q)
: q_ptr(q)
{}

FileUtil *q_ptr;

QFile file;
//QTextStream 读写分离的,内部有缓冲区static const int QTEXTSTREAM_BUFFERSIZE = 16384;
QTextStream stream;
qint64 startTime = 0;
qint64 lastRoll = 0;
int count = 0;
qint64 autoDelFileDays = 30;
};

FileUtil::FileUtil(qint64 days, QObject *parent)
FileUtil::FileUtil(QObject *parent)
: QObject(parent)
, d_ptr(new FileUtilPrivate)
, d_ptr(new FileUtilPrivate(this))
{
d_ptr->autoDelFileDays = days;
generateDirectorys(qApp->applicationDirPath() + "/log");
rollFile(0);
setTimer();
}
Expand All @@ -66,7 +81,7 @@ void FileUtil::onWrite(const QString &msg)
rollFile(++d_ptr->count);
} else {
qint64 now = QDateTime::currentSecsSinceEpoch();
qint64 thisPeriod = now / kRollPerSeconds_ * kRollPerSeconds_;
qint64 thisPeriod = now / g_kRollPerSeconds * g_kRollPerSeconds;
if (thisPeriod != d_ptr->startTime) {
d_ptr->count = 0;
rollFile(0);
Expand All @@ -81,29 +96,16 @@ void FileUtil::onFlush()
d_ptr->stream.flush();
}

auto FileUtil::getFileName(qint64 *now) const -> QString
{
*now = QDateTime::currentSecsSinceEpoch();
QString data = QDateTime::fromSecsSinceEpoch(*now).toString("yyyy-MM-dd-hh-mm-ss");
QString filename = QString("%1/log/%2.%3.%4.%5.log")
.arg(qApp->applicationDirPath(),
qAppName(),
data,
QSysInfo::machineHostName(),
QString::number(qApp->applicationPid()));
return filename;
}

auto FileUtil::rollFile(int count) -> bool
{
qint64 now = 0;
QString filename = getFileName(&now);
qint64 now = QDateTime::currentSecsSinceEpoch();
QString filename = getFileName(now);
if (count != 0) {
filename += QString(".%1").arg(count);
} else {
} else if (LogAsync::instance()->autoDelFile()) {
autoDelFile();
}
qint64 start = now / kRollPerSeconds_ * kRollPerSeconds_;
qint64 start = now / g_kRollPerSeconds * g_kRollPerSeconds;
if (now > d_ptr->lastRoll) {
d_ptr->startTime = start;
d_ptr->lastRoll = now;
Expand All @@ -120,26 +122,9 @@ auto FileUtil::rollFile(int count) -> bool
return false;
}

void FileUtil::autoDelFile()
{
const QString path(qApp->applicationDirPath() + "/log");
generateDirectorys(path);
QDir dir(path);

const QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Time);
const QDateTime cur = QDateTime::currentDateTime();
const QDateTime pre = cur.addDays(-d_ptr->autoDelFileDays);

for (const QFileInfo &info : qAsConst(list)) {
if (info.lastModified() <= pre) {
dir.remove(info.fileName());
}
}
}

void FileUtil::setTimer()
{
QTimer *timer = new QTimer(this);
auto *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &FileUtil::onFlush);
timer->start(5000); // 5秒刷新一次
}
10 changes: 4 additions & 6 deletions LogAsynchronous/fileutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@

#include <QObject>

struct FileUtilPrivate;
class FileUtil : public QObject
{
Q_OBJECT
public:
explicit FileUtil(qint64 days = 30, QObject *parent = nullptr);
explicit FileUtil(QObject *parent = nullptr);
~FileUtil() override;

public slots:
void onWrite(const QString &);
void onWrite(const QString & /*msg*/);

private slots:
void onFlush();

private:
auto getFileName(qint64 *now) const -> QString;
auto rollFile(int) -> bool;
void autoDelFile();
auto rollFile(int /*count*/) -> bool;
void setTimer();

class FileUtilPrivate;
QScopedPointer<FileUtilPrivate> d_ptr;
};

Expand Down
66 changes: 52 additions & 14 deletions LogAsynchronous/logasync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
// 消息处理函数
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if (type < LogAsync::instance()->logLevel()) {
auto *instance = LogAsync::instance();
if (type < instance->logLevel()) {
return;
}

Expand Down Expand Up @@ -38,7 +39,7 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt
}

const QString dataTimeString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
const QString threadId = QString("%1").arg(qint64(QThread::currentThreadId()),
const QString threadId = QString("%1").arg(reinterpret_cast<quint64>(QThread::currentThreadId()),
5,
10,
QLatin1Char('0'));
Expand All @@ -48,19 +49,19 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt
#ifndef QT_NO_DEBUG
contexInfo = QString("File:(%1) Line:(%2)").arg(context.file).arg(context.line);
#endif
const QString message = QString("%1 %2 [%3] %4 - %5\n")
.arg(dataTimeString, threadId, level, msg, contexInfo);
const auto message = QString("%1 %2 [%3] %4 - %5\n")
.arg(dataTimeString, threadId, level, msg, contexInfo);

switch (LogAsync::instance()->orientation()) {
switch (instance->orientation()) {
case LogAsync::Orientation::Std:
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
::fflush(stdPrint);
break;
case LogAsync::Orientation::File: emit LogAsync::instance()->appendBuf(message); break;
case LogAsync::Orientation::File: emit instance->appendBuf(message); break;
case LogAsync::Orientation::StdAndFile:
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
::fflush(stdPrint);
emit LogAsync::instance()->appendBuf(message);
emit instance->appendBuf(message);
break;
default:
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
Expand All @@ -69,21 +70,58 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt
}
}

struct LogAsyncPrivate
class LogAsync::LogAsyncPrivate
{
public:
explicit LogAsyncPrivate(LogAsync *q)
: q_ptr(q)
{}

LogAsync *q_ptr;

QString logPath;
bool autoDelFile = false;
qint64 autoDelFileDays = 7;
QtMsgType msgType = QtWarningMsg;
LogAsync::Orientation orientation = LogAsync::Orientation::Std;
QWaitCondition waitCondition;
QMutex mutex;
};

QMutex LogAsync::m_mutex;

auto LogAsync::instance() -> LogAsync *
{
QMutexLocker locker(&m_mutex);
static LogAsync log;
return &log;
static LogAsync instance;
return &instance;
}

void LogAsync::setLogPath(const QString &path)
{
d_ptr->logPath = path;
}

auto LogAsync::logPath() -> QString
{
return d_ptr->logPath;
}

void LogAsync::setAutoDelFile(bool on)
{
d_ptr->autoDelFile = on;
}

auto LogAsync::autoDelFile() -> bool
{
return d_ptr->autoDelFile;
}

void LogAsync::setAutoDelFileDays(qint64 days)
{
d_ptr->autoDelFileDays = days;
}

auto LogAsync::autoDelFileDays() -> qint64
{
return d_ptr->autoDelFileDays;
}

void LogAsync::setOrientation(LogAsync::Orientation orientation)
Expand Down Expand Up @@ -132,7 +170,7 @@ void LogAsync::run()

LogAsync::LogAsync(QObject *parent)
: QThread(parent)
, d_ptr(new LogAsyncPrivate)
, d_ptr(new LogAsyncPrivate(this))
{
qInstallMessageHandler(messageHandler);
}
Expand Down
16 changes: 12 additions & 4 deletions LogAsynchronous/logasync.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <QMutex>
#include <QThread>

struct LogAsyncPrivate;
class LogAsync : public QThread
{
Q_OBJECT
Expand All @@ -13,10 +12,19 @@ class LogAsync : public QThread

static auto instance() -> LogAsync *;

void setOrientation(Orientation);
void setLogPath(const QString &path);
auto logPath() -> QString;

void setAutoDelFile(bool on);
auto autoDelFile() -> bool;

void setAutoDelFileDays(qint64 days);
auto autoDelFileDays() -> qint64;

void setOrientation(Orientation /*orientation*/);
auto orientation() -> Orientation;

void setLogLevel(QtMsgType);
void setLogLevel(QtMsgType /*type*/);
auto logLevel() -> QtMsgType;

void startWork();
Expand All @@ -32,7 +40,7 @@ class LogAsync : public QThread
explicit LogAsync(QObject *parent = nullptr);
~LogAsync() override;

static QMutex m_mutex;
class LogAsyncPrivate;
QScopedPointer<LogAsyncPrivate> d_ptr;
};

Expand Down
18 changes: 0 additions & 18 deletions LogAsynchronous/main.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
#include <QApplication>
#include <QElapsedTimer>
#include <QDebug>

#include "logasync.h"
#include "mainwindow.h"

auto main(int argc, char *argv[]) -> int
{
QApplication a(argc, argv);

LogAsync *log = LogAsync::instance();
log->setOrientation(LogAsync::Orientation::StdAndFile);
log->setLogLevel(QtDebugMsg);
log->startWork();

// QElapsedTimer timer;
// timer.start();

// for(int i=0; i< 1000 * 1000; i++){
// qInfo() << "1234567890qwertyuiopasdfghjklzxcvbnm" << i;
// }

// qInfo() << timer.elapsed();

MainWindow w;
w.show();

int result = a.exec();
log->stop();
return result;
}
Loading

0 comments on commit 259b83a

Please sign in to comment.