From ba9c05a861f7ab9e180d8da13cfbf243365ffb5f Mon Sep 17 00:00:00 2001 From: orz12 Date: Fri, 16 Feb 2024 20:19:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A5=E6=9C=9F=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=AF=BC=E8=87=B4=E6=97=A5=E5=BF=97=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=98=BE=E7=A4=BA=EF=BC=8C=E6=96=B0=E5=A2=9Efatal?= =?UTF-8?q?=E7=AD=89=E7=BA=A7=E8=AE=B0=E5=BD=95=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/setting/pages/logs.dart | 13 +++++++++---- lib/services/loggeer.dart | 16 ++++++---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/pages/setting/pages/logs.dart b/lib/pages/setting/pages/logs.dart index 0958edb8..ec72f7ca 100644 --- a/lib/pages/setting/pages/logs.dart +++ b/lib/pages/setting/pages/logs.dart @@ -45,14 +45,19 @@ class _LogsPageState extends State { }).toList(); List> 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; diff --git a/lib/services/loggeer.dart b/lib/services/loggeer.dart index 5555432c..88f75999 100644 --- a/lib/services/loggeer.dart +++ b/lib/services/loggeer.dart @@ -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, ); @@ -35,17 +33,15 @@ class PiliLogger extends Logger { Future 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 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) {