feat: migrate Flutter to Cupertino with daily logs and computer groups

This commit is contained in:
tongtong committed 2026-11-13 11:27:46 +08:00
1 parent c568749d23
commit 87e1fff59b
22 files changed
+808 -55

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart' as p;
class DailyLog {
DailyLog(
this.directory, {
this.maxBytes = 5 * 1024 * 1024,
this.maxParts = 5,
this.keepDays = 14,
DateTime Function()? clock,
}) : assert(maxBytes > 0 && maxParts > 0 && keepDays > 0),
clock = clock ?? DateTime.now;
final Directory directory;
final int maxBytes, maxParts, keepDays;
final DateTime Function() clock;
Future<void> _pending = Future.value();
String? error;
String _cleaned = '';
void add(String text) {
final time = clock();
_pending = _pending
.then((_) async {
await directory.create(recursive: true);
final day = time.toIso8601String().substring(0, 10);
if (_cleaned != day) {
final cutoff = DateTime(
time.year,
time.month,
time.day,
).subtract(Duration(days: keepDays - 1));
await for (final entry in directory.list()) {
if (entry is! File) continue;
final match = RegExp(r'^ads-(\d{4}-\d{2}-\d{2})(?:\.\d+)?\.log$')
.firstMatch(p.basename(entry.path));
if (match != null && DateTime.parse(match[1]!).isBefore(cutoff)) {
await entry.delete();
}
}
_cleaned = day;
}
final base = p.join(directory.path, 'ads-$day');
final file = File('$base.log');
if (await file.exists() && await file.length() >= maxBytes) {
final oldest = File('$base.${maxParts - 1}.log');
if (await oldest.exists()) await oldest.delete();
for (var part = maxParts - 2; part >= 1; part--) {
final old = File('$base.$part.log');
if (await old.exists()) await old.rename('$base.${part + 1}.log');
}
if (maxParts > 1) {
await file.rename('$base.1.log');
} else {
await file.delete();
}
}
await file.writeAsString(
'${time.toIso8601String()} $text\n',
mode: FileMode.append,
flush: true,
);
error = null;
})
.catchError((Object e) {
error = e.toString();
});
}
Future<void> flush() => _pending;
}