Skip to content

Commit

Permalink
[更新项目配置以支持最新的Qt6和Windows开发环境]: 主要涉及项目构建和日志处理模块的更新,以适配最新的Qt6库和Windows开…
Browse files Browse the repository at this point in the history
…发环境。

- 更新了`.github/workflows/qmake.yml`文件,将Windows操作系统的版本从`windows-2019`更新为`windows-latest`,以支持最新的Windows版本。
- 在`LogAsynchronous/CMakeLists.txt`中,调整了项目源文件列表,移除了`fileutil.h`和`fileutil.cpp`,并添加了新的`logfile.cc`和`logfile.hpp`文件,以实现日志的异步写入功能。
- `LogAsynchronous/LogAsynchronous.pro`文件中,移除了`fileutil.cpp`,并添加了新的`logfile.cc`文件,同时更新了头文件列表。
- 删除了`LogAsynchronous/fileutil.h`文件,因为它已被新的日志文件处理模块替代。
- 在`LogAsynchronous/logasync.cpp`中,包含了新的`logfile.hpp`头文件,并更新了日志处理逻辑,以支持日志的异步写入到文件和控制台。
- `LogAsynchronous/logasync.h`文件中,添加了新的枚举值和方法,以支持日志方向和日志级别的设置。
- 将`LogAsynchronous/fileutil.cpp`文件重命名为`LogAsynchronous/logfile.cc`,并进行了相应的代码更新,以实现新的日志文件处理逻辑。
- 添加了新的`LogAsynchronous/logfile.hpp`文件,定义了新的`LogFile`类,用于处理日志文件的写入。
- 在`cmake/qt.cmake`文件中,添加了新的编译定义`-DQT_DEPRECATED_WARNINGS`,以启用Qt6中弃用的警告。
- 更新了`scripts/windows/setVsDev.ps1`脚本,以支持最新的Visual Studio安装路径和架构设置。
  • Loading branch information
RealChuan committed Dec 26, 2024
1 parent 07ac631 commit d5f2efa
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 89 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/qmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
fail-fast: false
matrix:
os:
- windows-2019
- windows-latest
- macos-latest
- ubuntu-latest

Expand All @@ -54,7 +54,7 @@ jobs:
if: startsWith(matrix.os, 'windows')
shell: pwsh
run: |
..\scripts\windows\setVsDev.ps1 -VersionRange "[16.0,17.0)" -Arch "x64"
..\scripts\windows\setVsDev.ps1
& qmake ./../.
& jom
working-directory: build
Expand Down
14 changes: 7 additions & 7 deletions LogAsynchronous/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
fileutil.h
fileutil.cpp
logasync.h
logasync.cpp)
logasync.cpp
logasync.h
logfile.cc
logfile.hpp
main.cpp
mainwindow.cpp
mainwindow.h)

qt_add_executable(LogAsynchronous MANUAL_FINALIZATION ${PROJECT_SOURCES})
target_link_libraries(LogAsynchronous PRIVATE Qt6::Widgets Qt6::Concurrent)
Expand Down
4 changes: 2 additions & 2 deletions LogAsynchronous/LogAsynchronous.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
fileutil.cpp \
logasync.cpp \
logfile.cc \
main.cpp \
mainwindow.cpp

HEADERS += \
fileutil.h \
logasync.h \
logfile.hpp \
mainwindow.h

# Default rules for deployment.
Expand Down
27 changes: 0 additions & 27 deletions LogAsynchronous/fileutil.h

This file was deleted.

62 changes: 42 additions & 20 deletions LogAsynchronous/logasync.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "logasync.h"
#include "fileutil.h"
#include "logfile.hpp"

#include <QDateTime>
#include <QMutex>
#include <QWaitCondition>

// 消息处理函数
Expand Down Expand Up @@ -38,33 +39,40 @@ void messageHandler(QtMsgType type, const QMessageLogContext &context, const QSt
default: level = QString("%1").arg("Unknown", -7); break;
}

