diff --git a/lib/bin/logger_web.dart b/lib/bin/logger_web.dart new file mode 100644 index 0000000..0be5f76 --- /dev/null +++ b/lib/bin/logger_web.dart @@ -0,0 +1,13 @@ +import 'package:flutter/foundation.dart'; +import 'package:logger/logger.dart'; + +class AppConsoleOutput extends LogOutput { + AppConsoleOutput(); + + @override + void output(OutputEvent event) async { + for (var line in event.lines) { + debugPrint(line); //print to console as well + } + } +} diff --git a/lib/bin/logger_windows.dart b/lib/bin/logger_windows.dart new file mode 100644 index 0000000..33e0aba --- /dev/null +++ b/lib/bin/logger_windows.dart @@ -0,0 +1,25 @@ +import 'package:flutter/foundation.dart'; +import 'package:logger/logger.dart'; +import 'package:path/path.dart' as p; +import 'dart:io'; + +import 'package:path_provider/path_provider.dart'; + +class AppFileOutput extends LogOutput { + AppFileOutput(); + + late File file; + + @override + void output(OutputEvent event) async { + final appDir = await getApplicationDocumentsDirectory(); + final path = p.join(appDir.path, 'ReliabilityApp', 'iko_reliability.log'); + late File file = File(path); + + for (var line in event.lines) { + await file.writeAsString("${line.toString()}\n", + mode: FileMode.writeOnlyAppend); + debugPrint(line); //print to console as well + } + } +} diff --git a/lib/main.dart b/lib/main.dart index b418abe..f309492 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,12 +1,15 @@ -import 'package:flutter/foundation.dart' show kIsWeb; +import 'package:flutter/foundation.dart' show PlatformDispatcher, kIsWeb; import 'dart:io' show Platform; import 'package:auto_route/auto_route.dart'; import 'package:flutter/material.dart'; import 'package:iko_reliability_flutter/bin/consts.dart'; import 'package:hive_flutter/hive_flutter.dart'; +import 'package:iko_reliability_flutter/bin/logger_web.dart'; +import 'package:iko_reliability_flutter/bin/logger_windows.dart'; import 'package:iko_reliability_flutter/settings/settings_notifier.dart'; import 'package:iko_reliability_flutter/settings/theme_manager.dart'; +import 'package:logger/logger.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:flutter_window_close/flutter_window_close.dart'; @@ -27,8 +30,55 @@ import 'routes/route.dart'; MyDatabase? database; final navigatorKey = GlobalKey(); +Logger? logger; void main() async { + WidgetsFlutterBinding.ensureInitialized(); + if (kIsWeb) { + logger = Logger( + filter: ProductionFilter(), + printer: PrettyPrinter( + methodCount: 1, + errorMethodCount: 6, + lineLength: 80, + colors: false, + printEmojis: false, + printTime: true, + ), + output: AppConsoleOutput(), + ); + } else if (Platform.isWindows) { + setWindowMinSize(const Size(640, 360)); + logger = Logger( + filter: ProductionFilter(), + printer: PrettyPrinter( + methodCount: 1, + errorMethodCount: 6, + lineLength: 80, + colors: false, + printEmojis: false, + printTime: true, + ), + output: AppFileOutput(), + ); + } + // put flutter errors in the logger + FlutterError.onError = (details) { + FlutterError.presentError(details); + logger?.f( + details.exceptionAsString(), + error: (details.toDiagnosticsNode().toStringDeep()), + stackTrace: details.stack, + ); + }; + PlatformDispatcher.instance.onError = (exception, stackTrace) { + logger?.f( + "Unhandled Exception", + error: exception, + stackTrace: stackTrace, + ); + return false; + }; await Hive.initFlutter(); await Hive.openBox('pmNumber'); await Hive.openBox('jpNumber'); @@ -40,11 +90,6 @@ void main() async { box = Hive.box('routeNumber'); box.clear(); database = MyDatabase(); - WidgetsFlutterBinding.ensureInitialized(); - if (kIsWeb) { - } else if (Platform.isWindows) { - setWindowMinSize(const Size(640, 360)); - } SettingsNotifier settingsNotifier = SettingsNotifier(); SelectedSiteNotifier selectedSiteNotifier = SelectedSiteNotifier(); ThemeManager themeManager = ThemeManager(); @@ -128,7 +173,7 @@ class MyApp extends StatelessWidget { Future loadAppSettings(SelectedSiteNotifier selectedSiteNotifier, ThemeManager themeManager, SettingsNotifier settingsNotifier) async { - debugPrint('running load app'); + logger?.d('Loading App Settings Before Start'); final settings = await database!.getSettings(); final selectedSite = settings .firstWhere( diff --git a/pubspec.lock b/pubspec.lock index f70e636..71ac2f3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -536,6 +536,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.0+4" + logger: + dependency: "direct main" + description: + name: logger + sha256: "6bbb9d6f7056729537a4309bda2e74e18e5d9f14302489cc1e93f33b3fe32cac" + url: "https://pub.dev" + source: hosted + version: "2.0.2+1" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8fc34f1..9eb61a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: intl: ^0.18.1 file_selector: ^1.0.1 collection: ^1.17.2 + logger: ^2.0.2+1 dev_dependencies: flutter_test: