Skip to content

Commit

Permalink
Redirect isolate Log to main thread (#30)
Browse files Browse the repository at this point in the history
* Redirect isolate Log to main thread

* Use specific logger
  • Loading branch information
Jupi007 authored Nov 7, 2024
1 parent 36f77b5 commit 0e5c7b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/sane/lib/src/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:logging/logging.dart';
import 'package:sane/src/bindings.g.dart';
import 'package:sane/src/exceptions.dart';

Expand All @@ -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,
);
}
}
37 changes: 35 additions & 2 deletions packages/sane/lib/src/sane_isolate.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -38,7 +43,16 @@ class SaneIsolate implements Sane {
sane: _sane,
),
);
_sendPort = await receivePort.first as SendPort;

final sendPortCompleter = Completer<SendPort>();
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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

0 comments on commit 0e5c7b9

Please sign in to comment.