const QString dataTimeString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
const QString threadId = QString("%1").arg(reinterpret_cast<quint64>(QThread::currentThreadId()),
5,
10,
QLatin1Char('0'));
const auto dataTimeString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
const auto threadId = QString("%1").arg(reinterpret_cast<quint64>(QThread::currentThreadId()),
5,
10,
QLatin1Char('0'));
// By default, this information is recorded only in debug builds.
// You can overwrite this explicitly by defining QT_MESSAGELOGCONTEXT or QT_NO_MESSAGELOGCONTEXT.
QString contexInfo;
#ifndef QT_NO_DEBUG
contexInfo = QString("File:(%1) Line:(%2)").arg(context.file).arg(context.line);
#endif
const auto message = QString("%1 %2 [%3] %4 - %5\n")
.arg(dataTimeString, threadId, level, msg, contexInfo);
const auto printToFile = QString("%1 %2 [%3] %4 - %5\n")
.arg(dataTimeString, threadId, level, msg, contexInfo);
QString printToConsole;
if (msg.size() > instance->maxConsoleLineSize()) {
printToConsole = QString("%1 %2 [%3] %4 - %5\n")
.arg(dataTimeString,
threadId,
level,
msg.left(instance->maxConsoleLineSize()),
contexInfo);
} else {
printToConsole = printToFile;
}

switch (instance->orientation()) {
case LogAsync::Orientation::Std:
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
::fflush(stdPrint);
break;
case LogAsync::Orientation::File: emit instance->appendBuf(message); break;
case LogAsync::Orientation::File: emit instance->appendBuf(printToFile); break;
case LogAsync::Orientation::StdAndFile:
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
emit instance->appendBuf(printToFile);
fprintf(stdPrint, "%s", printToConsole.toLocal8Bit().constData());
::fflush(stdPrint);
emit instance->appendBuf(message);
break;
default:
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
fprintf(stdPrint, "%s", printToConsole.toLocal8Bit().constData());
::fflush(stdPrint);
break;
}
Expand All @@ -84,6 +92,7 @@ class LogAsync::LogAsyncPrivate
qint64 autoDelFileDays = 7;
QtMsgType msgType = QtWarningMsg;
LogAsync::Orientation orientation = LogAsync::Orientation::Std;
int maxConsoleLineSize = 1024 * 10;
QWaitCondition waitCondition;
QMutex mutex;
};
Expand Down Expand Up @@ -144,26 +153,39 @@ auto LogAsync::logLevel() -> QtMsgType
return d_ptr->msgType;
}

void LogAsync::setMaxConsoleLineSize(int size)
{
if (size < 1) {
return;
}
d_ptr->maxConsoleLineSize = size;
}

auto LogAsync::maxConsoleLineSize() -> int
{
return d_ptr->maxConsoleLineSize;
}

void LogAsync::startWork()
{
start();
QMutexLocker lock(&d_ptr->mutex);
d_ptr->waitCondition.wait(&d_ptr->mutex);
d_ptr->waitCondition.wait(&d_ptr->mutex, 5000);
}

void LogAsync::stop()
{
// 可能有部分日志未写入文件,异步信号槽机制问题
if (isRunning()) {
//QThread::sleep(1); // 最后一条日志格式化可能来不及进入信号槽
quit();
wait();
}
}

void LogAsync::run()
{
FileUtil fileUtil;
connect(this, &LogAsync::appendBuf, &fileUtil, &FileUtil::onWrite);
LogFile logFile;
connect(this, &LogAsync::appendBuf, &logFile, &LogFile::onWrite);
d_ptr->waitCondition.wakeOne();
exec();
}
Expand Down
15 changes: 7 additions & 8 deletions LogAsynchronous/logasync.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#ifndef LOGASYNC_H
#define LOGASYNC_H
#pragma once

#include <QMutex>
#include <QThread>

