From dd14a34285d895de2e57e37575ac65089ebb00df Mon Sep 17 00:00:00 2001 From: xkeyC <3334969096@qq.com> Date: Fri, 3 May 2024 13:12:11 +0800 Subject: [PATCH] feat: delete logs when inDays > 7 --- lib/common/utils/log.dart | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/common/utils/log.dart b/lib/common/utils/log.dart index 9356b8d..2184ab2 100644 --- a/lib/common/utils/log.dart +++ b/lib/common/utils/log.dart @@ -19,8 +19,22 @@ void dPrint(src) async { }); } -void setDPrintFile(File file) { - _logFile = file; +Future initDPrintFile(String applicationSupportDir) async { + final now = DateTime.now(); + final logFile = + File("$applicationSupportDir\\logs\\${now.millisecondsSinceEpoch}.log"); + await logFile.create(recursive: true); + _logFile = logFile; + final logsDir = Directory("$applicationSupportDir\\logs"); + await for (final files in logsDir.list()) { + if (files is File) { + final stat = await files.stat(); + if (stat.type == FileSystemEntityType.file && + now.difference(await files.lastModified()).inDays > 7) { + await files.delete(); + } + } + } } File? getDPrintFile() {