refactor: add back exceptions to file support

This commit is contained in:
Kingkor Roy Tirtho 2024-06-09 22:58:14 +06:00
parent f9087b63d5
commit de61d90938

View File

@ -1,16 +1,23 @@
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:spotube/utils/platform.dart';
class AppLogger {
static late final Logger log;
static late final File logFile;
static initialize(bool verbose) {
log = Logger(
level: kDebugMode || (verbose && kReleaseMode) ? Level.all : Level.info,
);
getLogsPath().then((value) => logFile = value);
}
static R? runZoned<R>(R Function() body) {
@ -43,11 +50,36 @@ class AppLogger {
);
}
static void reportError(
static Future<File> getLogsPath() async {
String dir = (await getApplicationDocumentsDirectory()).path;
if (kIsAndroid) {
dir = (await getExternalStorageDirectory())?.path ?? "";
}
if (kIsMacOS) {
dir = join((await getLibraryDirectory()).path, "Logs");
}
final file = File(join(dir, ".spotube_logs"));
if (!await file.exists()) {
await file.create(recursive: true);
}
return file;
}
static Future<void> reportError(
dynamic error, [
StackTrace? stackTrace,
message = "",
]) {
]) async {
log.e(message, error: error, stackTrace: stackTrace);
if (kReleaseMode) {
await logFile.writeAsString(
"[${DateTime.now()}]---------------------\n"
"$error\n$stackTrace\n"
"----------------------------------------\n",
mode: FileMode.writeOnlyAppend,
);
}
}
}