class LogAsync : public QThread
{
Q_OBJECT
public:
enum Orientation { Std = 1, File, StdAndFile };
enum Orientation : int { Std = 1, File, StdAndFile };

static auto instance() -> LogAsync *;

Expand All @@ -21,12 +19,15 @@ class LogAsync : public QThread
void setAutoDelFileDays(qint64 days);
auto autoDelFileDays() -> qint64;

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

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

void setMaxConsoleLineSize(int size);
auto maxConsoleLineSize() -> int;

void startWork();
void stop();

Expand All @@ -43,5 +44,3 @@ class LogAsync : public QThread
class LogAsyncPrivate;
QScopedPointer<LogAsyncPrivate> d_ptr;
};

#endif // LOGASYNC_H
37 changes: 17 additions & 20 deletions LogAsynchronous/fileutil.cpp → LogAsynchronous/logfile.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include "fileutil.h"
#include "logfile.hpp"
#include "logasync.h"

#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QTextStream>
#include <QTimer>

#define ROLLSIZE (1000 * 1000 * 1000)
Expand All @@ -28,31 +25,31 @@ static auto getFileName(qint64 seconds) -> QString
static void autoDelFile()
{
auto *instance = LogAsync::instance();
const QString path(instance->logPath());
const auto path(instance->logPath());
QDir dir(path);
if (!dir.exists()) {
return;
}

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

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

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

FileUtil *q_ptr;
LogFile *q_ptr;

QFile file;
//QTextStream 读写分离的,内部有缓冲区static const int QTEXTSTREAM_BUFFERSIZE = 16384;
Expand All @@ -62,20 +59,20 @@ class FileUtil::FileUtilPrivate
int count = 0;
};

FileUtil::FileUtil(QObject *parent)
LogFile::LogFile(QObject *parent)
: QObject(parent)
, d_ptr(new FileUtilPrivate(this))
, d_ptr(new LogFilePrivate(this))
{
rollFile(0);
setTimer();
}

FileUtil::~FileUtil()
LogFile::~LogFile()
{
onFlush();
}

void FileUtil::onWrite(const QString &msg)
void LogFile::onWrite(const QString &msg)
{
if (d_ptr->file.size() > ROLLSIZE) {
rollFile(++d_ptr->count);
Expand All @@ -91,12 +88,12 @@ void FileUtil::onWrite(const QString &msg)
d_ptr->stream << msg;
}

void FileUtil::onFlush()
void LogFile::onFlush()
{
d_ptr->stream.flush();
}

auto FileUtil::rollFile(int count) -> bool
auto LogFile::rollFile(int count) -> bool
{
qint64 now = QDateTime::currentSecsSinceEpoch();
QString filename = getFileName(now);
Expand All @@ -122,9 +119,9 @@ auto FileUtil::rollFile(int count) -> bool
return false;
}

void FileUtil::setTimer()
void LogFile::setTimer()
{
auto *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &FileUtil::onFlush);
connect(timer, &QTimer::timeout, this, &LogFile::onFlush);
timer->start(5000); // 5秒刷新一次
}
24 changes: 24 additions & 0 deletions LogAsynchronous/logfile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <QObject>

class LogFile : public QObject
{
Q_OBJECT
public:
explicit LogFile(QObject *parent = nullptr);
~LogFile() override;

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

private slots:
void onFlush();

private:
auto rollFile(int count) -> bool;
void setTimer();

class LogFilePrivate;
QScopedPointer<LogFilePrivate> d_ptr;
};
2 changes: 2 additions & 0 deletions cmake/qt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ elseif(CMAKE_HOST_APPLE)
elseif(CMAKE_HOST_LINUX)
list(APPEND CMAKE_PREFIX_PATH "/opt/Qt/6.8.1/gcc_64")
endif()

add_definitions(-DQT_DEPRECATED_WARNINGS)
Loading

0 comments on commit d5f2efa

Please sign in to comment.