Skip to content

Commit

Permalink
Disabled logging to file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Komodo5197 committed Jan 26, 2024
1 parent e9873ee commit 69ff2c6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
6 changes: 5 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void main() async {
// If the app has failed, this is set to true. If true, we don't attempt to run the main app since the error app has started.
bool hasFailed = false;
try {
setupLogging();
await setupLogging();
await setupHive();
_migrateDownloadLocations();
_migrateSortOptions();
Expand Down Expand Up @@ -434,6 +434,10 @@ class ErrorScreen extends StatelessWidget {
AppLocalizations.of(context)!.startupError(error.toString()),
),
),
/*bottomNavigationBar: const Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [ShareLogsButton(), CopyLogsButton()],
),*/
);
}
}
47 changes: 26 additions & 21 deletions lib/services/finamp_logs_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ class FinampLogsHelper {
final List<LogRecord> logs = [];
IOSink? _logFileWriter;

FinampLogsHelper() {
Future.sync(() async {
WidgetsFlutterBinding.ensureInitialized();
final basePath = await getApplicationDocumentsDirectory();
final logFile = File(path_helper.join(basePath.path, "finamp-logs.txt"));
if (logFile.existsSync() && logFile.lengthSync() >= 1024 * 1024 * 10) {
logFile
.renameSync(path_helper.join(basePath.path, "finamp-logs-old.txt"));
}
_logFileWriter = logFile.openWrite(mode: FileMode.writeOnlyAppend);
});
// Logging to a file is currently disabled.
Future<void> openLog() async {
WidgetsFlutterBinding.ensureInitialized();
final basePath = await getApplicationDocumentsDirectory();
final logFile = File(path_helper.join(basePath.path, "finamp-logs.txt"));
if (logFile.existsSync() && logFile.lengthSync() >= 1024 * 1024 * 10) {
logFile
.renameSync(path_helper.join(basePath.path, "finamp-logs-old.txt"));
}
_logFileWriter = logFile.openWrite(mode: FileMode.writeOnlyAppend);
}

void addLog(LogRecord log) {
logs.add(log);
if (_logFileWriter != null) {
// This fails if we log an event before setting up userHelper
var message = log.censoredMessage;
if (log.stackTrace == null) {
// Truncate long messages from chopper, but leave long stack traces
Expand Down Expand Up @@ -64,16 +64,21 @@ class FinampLogsHelper {
final tempFile = File(path_helper.join(tempDir.path, "finamp-logs.txt"));
tempFile.createSync();

final basePath = await getApplicationDocumentsDirectory();
var oldLogs = File(path_helper.join(basePath.path, "finamp-logs-old.txt"));
var newLogs = File(path_helper.join(basePath.path, "finamp-logs.txt"));
if (oldLogs.existsSync()) {
await tempFile.writeAsBytes(await oldLogs.readAsBytes(),
mode: FileMode.writeOnly);
}
if (newLogs.existsSync()) {
await tempFile.writeAsBytes(await newLogs.readAsBytes(),
mode: FileMode.writeOnlyAppend);
if (_logFileWriter != null) {
final basePath = await getApplicationDocumentsDirectory();
var oldLogs =
File(path_helper.join(basePath.path, "finamp-logs-old.txt"));
var newLogs = File(path_helper.join(basePath.path, "finamp-logs.txt"));
if (oldLogs.existsSync()) {
await tempFile.writeAsBytes(await oldLogs.readAsBytes(),
mode: FileMode.writeOnly);
}
if (newLogs.existsSync()) {
await tempFile.writeAsBytes(await newLogs.readAsBytes(),
mode: FileMode.writeOnlyAppend);
}
} else {
await tempFile.writeAsString(getSanitisedLogs());
}

final xFile = XFile(tempFile.path, mimeType: "text/plain");
Expand Down
4 changes: 2 additions & 2 deletions lib/setup_logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import 'package:logging/logging.dart';

import 'services/finamp_logs_helper.dart';

void setupLogging() {
Future<void> setupLogging() async {
GetIt.instance.registerSingleton(FinampLogsHelper());
//await GetIt.instance<FinampLogsHelper>().openLog();
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((event) {
final finampLogsHelper = GetIt.instance<FinampLogsHelper>();
Expand All @@ -18,7 +19,6 @@ void setupLogging() {
if (kDebugMode &&
event.loggerName != "Flutter" &&
event.stackTrace != null) {
// ignore: avoid_print
debugPrintStack(stackTrace: event.stackTrace);
}
finampLogsHelper.addLog(event);
Expand Down

0 comments on commit 69ff2c6

Please sign in to comment.