Skip to content

Commit

Permalink
add provider, get_it, logger
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder2 committed Nov 13, 2021
1 parent 67135c2 commit d58c1e4
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import 'dart:async';

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/material.dart';
import 'package:flutter_acrylic/flutter_acrylic.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:odin/pages/home_page.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/services/logger.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load();
await setupLocator();
await Window.initialize();
await Window.setEffect(
effect: WindowEffect.acrylic,
color: const Color(0xCC222222),
);
runApp(const MyApp());
runZonedGuarded(() {
runApp(const MyApp());
}, (obj, stacktrace) {
logger.e(obj, obj, stacktrace);
});
doWhenWindowReady(() {
final win = appWindow;
const initialSize = Size(412, 192);
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/material.dart';
import 'package:odin/services/data_service.dart';
import 'package:odin/services/locator.dart';
import 'package:odin/widgets/window_top_bar.dart';

const backgroundStartColor = Color(0x55121212);
Expand All @@ -19,7 +20,7 @@ class _HomePageState extends State<HomePage> {
bool _dragging = false;
bool _loading = false;
String? _fileLink;
final _ds = DataService();
final _ds = locator<DataService>();

@override
Widget build(BuildContext context) {
Expand Down
14 changes: 14 additions & 0 deletions lib/services/locator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:get_it/get_it.dart';
import 'package:odin/services/data_service.dart';

import 'logger.dart';

GetIt locator = GetIt.instance;

Future<void> setupLocator() async {
Stopwatch stopwatch = Stopwatch()..start();
// locator.registerFactory<CurrentDataNotifier>(() => CurrentDataNotifier());
locator.registerLazySingleton<DataService>(() => DataService());
logger.d('Locator setup in ${stopwatch.elapsed}');
stopwatch.stop();
}
166 changes: 166 additions & 0 deletions lib/services/logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import 'dart:developer' as developer;
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

final printer = LogOutputPrinter();
final logger = Logger(
level: Level.debug,
printer: printer,
filter: kDebugMode
? PassThroughFilter()
: PassThroughFilter(), //!ProductionFilter(), Add before final release
);

class PassThroughFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) {
return true;
}
}

class LogOutputPrinter extends PrettyPrinter {
late String _logFolderPath;
RandomAccessFile? _logFile;

LogOutputPrinter() {
if (Platform.isWindows) {
getTemporaryDirectory().then((cacheDir) async {
if (!cacheDir.existsSync()) {
getDownloadsDirectory().then((cDir) async {
_logFolderPath = join(cDir?.path ?? '', "logs");
try {
await Directory(_logFolderPath).create();
} catch (e) {
// Ignore if it already exists
}
await setLogCapture(true);
});
} else {
_logFolderPath = join(cacheDir.path, "logs");
try {
await Directory(_logFolderPath).create();
} catch (e) {
// Ignore if it already exists
}
await setLogCapture(true);
}
developer.log(cacheDir.path);
});
}
}

@override
List<String> log(LogEvent event) {
final logMsg = event.message;
final logLvl = event.level;
final logStrace = event.stackTrace;
final logError = event.error;
final color = PrettyPrinter.levelColors[logLvl];
final prefix = SimplePrinter.levelPrefixes[logLvl];
final str =
"---------------------------------------------------------------------------\nLEVEL : $logLvl\nMESSAGE : ${DateTime.now().toString().substring(11, 22)} :: $logMsg\nERROR : $logError\nSTACKTRACE : $logStrace";
Future.delayed(const Duration(seconds: 1))
.then((value) => _logFile?.writeStringSync('$str\n'));
final timeStr = getTime().substring(0, 12);
if (logStrace != null) {
// print(color!('$timeStr $prefix - $logMsg \n$logStrace'));
developer.log(
color!('$logMsg \n$logError'),
name: "$timeStr :: ${prefix!.replaceAll("[", "").replaceAll("]", "")}",
stackTrace: logStrace,
level: 2000,
);
} else {
// print(color!('$timeStr $prefix - $logMsg'));
developer.log(
color!('$logMsg'),
name: "$timeStr :: ${prefix!.replaceAll("[", "").replaceAll("]", "")}",
);
}
return [];
}

Future<void> setLogCapture(bool state) async {
if (state) {
final today = DateTime.now().toString().substring(0, 10);
final logFilePath = join(_logFolderPath, '$today.txt');
_logFile = await File(logFilePath).open(mode: FileMode.append);
} else {
if (_logFile != null) {
await _logFile!.close();
}
_logFile = null;
}
}

String filePathForDate(DateTime dt) {
final date = dt.toString().substring(0, 10);
return join(_logFolderPath, '$date.txt');
}

String logsFolderPath() {
return _logFolderPath;
}

List<String> filePathsForDates(int n) {
final DateTime today = DateTime.now();
final l = <String>[];
for (var i = 0; i < n; i++) {
final String fp = filePathForDate(
today.subtract(
Duration(days: i),
),
);
if (File(fp).existsSync()) {
l.add(fp);
} else {
logger.i("Log file $fp not found");
}
}

return l;
}

Iterable<String> fetchLogs() sync* {
final today = DateTime.now();
for (final msg in fetchLogsForDate(today)) {
yield msg;
}
}

Iterable<String> fetchLogsForDate(DateTime date) sync* {
final file = File(filePathForDate(date));
if (!file.existsSync()) {
logger.i("No log file for $date, path = ${file.path}");
return;
}

final str = file.readAsStringSync();
for (final line in str.split("\n")) {
yield line;
}
}
}

// Future<String> zipLogs() async {
// logger.v("Zipping Logs");
// final sourceDir = Directory(printer.logsFolderPath());
// final files = printer.filePathsForDates(2).map((e) => File(e)).toList();
// final zipFile = File(join(printer.logsFolderPath(), 'logs.zip'));
// try {
// logger.v("Zipping Started");
// await ZipFile.createFromFiles(
// sourceDir: sourceDir, files: files, zipFile: zipFile);
// logger.v("Zipping Finished Successfully");
// logger.v("Renaming Zip File");
// await zipFile.rename(join(printer.logsFolderPath(), 'logs'));
// logger.v("Renaming Done");
// } catch (e, strace) {
// logger.e(e, e, strace);
// }
// return join(printer.logsFolderPath(), 'logs');
// }
93 changes: 92 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.2"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -137,6 +144,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
get_it:
dependency: "direct main"
description:
name: get_it
url: "https://pub.dartlang.org"
source: hosted
version: "7.2.0"
github:
dependency: "direct main"
description:
Expand Down Expand Up @@ -179,6 +193,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
logger:
dependency: "direct main"
description:
name: logger
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
matcher:
dependency: transitive
description:
Expand All @@ -193,20 +214,83 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
nested:
dependency: transitive
description:
name: nested
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
path:
dependency: "direct main"
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.2"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
provider:
dependency: "direct main"
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -275,6 +359,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
sdks:
dart: ">=2.14.0 <3.0.0"
flutter: ">=1.22.0"
flutter: ">=2.5.0"
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ dependencies:
path: ^1.8.0
flutter_dotenv: ^5.0.2
intl: ^0.17.0
provider: ^6.0.1
get_it: ^7.2.0
logger: ^1.1.0
path_provider: ^2.0.6

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit d58c1e4

Please sign in to comment.