From 2d5f0042223e23724adae96369fc63de98e273e1 Mon Sep 17 00:00:00 2001 From: otsmr Date: Sun, 5 Apr 2026 22:06:08 +0200 Subject: [PATCH] improve log cleaning --- lib/src/utils/log.dart | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/src/utils/log.dart b/lib/src/utils/log.dart index 09646b1..f40302e 100644 --- a/lib/src/utils/log.dart +++ b/lib/src/utils/log.dart @@ -126,17 +126,34 @@ Future cleanLogFile() async { return _protectFileAccess(() async { final logFile = File('$globalApplicationSupportDirectory/app.log'); - if (logFile.existsSync()) { - final lines = await logFile.readAsLines(); - - if (lines.length <= 100000) return; - - final removeCount = lines.length - 100000; - final remaining = lines.sublist(removeCount, lines.length); - - final sink = logFile.openWrite()..writeAll(remaining, '\n'); - await sink.close(); + if (!logFile.existsSync()) { + return; } + final lines = await logFile.readAsLines(); + + final twoWeekAgo = clock.now().subtract(const Duration(days: 14)); + var keepStartIndex = -1; + + for (var i = 0; i < lines.length; i += 100) { + if (lines[i].length >= 19) { + final date = DateTime.tryParse(lines[i].substring(0, 19)); + if (date != null && date.isAfter(twoWeekAgo)) { + keepStartIndex = i; + break; + } + } + } + + if (keepStartIndex == 0) return; + + if (keepStartIndex == -1) { + await logFile.writeAsString(''); + return; + } + + final remaining = lines.sublist(keepStartIndex); + final sink = logFile.openWrite()..writeAll(remaining, '\n'); + await sink.close(); }); }