Skip to content

Commit

Permalink
add logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmajh committed Nov 14, 2023
1 parent a05f88c commit 2d24f98
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
13 changes: 13 additions & 0 deletions lib/bin/logger_web.dart
Original file line number Diff line number Diff line change
@@ -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
}
}
}
25 changes: 25 additions & 0 deletions lib/bin/logger_windows.dart
Original file line number Diff line number Diff line change
@@ -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
}
}
}
59 changes: 52 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -27,8 +30,55 @@ import 'routes/route.dart';

MyDatabase? database;
final navigatorKey = GlobalKey<NavigatorState>();
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');
Expand All @@ -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();
Expand Down Expand Up @@ -128,7 +173,7 @@ class MyApp extends StatelessWidget {

Future<void> 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(
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2d24f98

Please sign in to comment.