fix: use second-level log timestamps

This commit is contained in:
2026-07-14 08:50:44 +08:00
parent 62ed176bdd
commit b41d744965
3 changed files with 25 additions and 15 deletions
+20 -10
View File
@@ -45,6 +45,7 @@ struct LogState
qint64 currentFileBytes = 0;
int currentPart = 0;
QDate activeDate;
QString currentFilePrefix;
bool installed = false;
#if defined(MYLOGGER_TESTING)
// 测试编译时将文件系统副作用收敛到临时目录,并用小阈值快速覆盖分卷逻辑。
@@ -174,11 +175,18 @@ QString shortFileName(const QMessageLogContext &context)
return separatorIndex >= 0 ? fileName.mid(separatorIndex + 1) : fileName;
}
// 仅清理本模块按日期、进程号和分卷号命名的日志,避免公共 logs 目录中的宿主业务文件被误删。
// 日志文件以一次打开日志时的本地秒级时间作为会话标识;分卷只追加序号,
// 既能让现场直接看出开始时间,也避免把进程 ID 暴露到文件名中。
QString createLogFilePrefix(const QDateTime &dateTime)
{
return dateTime.toString(QStringLiteral("yyyy-MM-dd_hhmmss"));
}
// 仅清理本模块按“日期_时分秒_分卷”命名的日志,避免公共 logs 目录中的宿主业务文件被误删。
void removeExpiredLogs(const QDir &logDir, const QDate &today)
{
const QRegularExpression logNamePattern(
QStringLiteral("^\\d{4}-\\d{2}-\\d{2}_\\d+_\\d{3}\\.log$"));
QStringLiteral("^\\d{4}-\\d{2}-\\d{2}_\\d{6}_\\d{3}\\.log$"));
const QDate oldestDate = today.addDays(-kLogRetentionDays);
const QFileInfoList files = logDir.entryInfoList(QStringList() << QStringLiteral("*.log"),
QDir::Files | QDir::Readable);
@@ -197,12 +205,15 @@ void removeExpiredLogs(const QDir &logDir, const QDate &today)
}
}
// 在锁保护下选择当前进程当天可继续写入的分卷;已满分卷永不复用,防止重启后突破大小上限。
// 在锁保护下为当前日期选择可继续写入的分卷;同一会话始终复用首次生成的详细时间前缀,
// 已满分卷永不复用,防止重启后突破大小上限。
bool openCurrentLogFileLocked(LogState &state, const QDir &logDir, const QDate &today)
{
const QString prefix = QStringLiteral("%1_%2_")
.arg(today.toString(QStringLiteral("yyyy-MM-dd")))
.arg(QCoreApplication::applicationPid());
if (state.currentFilePrefix.isEmpty() || state.activeDate != today)
{
state.currentFilePrefix = createLogFilePrefix(QDateTime::currentDateTime());
}
const QString prefix = state.currentFilePrefix + QLatin1Char('_');
int part = 1;
while (true)
{
@@ -232,14 +243,12 @@ bool openCurrentLogFileLocked(LogState &state, const QDir &logDir, const QDate &
}
}
// 在当前文件即将超过上限时切到下一个空闲分卷;连续序号便于现场按时间顺序定位日志。
// 在当前文件即将超过上限时切到同一详细时间会话的下一个空闲分卷;连续序号便于现场按顺序定位日志。
bool rotateLogFileLocked(LogState &state)
{
const QFileInfo currentInfo(state.file);
const QDir logDir = currentInfo.dir();
const QString prefix = QStringLiteral("%1_%2_")
.arg(QDate::currentDate().toString(QStringLiteral("yyyy-MM-dd")))
.arg(QCoreApplication::applicationPid());
const QString prefix = state.currentFilePrefix + QLatin1Char('_');
int part = state.currentPart + 1;
QString filePath;
do
@@ -396,6 +405,7 @@ void LogHandler::uninstallMessageHandler()
g_logState.currentFileBytes = 0;
g_logState.currentPart = 0;
g_logState.activeDate = QDate();
g_logState.currentFilePrefix.clear();
g_logState.installed = false;
}