diff --git a/lib/main.dart b/lib/main.dart index 5ea21aec..1ae94fd1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,7 +3,6 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; -import 'package:flutter_logs/flutter_logs.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:frosty/frosty.dart'; import 'package:hive_ce_flutter/hive_flutter.dart'; @@ -176,21 +175,7 @@ void main() async { if (!kIsWeb) { //init logger - await FlutterLogs.initLogs( - logLevelsEnabled: [ - LogLevel.INFO, - LogLevel.WARNING, - LogLevel.ERROR, - LogLevel.SEVERE, - ], - timeStampFormat: TimeStampFormat.TIME_FORMAT_READABLE, - directoryStructure: DirectoryStructure.FOR_DATE, - logFileExtension: LogFileExtension.LOG, - logsWriteDirectoryName: 'MyLogs', - logsExportDirectoryName: 'MyLogs/Exported', - debugFileOperations: true, - isDebuggable: true, - ); + await LoggerWrapper.initLogs(); LoggerWrapper.logInfo('main', 'initLogs', 'Init logs..'); diff --git a/lib/screens/settings/app_settings_screen.dart b/lib/screens/settings/app_settings_screen.dart index f42ec7c3..86322112 100644 --- a/lib/screens/settings/app_settings_screen.dart +++ b/lib/screens/settings/app_settings_screen.dart @@ -1,7 +1,6 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; -import 'package:flutter_logs/flutter_logs.dart'; import 'package:flutter/material.dart'; import 'package:peercoin/screens/settings/settings_helpers.dart'; @@ -11,7 +10,7 @@ import '../../providers/wallet_provider.dart'; import '../../providers/app_settings_provider.dart'; import '../../tools/app_localizations.dart'; import '../../tools/auth.dart'; -import '../../tools/debug_log_handler.dart'; +import '../../tools/logger_wrapper.dart'; import '../../tools/share_wrapper.dart'; import '../../widgets/buttons.dart'; import '../../widgets/double_tab_to_clipboard.dart'; @@ -40,8 +39,6 @@ class _AppSettingsScreenState extends State { await _settings.init(); //only required in home widget await _activeWallets.init(); - await initDebugLogHandler(); - setState(() { _initial = false; }); @@ -171,7 +168,7 @@ class _AppSettingsScreenState extends State { PeerButton( text: AppLocalizations.instance .translate('app_settings_logs_export'), - action: () => FlutterLogs.exportLogs(), + action: () => LoggerWrapper.exportLogs(), ), ], ), diff --git a/lib/screens/wallet/wallet_list.dart b/lib/screens/wallet/wallet_list.dart index 35506b52..629ae74e 100644 --- a/lib/screens/wallet/wallet_list.dart +++ b/lib/screens/wallet/wallet_list.dart @@ -4,7 +4,6 @@ import 'dart:io'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_logs/flutter_logs.dart'; import 'package:loader_overlay/loader_overlay.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:peercoin/screens/wallet/roast/roast_wallet_home.dart'; @@ -20,7 +19,6 @@ import '../../tools/app_localizations.dart'; import '../../tools/app_routes.dart'; import '../../tools/auth.dart'; import '../../tools/background_sync.dart'; -import '../../tools/debug_log_handler.dart'; import '../../tools/periodic_reminders.dart'; import '../../tools/price_ticker.dart'; import '../../tools/session_checker.dart'; @@ -557,9 +555,8 @@ class _WalletListScreenState extends State e.toString(), ); - //automatically toggle exportLogs for this event, since it is very likely app settings can not be accessed - await initDebugLogHandler(); - FlutterLogs.exportLogs(); + // Automatically export logs for this event, since app settings may be inaccessible. + await LoggerWrapper.exportLogs(); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( diff --git a/lib/tools/debug_log_handler.dart b/lib/tools/debug_log_handler.dart deleted file mode 100644 index 31b2d1ac..00000000 --- a/lib/tools/debug_log_handler.dart +++ /dev/null @@ -1,54 +0,0 @@ -import 'dart:io'; - -import 'package:flutter_logs/flutter_logs.dart'; -import 'package:path_provider/path_provider.dart'; -import 'package:peercoin/tools/logger_wrapper.dart'; -import 'package:share_plus/share_plus.dart'; - -Future initDebugLogHandler() async { - FlutterLogs.channel.setMethodCallHandler((call) async { - if (call.method == 'logsExported') { - var zipName = call.arguments.toString(); - Directory? externalDirectory; - - if (Platform.isIOS) { - externalDirectory = await getApplicationDocumentsDirectory(); - } else { - externalDirectory = await getExternalStorageDirectory(); - } - - LoggerWrapper.logInfo( - 'AppSettingsScreen', - 'found', - 'External Storage:$externalDirectory', - ); - - var file = File('${externalDirectory!.path}/$zipName'); - - LoggerWrapper.logInfo( - 'AppSettingsScreen', - 'path', - 'Path: \n${file.path}', - ); - - if (file.existsSync()) { - LoggerWrapper.logInfo( - 'AppSettingsScreen', - 'existsSync', - 'Logs zip found, opening Share overlay', - ); - await Share.shareXFiles( - [ - XFile(file.path), - ], - ); - } else { - LoggerWrapper.logError( - 'AppSettingsScreen', - 'existsSync', - 'File not found in storage.', - ); - } - } - }); -} diff --git a/lib/tools/logger_wrapper.dart b/lib/tools/logger_wrapper.dart index fc896cb7..126df649 100644 --- a/lib/tools/logger_wrapper.dart +++ b/lib/tools/logger_wrapper.dart @@ -3,17 +3,75 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; -import 'package:flutter_logs/flutter_logs.dart'; +import 'package:logger/logger.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:share_plus/share_plus.dart'; class LoggerWrapper { - static void logInfo(String tag, String subTag, String logMessage) { - if (kIsWeb) { - print('$tag $subTag $logMessage'); - return; + static final MemoryOutput _memoryOutput = MemoryOutput( + bufferSize: 3000, + secondOutput: ConsoleOutput(), + ); + + static final Logger _logger = Logger( + filter: ProductionFilter(), + printer: PrettyPrinter( + methodCount: 0, + errorMethodCount: 5, + lineLength: 120, + colors: false, + printEmojis: false, + dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart, + ), + output: _memoryOutput, + ); + + static bool _initialized = false; + + static Future initLogs() async { + if (kIsWeb || Platform.environment.containsKey('FLUTTER_TEST')) return; + if (_initialized) return; + + await _logger.init; + _initialized = true; + } + + static Future exportLogs() async { + if (kIsWeb || Platform.environment.containsKey('FLUTTER_TEST')) return; + + try { + final lines = []; + for (final event in _memoryOutput.buffer) { + lines.addAll(event.lines); + } + + if (lines.isEmpty) { + _logger.w('[LoggerWrapper][exportLogs] No logs available to export.'); + return; + } + + final tempDirectory = await getTemporaryDirectory(); + final timestamp = DateTime.now().toIso8601String().replaceAll(':', '-'); + final file = File('${tempDirectory.path}/peercoin_logs_$timestamp.txt'); + + await file.writeAsString('${lines.join('\n')}\n'); + await Share.shareXFiles( + [ + XFile(file.path), + ], + ); + } catch (e, stackTrace) { + _logger.e( + '[LoggerWrapper][exportLogs] Failed to export logs.', + error: e, + stackTrace: stackTrace, + ); } - if (Platform.environment.containsKey('FLUTTER_TEST')) return; + } - FlutterLogs.logInfo( + static void logInfo(String tag, String subTag, String logMessage) { + _log( + Level.info, tag, subTag, logMessage, @@ -21,13 +79,8 @@ class LoggerWrapper { } static void logError(String tag, String subTag, String logMessage) { - if (kIsWeb) { - print('$tag $subTag $logMessage'); - return; - } - if (Platform.environment.containsKey('FLUTTER_TEST')) return; - - FlutterLogs.logError( + _log( + Level.error, tag, subTag, logMessage, @@ -35,16 +88,24 @@ class LoggerWrapper { } static void logWarn(String tag, String subTag, String logMessage) { + _log( + Level.warning, + tag, + subTag, + logMessage, + ); + } + + static void _log(Level level, String tag, String subTag, String message) { if (kIsWeb) { - print('$tag $subTag $logMessage'); + print('[$tag][$subTag] $message'); return; } if (Platform.environment.containsKey('FLUTTER_TEST')) return; - FlutterLogs.logWarn( - tag, - subTag, - logMessage, + _logger.log( + level, + '[$tag][$subTag] $message', ); } } diff --git a/pubspec.lock b/pubspec.lock index 83207401..48a90932 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -504,15 +504,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_logs: - dependency: "direct main" - description: - path: "." - ref: HEAD - resolved-ref: "42949f2d7b4f5d8f5d7771d40d5497517da0a884" - url: "https://github.com/ced1check/flutter_logs" - source: git - version: "2.2.1" flutter_markdown: dependency: "direct main" description: @@ -901,6 +892,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.11" + logger: + dependency: "direct main" + description: + name: logger + sha256: "25aee487596a6257655a1e091ec2ae66bc30e7af663592cc3a27e6591e05035c" + url: "https://pub.dev" + source: hosted + version: "2.7.0" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index f749b899..bc2c232b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,11 +40,7 @@ dependencies: collection: ^1.15.0-nullsafety.4 flutter_markdown: ^0.6.6 background_fetch: ^1.3.8 - flutter_logs: - git: - url: https://github.com/ced1check/flutter_logs - refh: master - # TODO move back once https://github.com/umair13adil/flutter_logs/issues/69 or 73 is fixed + logger: ^2.7.0 auto_size_text: ^3.0.0 camera: ^0.10.0+4 path_provider: ^2.0.11