Skip to content

Commit d5f2efa

Browse files
committed
[更新项目配置以支持最新的Qt6和Windows开发环境]: 主要涉及项目构建和日志处理模块的更新,以适配最新的Qt6库和Windows开发环境。
- 更新了`.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安装路径和架构设置。
1 parent 07ac631 commit d5f2efa

File tree

10 files changed

+111
-89
lines changed

10 files changed

+111
-89
lines changed

.github/workflows/qmake.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
fail-fast: false
3131
matrix:
3232
os:
33-
- windows-2019
33+
- windows-latest
3434
- macos-latest
3535
- ubuntu-latest
3636

@@ -54,7 +54,7 @@ jobs:
5454
if: startsWith(matrix.os, 'windows')
5555
shell: pwsh
5656
run: |
57-
..\scripts\windows\setVsDev.ps1 -VersionRange "[16.0,17.0)" -Arch "x64"
57+
..\scripts\windows\setVsDev.ps1
5858
& qmake ./../.
5959
& jom
6060
working-directory: build

LogAsynchronous/CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
set(PROJECT_SOURCES
2-
main.cpp
3-
mainwindow.cpp
4-
mainwindow.h
5-
fileutil.h
6-
fileutil.cpp
7-
logasync.h
8-
logasync.cpp)
2+
logasync.cpp
3+
logasync.h
4+
logfile.cc
5+
logfile.hpp
6+
main.cpp
7+
mainwindow.cpp
8+
mainwindow.h)
99

1010
qt_add_executable(LogAsynchronous MANUAL_FINALIZATION ${PROJECT_SOURCES})
1111
target_link_libraries(LogAsynchronous PRIVATE Qt6::Widgets Qt6::Concurrent)

LogAsynchronous/LogAsynchronous.pro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
1414
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
1515

1616
SOURCES += \
17-
fileutil.cpp \
1817
logasync.cpp \
18+
logfile.cc \
1919
main.cpp \
2020
mainwindow.cpp
2121

2222
HEADERS += \
23-
fileutil.h \
2423
logasync.h \
24+
logfile.hpp \
2525
mainwindow.h
2626

2727
# Default rules for deployment.

LogAsynchronous/fileutil.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

LogAsynchronous/logasync.cpp

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "logasync.h"
2-
#include "fileutil.h"
2+
#include "logfile.hpp"
33

44
#include <QDateTime>
5+
#include <QMutex>
56
#include <QWaitCondition>
67

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

41-
const QString dataTimeString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
42-
const QString threadId = QString("%1").arg(reinterpret_cast<quint64>(QThread::currentThreadId()),
43-
5,
44-
10,
45-
QLatin1Char('0'));
42+
const auto dataTimeString(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
43+
const auto threadId = QString("%1").arg(reinterpret_cast<quint64>(QThread::currentThreadId()),
44+
5,
45+
10,
46+
QLatin1Char('0'));
4647
// By default, this information is recorded only in debug builds.
4748
// You can overwrite this explicitly by defining QT_MESSAGELOGCONTEXT or QT_NO_MESSAGELOGCONTEXT.
4849
QString contexInfo;
4950
#ifndef QT_NO_DEBUG
5051
contexInfo = QString("File:(%1) Line:(%2)").arg(context.file).arg(context.line);
5152
#endif
52-
const auto message = QString("%1 %2 [%3] %4 - %5\n")
53-
.arg(dataTimeString, threadId, level, msg, contexInfo);
53+
const auto printToFile = QString("%1 %2 [%3] %4 - %5\n")
54+
.arg(dataTimeString, threadId, level, msg, contexInfo);
55+
QString printToConsole;
56+
if (msg.size() > instance->maxConsoleLineSize()) {
57+
printToConsole = QString("%1 %2 [%3] %4 - %5\n")
58+
.arg(dataTimeString,
59+
threadId,
60+
level,
61+
msg.left(instance->maxConsoleLineSize()),
62+
contexInfo);
63+
} else {
64+
printToConsole = printToFile;
65+
}
5466

5567
switch (instance->orientation()) {
56-
case LogAsync::Orientation::Std:
57-
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
58-
::fflush(stdPrint);
59-
break;
60-
case LogAsync::Orientation::File: emit instance->appendBuf(message); break;
68+
case LogAsync::Orientation::File: emit instance->appendBuf(printToFile); break;
6169
case LogAsync::Orientation::StdAndFile:
62-
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
70+
emit instance->appendBuf(printToFile);
71+
fprintf(stdPrint, "%s", printToConsole.toLocal8Bit().constData());
6372
::fflush(stdPrint);
64-
emit instance->appendBuf(message);
6573
break;
6674
default:
67-
fprintf(stdPrint, "%s", message.toLocal8Bit().constData());
75+
fprintf(stdPrint, "%s", printToConsole.toLocal8Bit().constData());
6876
::fflush(stdPrint);
6977
break;
7078
}
@@ -84,6 +92,7 @@ class LogAsync::LogAsyncPrivate
8492
qint64 autoDelFileDays = 7;
8593
QtMsgType msgType = QtWarningMsg;
8694
LogAsync::Orientation orientation = LogAsync::Orientation::Std;
95+
int maxConsoleLineSize = 1024 * 10;
8796
QWaitCondition waitCondition;
8897
QMutex mutex;
8998
};
@@ -144,26 +153,39 @@ auto LogAsync::logLevel() -> QtMsgType
144153
return d_ptr->msgType;
145154
}
146155

156+
void LogAsync::setMaxConsoleLineSize(int size)
157+
{
158+
if (size < 1) {
159+
return;
160+
}
161+
d_ptr->maxConsoleLineSize = size;
162+
}
163+
164+
auto LogAsync::maxConsoleLineSize() -> int
165+
{
166+
return d_ptr->maxConsoleLineSize;
167+
}
168+
147169
void LogAsync::startWork()
148170
{
149171
start();
150172
QMutexLocker lock(&d_ptr->mutex);
151-
d_ptr->waitCondition.wait(&d_ptr->mutex);
173+
d_ptr->waitCondition.wait(&d_ptr->mutex, 5000);
152174
}
153175

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

163185
void LogAsync::run()
164186
{
165-
FileUtil fileUtil;
166-
connect(this, &LogAsync::appendBuf, &fileUtil, &FileUtil::onWrite);
187+
LogFile logFile;
188+
connect(this, &LogAsync::appendBuf, &logFile, &LogFile::onWrite);
167189
d_ptr->waitCondition.wakeOne();
168190
exec();
169191
}

LogAsynchronous/logasync.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
#ifndef LOGASYNC_H
2-
#define LOGASYNC_H
1+
#pragma once
32

4-
#include <QMutex>
53
#include <QThread>
64

75
class LogAsync : public QThread
86
{
97
Q_OBJECT
108
public:
11-
enum Orientation { Std = 1, File, StdAndFile };
9+
enum Orientation : int { Std = 1, File, StdAndFile };
1210

1311
static auto instance() -> LogAsync *;
1412

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

24-
void setOrientation(Orientation /*orientation*/);
22+
void setOrientation(Orientation orientation);
2523
auto orientation() -> Orientation;
2624

27-
void setLogLevel(QtMsgType /*type*/);
25+
void setLogLevel(QtMsgType type);
2826
auto logLevel() -> QtMsgType;
2927

28+
void setMaxConsoleLineSize(int size);
29+
auto maxConsoleLineSize() -> int;
30+
3031
void startWork();
3132
void stop();
3233

@@ -43,5 +44,3 @@ class LogAsync : public QThread
4344
class LogAsyncPrivate;
4445
QScopedPointer<LogAsyncPrivate> d_ptr;
4546
};
46-
47-
#endif // LOGASYNC_H
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#include "fileutil.h"
1+
#include "logfile.hpp"
22
#include "logasync.h"
33

44
#include <QCoreApplication>
55
#include <QDateTime>
66
#include <QDir>
7-
#include <QFile>
8-
#include <QProcess>
9-
#include <QTextStream>
107
#include <QTimer>
118

129
#define ROLLSIZE (1000 * 1000 * 1000)
@@ -28,31 +25,31 @@ static auto getFileName(qint64 seconds) -> QString
2825
static void autoDelFile()
2926
{
3027
auto *instance = LogAsync::instance();
31-
const QString path(instance->logPath());
28+
const auto path(instance->logPath());
3229
QDir dir(path);
3330
if (!dir.exists()) {
3431
return;
3532
}
3633

37-
const QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Time);
38-
const QDateTime cur = QDateTime::currentDateTime();
39-
const QDateTime pre = cur.addDays(-instance->autoDelFileDays());
34+
const auto list = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Time);
35+
const auto cur = QDateTime::currentDateTime();
36+
const auto pre = cur.addDays(-instance->autoDelFileDays());
4037

41-
for (const QFileInfo &info : std::as_const(list)) {
38+
for (const auto &info : std::as_const(list)) {
4239
if (info.lastModified() <= pre) {
4340
dir.remove(info.fileName());
4441
}
4542
}
4643
}
4744

48-
class FileUtil::FileUtilPrivate
45+
class LogFile::LogFilePrivate
4946
{
5047
public:
51-
explicit FileUtilPrivate(FileUtil *q)
48+
explicit LogFilePrivate(LogFile *q)
5249
: q_ptr(q)
5350
{}
5451

55-
FileUtil *q_ptr;
52+
LogFile *q_ptr;
5653

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

65-
FileUtil::FileUtil(QObject *parent)
62+
LogFile::LogFile(QObject *parent)
6663
: QObject(parent)
67-
, d_ptr(new FileUtilPrivate(this))
64+
, d_ptr(new LogFilePrivate(this))
6865
{
6966
rollFile(0);
7067
setTimer();
7168
}
7269

73-
FileUtil::~FileUtil()
70+
LogFile::~LogFile()
7471
{
7572
onFlush();
7673
}
7774

78-
void FileUtil::onWrite(const QString &msg)
75+
void LogFile::onWrite(const QString &msg)
7976
{
8077
if (d_ptr->file.size() > ROLLSIZE) {
8178
rollFile(++d_ptr->count);
@@ -91,12 +88,12 @@ void FileUtil::onWrite(const QString &msg)
9188
d_ptr->stream << msg;
9289
}
9390

94-
void FileUtil::onFlush()
91+
void LogFile::onFlush()
9592
{
9693
d_ptr->stream.flush();
9794
}
9895

99-
auto FileUtil::rollFile(int count) -> bool
96+
auto LogFile::rollFile(int count) -> bool
10097
{
10198
qint64 now = QDateTime::currentSecsSinceEpoch();
10299
QString filename = getFileName(now);
@@ -122,9 +119,9 @@ auto FileUtil::rollFile(int count) -> bool
122119
return false;
123120
}
124121

125-
void FileUtil::setTimer()
122+
void LogFile::setTimer()
126123
{
127124
auto *timer = new QTimer(this);
128-
connect(timer, &QTimer::timeout, this, &FileUtil::onFlush);
125+
connect(timer, &QTimer::timeout, this, &LogFile::onFlush);
129126
timer->start(5000); // 5秒刷新一次
130127
}

LogAsynchronous/logfile.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <QObject>
4+
5+
class LogFile : public QObject
6+
{
7+
Q_OBJECT
8+
public:
9+
explicit LogFile(QObject *parent = nullptr);
10+
~LogFile() override;
11+
12+
public slots:
13+
void onWrite(const QString &msg);
14+
15+
private slots:
16+
void onFlush();
17+
18+
private:
19+
auto rollFile(int count) -> bool;
20+
void setTimer();
21+
22+
class LogFilePrivate;
23+
QScopedPointer<LogFilePrivate> d_ptr;
24+
};

cmake/qt.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ elseif(CMAKE_HOST_APPLE)
55
elseif(CMAKE_HOST_LINUX)
66
list(APPEND CMAKE_PREFIX_PATH "/opt/Qt/6.8.1/gcc_64")
77
endif()
8+
9+
add_definitions(-DQT_DEPRECATED_WARNINGS)

0 commit comments

Comments
 (0)