From 69ff2c6a612e883796c451b62aef64ffb28dbacf Mon Sep 17 00:00:00 2001 From: Komodo <45665554+Komodo5197@users.noreply.github.com> Date: Thu, 25 Jan 2024 20:44:12 -0500 Subject: [PATCH] Disabled logging to file. --- lib/main.dart | 6 +++- lib/services/finamp_logs_helper.dart | 47 +++++++++++++++------------- lib/setup_logging.dart | 4 +-- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 5fd537213..53fec2367 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -62,7 +62,7 @@ void main() async { // If the app has failed, this is set to true. If true, we don't attempt to run the main app since the error app has started. bool hasFailed = false; try { - setupLogging(); + await setupLogging(); await setupHive(); _migrateDownloadLocations(); _migrateSortOptions(); @@ -434,6 +434,10 @@ class ErrorScreen extends StatelessWidget { AppLocalizations.of(context)!.startupError(error.toString()), ), ), + /*bottomNavigationBar: const Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ShareLogsButton(), CopyLogsButton()], + ),*/ ); } } diff --git a/lib/services/finamp_logs_helper.dart b/lib/services/finamp_logs_helper.dart index 1258bcfb6..c2cd36d12 100644 --- a/lib/services/finamp_logs_helper.dart +++ b/lib/services/finamp_logs_helper.dart @@ -14,22 +14,22 @@ class FinampLogsHelper { final List logs = []; IOSink? _logFileWriter; - FinampLogsHelper() { - Future.sync(() async { - WidgetsFlutterBinding.ensureInitialized(); - final basePath = await getApplicationDocumentsDirectory(); - final logFile = File(path_helper.join(basePath.path, "finamp-logs.txt")); - if (logFile.existsSync() && logFile.lengthSync() >= 1024 * 1024 * 10) { - logFile - .renameSync(path_helper.join(basePath.path, "finamp-logs-old.txt")); - } - _logFileWriter = logFile.openWrite(mode: FileMode.writeOnlyAppend); - }); + // Logging to a file is currently disabled. + Future openLog() async { + WidgetsFlutterBinding.ensureInitialized(); + final basePath = await getApplicationDocumentsDirectory(); + final logFile = File(path_helper.join(basePath.path, "finamp-logs.txt")); + if (logFile.existsSync() && logFile.lengthSync() >= 1024 * 1024 * 10) { + logFile + .renameSync(path_helper.join(basePath.path, "finamp-logs-old.txt")); + } + _logFileWriter = logFile.openWrite(mode: FileMode.writeOnlyAppend); } void addLog(LogRecord log) { logs.add(log); if (_logFileWriter != null) { + // This fails if we log an event before setting up userHelper var message = log.censoredMessage; if (log.stackTrace == null) { // Truncate long messages from chopper, but leave long stack traces @@ -64,16 +64,21 @@ class FinampLogsHelper { final tempFile = File(path_helper.join(tempDir.path, "finamp-logs.txt")); tempFile.createSync(); - final basePath = await getApplicationDocumentsDirectory(); - var oldLogs = File(path_helper.join(basePath.path, "finamp-logs-old.txt")); - var newLogs = File(path_helper.join(basePath.path, "finamp-logs.txt")); - if (oldLogs.existsSync()) { - await tempFile.writeAsBytes(await oldLogs.readAsBytes(), - mode: FileMode.writeOnly); - } - if (newLogs.existsSync()) { - await tempFile.writeAsBytes(await newLogs.readAsBytes(), - mode: FileMode.writeOnlyAppend); + if (_logFileWriter != null) { + final basePath = await getApplicationDocumentsDirectory(); + var oldLogs = + File(path_helper.join(basePath.path, "finamp-logs-old.txt")); + var newLogs = File(path_helper.join(basePath.path, "finamp-logs.txt")); + if (oldLogs.existsSync()) { + await tempFile.writeAsBytes(await oldLogs.readAsBytes(), + mode: FileMode.writeOnly); + } + if (newLogs.existsSync()) { + await tempFile.writeAsBytes(await newLogs.readAsBytes(), + mode: FileMode.writeOnlyAppend); + } + } else { + await tempFile.writeAsString(getSanitisedLogs()); } final xFile = XFile(tempFile.path, mimeType: "text/plain"); diff --git a/lib/setup_logging.dart b/lib/setup_logging.dart index 65cc1a061..b5a19c48e 100644 --- a/lib/setup_logging.dart +++ b/lib/setup_logging.dart @@ -4,8 +4,9 @@ import 'package:logging/logging.dart'; import 'services/finamp_logs_helper.dart'; -void setupLogging() { +Future setupLogging() async { GetIt.instance.registerSingleton(FinampLogsHelper()); + //await GetIt.instance().openLog(); Logger.root.level = Level.ALL; Logger.root.onRecord.listen((event) { final finampLogsHelper = GetIt.instance(); @@ -18,7 +19,6 @@ void setupLogging() { if (kDebugMode && event.loggerName != "Flutter" && event.stackTrace != null) { - // ignore: avoid_print debugPrintStack(stackTrace: event.stackTrace); } finampLogsHelper.addLog(event);