app/lib/common/utils/log.dart

44 lines
1.0 KiB
Dart
Raw Normal View History

2024-03-01 20:59:43 +08:00
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:synchronized/synchronized.dart';
2024-03-15 00:01:06 +08:00
export 'package:starcitizen_doctor/generated/l10n.dart';
2024-03-01 20:59:43 +08:00
var _logLock = Lock();
2024-03-07 23:01:32 +08:00
File? _logFile;
2024-03-01 20:59:43 +08:00
void dPrint(src) async {
if (kDebugMode) {
print(src);
2024-05-03 22:35:31 +08:00
return;
2024-03-01 20:59:43 +08:00
}
2024-03-23 17:53:42 +08:00
await _logLock.synchronized(() async {
try {
2024-03-23 17:54:32 +08:00
await _logFile?.writeAsString("$src\n", mode: FileMode.append);
2024-03-23 17:53:42 +08:00
} catch (_) {}
});
2024-03-01 20:59:43 +08:00
}
2024-03-07 23:01:32 +08:00
2024-05-03 13:12:11 +08:00
Future<void> initDPrintFile(String applicationSupportDir) async {
final now = DateTime.now();
final logFile =
2024-06-16 11:52:25 +08:00
File("$applicationSupportDir/logs/${now.millisecondsSinceEpoch}.log");
2024-05-03 13:12:11 +08:00
await logFile.create(recursive: true);
_logFile = logFile;
2024-06-16 11:52:25 +08:00
final logsDir = Directory("$applicationSupportDir/logs");
2024-05-03 13:12:11 +08:00
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();
}
}
}
2024-03-07 23:01:32 +08:00
}
2024-03-10 19:44:53 +08:00
File? getDPrintFile() {
return _logFile;
}