-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67135c2
commit d58c1e4
Showing
6 changed files
with
288 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters