Skip to content

Commit

Permalink
fix: 日期转换失败导致日志无法显示,新增fatal等级记录,统一日志文件获取
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Feb 16, 2024
1 parent b9e0ec9 commit ba9c05a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
13 changes: 9 additions & 4 deletions lib/pages/setting/pages/logs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ class _LogsPageState extends State<LogsPage> {
}).toList();
List<Map<String, dynamic>> result = [];
for (String i in contentList) {
DateTime? date;
dynamic date;
String body = i
.split("\n")
.map((l) {
if (l.startsWith("Crash occurred on")) {
date = DateTime.parse(
l.split("Crash occurred on")[1].trim().split('.')[0],
);
try {
date = DateTime.parse(
l.split("Crash occurred on")[1].trim().split('.')[0],
);
} catch (e) {
print(e.toString());
date = l.toString();
}
return "";
}
return l;
Expand Down
16 changes: 6 additions & 10 deletions lib/services/loggeer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ class PiliLogger extends Logger {
@override
void log(Level level, dynamic message,
{Object? error, StackTrace? stackTrace, DateTime? time}) async {
if (level == Level.error) {
String dir = (await getApplicationDocumentsDirectory()).path;
// 创建logo文件
final String filename = p.join(dir, ".pili_logs");
if (level == Level.error || level == Level.fatal) {
// 添加至文件末尾
await File(filename).writeAsString(
File logFile = await getLogsPath();
logFile.writeAsString(
"**${DateTime.now()}** \n $message \n $stackTrace",
mode: FileMode.writeOnlyAppend,
);
Expand All @@ -35,17 +33,15 @@ class PiliLogger extends Logger {
Future<File> getLogsPath() async {
String dir = (await getApplicationDocumentsDirectory()).path;
final String filename = p.join(dir, ".pili_logs");
final file = File(filename);
final File file = File(filename);
if (!await file.exists()) {
await file.create();
await file.create(recursive: true);
}
return file;
}

Future<bool> clearLogs() async {
String dir = (await getApplicationDocumentsDirectory()).path;
final String filename = p.join(dir, ".pili_logs");
final file = File(filename);
final File file = await getLogsPath();
try {
await file.writeAsString('');
} catch (e) {
Expand Down

0 comments on commit ba9c05a

Please sign in to comment.