From 0e5c7b9f0bf83c70fee901559ef9d40fa3e62750 Mon Sep 17 00:00:00 2001 From: Paul Kepinski Date: Thu, 7 Nov 2024 20:45:28 +0100 Subject: [PATCH] Redirect isolate Log to main thread (#30) * Redirect isolate Log to main thread * Use specific logger --- packages/sane/lib/src/extensions.dart | 13 +++++++++ packages/sane/lib/src/sane_isolate.dart | 37 +++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/sane/lib/src/extensions.dart b/packages/sane/lib/src/extensions.dart index 85bae1f..78d9c8d 100644 --- a/packages/sane/lib/src/extensions.dart +++ b/packages/sane/lib/src/extensions.dart @@ -1,3 +1,4 @@ +import 'package:logging/logging.dart'; import 'package:sane/src/bindings.g.dart'; import 'package:sane/src/exceptions.dart'; @@ -10,3 +11,15 @@ extension SaneStatusExtension on SANE_Status { } } } + +extension LoggerExtension on Logger { + void redirect(LogRecord record) { + log( + record.level, + record.message, + record.error, + record.stackTrace, + record.zone, + ); + } +} diff --git a/packages/sane/lib/src/sane_isolate.dart b/packages/sane/lib/src/sane_isolate.dart index e666c59..e27d853 100644 --- a/packages/sane/lib/src/sane_isolate.dart +++ b/packages/sane/lib/src/sane_isolate.dart @@ -1,7 +1,10 @@ +import 'dart:async'; import 'dart:isolate'; import 'dart:typed_data'; +import 'package:logging/logging.dart'; import 'package:sane/sane.dart'; +import 'package:sane/src/extensions.dart'; import 'package:sane/src/isolate_messages/cancel.dart'; import 'package:sane/src/isolate_messages/close.dart'; import 'package:sane/src/isolate_messages/control_button_option.dart'; @@ -19,6 +22,8 @@ import 'package:sane/src/isolate_messages/read.dart'; import 'package:sane/src/isolate_messages/set_io_mode.dart'; import 'package:sane/src/isolate_messages/start.dart'; +final _logger = Logger('sane.isolate'); + class SaneIsolate implements Sane { SaneIsolate({ required Sane sane, @@ -38,7 +43,16 @@ class SaneIsolate implements Sane { sane: _sane, ), ); - _sendPort = await receivePort.first as SendPort; + + final sendPortCompleter = Completer(); + receivePort.listen((message) { + if (message is _SendPortMessage) { + sendPortCompleter.complete(message.sendPort); + } else if (message is _LogRecordMessage) { + _logger.redirect(message.record); + } + }); + _sendPort = await sendPortCompleter.future; } void kill() { @@ -292,7 +306,16 @@ class _IsolateEntryPointArgs { void _isolateEntryPoint(_IsolateEntryPointArgs args) { final isolateReceivePort = ReceivePort(); - args.mainSendPort.send(isolateReceivePort.sendPort); + args.mainSendPort.send( + _SendPortMessage(isolateReceivePort.sendPort), + ); + + Logger.root.level = Level.ALL; + Logger.root.onRecord.listen((record) { + args.mainSendPort.send( + _LogRecordMessage(record), + ); + }); final sane = args.sane; isolateReceivePort.cast<_IsolateMessageEnvelope>().listen((envelope) async { @@ -320,3 +343,13 @@ class _IsolateMessageEnvelope { final SendPort replyPort; final IsolateMessage message; } + +class _SendPortMessage { + _SendPortMessage(this.sendPort); + final SendPort sendPort; +} + +class _LogRecordMessage { + _LogRecordMessage(this.record); + final LogRecord record; +}