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) {