Skip to content

Commit 024ef20

Browse files
committed
feat: rework logging integration
1 parent d057160 commit 024ef20

File tree

4 files changed

+28
-60
lines changed

4 files changed

+28
-60
lines changed

example/dart_rfb_example.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'package:dart_rfb/dart_rfb.dart';
22

33
void main() async {
4-
final RemoteFrameBufferClient client = RemoteFrameBufferClient()
5-
..loggingEnabled = true;
4+
final RemoteFrameBufferClient client = RemoteFrameBufferClient();
65
await client.connect(hostname: '127.0.0.1');
76
await client.startReadLoop();
87
}

lib/src/client/remote_frame_buffer_client.dart

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import 'package:dart_rfb/src/protocol/server_init_message.dart';
2020
import 'package:fpdart/fpdart.dart';
2121
import 'package:logging/logging.dart';
2222

23+
/// A client that implements communication according to
24+
/// The Remote Framebuffer Protocol, aka RFC 6143, aka VNC).
2325
class RemoteFrameBufferClient {
24-
static final Logger _logger = Logger('RemoteFrameBufferClient');
26+
static final Logger logger = Logger('RemoteFrameBufferClient');
2527

2628
final StreamController<RemoteFrameBufferClientUpdate>
2729
_updateStreamController =
@@ -31,10 +33,6 @@ class RemoteFrameBufferClient {
3133

3234
Option<RawSocket> _socket = none();
3335

34-
bool _loggingEnabled = false;
35-
36-
Option<StreamSubscription<LogRecord>> _loggingSubscription = none();
37-
3836
bool _readLoopRunning = false;
3937

4038
Option<String> _password = none();
@@ -43,35 +41,9 @@ class RemoteFrameBufferClient {
4341

4442
Option<RemoteFrameBufferSecurityType> _selectedSecurityType = none();
4543

46-
/// A client that implements communication according to
47-
/// The Remote Framebuffer Protocol, aka RFC 6143, aka VNC).
48-
RemoteFrameBufferClient() {
49-
loggingEnabled = false;
50-
_loggingSubscription = some(
51-
Logger.root.onRecord.listen(
52-
(final LogRecord logRecord) {
53-
// ignore: avoid_print
54-
print('${logRecord.loggerName}: ${logRecord.message}');
55-
},
56-
),
57-
);
58-
}
59-
6044
/// The config used by the underlying session.
6145
Option<Config> get config => _config;
6246

63-
/// Wether logging messages should be printed.
64-
bool get loggingEnabled => _loggingEnabled;
65-
66-
set loggingEnabled(final bool enabled) {
67-
_loggingEnabled = enabled;
68-
if (enabled) {
69-
Logger.root.level = Level.ALL;
70-
} else {
71-
Logger.root.level = Level.OFF;
72-
}
73-
}
74-
7547
/// A [Stream] that will give access to all incoming framebuffer updates.
7648
Stream<RemoteFrameBufferClientUpdate> get updateStream =>
7749
_updateStreamController.stream;
@@ -80,11 +52,6 @@ class RemoteFrameBufferClient {
8052
Future<void> close() async {
8153
_readLoopRunning = false;
8254
await _updateStreamController.close();
83-
await _loggingSubscription.match(
84-
() {},
85-
(final StreamSubscription<LogRecord> subscription) =>
86-
subscription.cancel(),
87-
);
8855
}
8956

9057
/// Connect to [hostname] on [port] and perform the protocol handshake,
@@ -131,7 +98,7 @@ class RemoteFrameBufferClient {
13198
x: 0,
13299
y: 0,
133100
);
134-
_logger.log(Level.INFO, '> $requestMessage');
101+
logger.log(Level.INFO, '> $requestMessage');
135102
socket.write(
136103
requestMessage.toBytes().asUint8List(),
137104
);
@@ -171,7 +138,7 @@ class RemoteFrameBufferClient {
171138
'Error reading incoming message type',
172139
),
173140
);
174-
_logger.log(Level.INFO, '< messageType: $messageType');
141+
logger.log(Level.INFO, '< messageType: $messageType');
175142
switch (messageType) {
176143
case 0:
177144
while (socket.available() < 1) {
@@ -187,15 +154,15 @@ class RemoteFrameBufferClient {
187154
.readFromSocket(config: config, socket: socket)
188155
.run())
189156
.match(
190-
(final Object error) => _logger.log(
157+
(final Object error) => logger.log(
191158
Level.INFO,
192159
'Error reading and handling update message: $error',
193160
),
194161
(
195162
final RemoteFrameBufferFrameBufferUpdateMessage
196163
updateMessage,
197164
) {
198-
_logger.log(
165+
logger.log(
199166
Level.INFO,
200167
'< ${updateMessage.rectangles.length} update rectangles',
201168
);
@@ -270,7 +237,7 @@ class RemoteFrameBufferClient {
270237
);
271238
break;
272239
default:
273-
_logger.log(
240+
logger.log(
274241
Level.INFO,
275242
'Receive unsupported message type: $messageType',
276243
);
@@ -302,7 +269,7 @@ class RemoteFrameBufferClient {
302269
() => throw Exception('Error reading security challenge'),
303270
),
304271
);
305-
_logger.info('< Security challenge');
272+
logger.info('< Security challenge');
306273
final ByteData encodedAndTruncatedPassword = ByteData.sublistView(
307274
Uint8List.fromList(ascii.encode(password).take(8).toList()),
308275
);
@@ -328,7 +295,7 @@ class RemoteFrameBufferClient {
328295
0,
329296
16,
330297
);
331-
_logger.info('> Security challenge response');
298+
logger.info('> Security challenge response');
332299
socket.write(
333300
response.buffer
334301
.asUint8List(response.offsetInBytes, response.lengthInBytes),
@@ -374,7 +341,7 @@ class RemoteFrameBufferClient {
374341
),
375342
),
376343
);
377-
_logger.log(Level.INFO, '< $protocolVersionHandshakeMessage');
344+
logger.log(Level.INFO, '< $protocolVersionHandshakeMessage');
378345
if (protocolVersionHandshakeMessage.version
379346
is RemoteFrameBufferProtocolVersionUnknown) {
380347
throw Exception(
@@ -400,7 +367,7 @@ class RemoteFrameBufferClient {
400367
final int numberOfSecurityTypes = optionOf(socket.read(1)).getOrElse(
401368
() => throw Exception('Error reading number of security types'),
402369
)[0];
403-
_logger.log(
370+
logger.log(
404371
Level.INFO,
405372
'< numberOfSecurityTypes=$numberOfSecurityTypes',
406373
);
@@ -447,7 +414,7 @@ class RemoteFrameBufferClient {
447414
),
448415
),
449416
);
450-
_logger.log(Level.INFO, '< $securityResultHandshakeMessage');
417+
logger.log(Level.INFO, '< $securityResultHandshakeMessage');
451418
if (_password.isNone() &&
452419
securityResultHandshakeMessage.securityTypes.notElem(
453420
const RemoteFrameBufferSecurityType.none(),
@@ -482,7 +449,7 @@ class RemoteFrameBufferClient {
482449
),
483450
),
484451
);
485-
_logger.info('< $securityType');
452+
logger.info('< $securityType');
486453

487454
if (_password.isNone() &&
488455
securityType != const RemoteFrameBufferSecurityType.none()) {
@@ -533,7 +500,7 @@ class RemoteFrameBufferClient {
533500
),
534501
),
535502
);
536-
_logger.log(Level.INFO, '< $securityResultHandshakeMessage');
503+
logger.log(Level.INFO, '< $securityResultHandshakeMessage');
537504
if (!securityResultHandshakeMessage.success) {
538505
while (socket.available() < 4) {
539506
await Future<void>.delayed(Constants.socketReadWaitDuration);
@@ -574,7 +541,7 @@ class RemoteFrameBufferClient {
574541
),
575542
),
576543
);
577-
_logger.log(Level.INFO, '< $securityResultHandshakeMessage');
544+
logger.log(Level.INFO, '< $securityResultHandshakeMessage');
578545
if (!securityResultHandshakeMessage.success) {
579546
throw Exception('Security handshake failed');
580547
}
@@ -622,7 +589,7 @@ class RemoteFrameBufferClient {
622589
}) =>
623590
TaskEither<Object, void>.tryCatch(
624591
() async {
625-
_logger.log(
592+
logger.log(
626593
Level.INFO,
627594
'> ${const RemoteFrameBufferClientInitMessage(sharedFlag: true)}',
628595
);
@@ -646,7 +613,7 @@ class RemoteFrameBufferClient {
646613
() => throw Exception('Error selecting protocol version'),
647614
),
648615
);
649-
_logger.log(Level.INFO, '> $message');
616+
logger.log(Level.INFO, '> $message');
650617
socket.write(message.toBytes().asUint8List());
651618
},
652619
(final Object error, final _) => error,
@@ -662,7 +629,7 @@ class RemoteFrameBufferClient {
662629
() => const RemoteFrameBufferSecurityType.none(),
663630
(final _) => const RemoteFrameBufferSecurityType.vncAuthentication(),
664631
);
665-
_logger.info('> $securityType');
632+
logger.info('> $securityType');
666633
_selectedSecurityType = some(securityType);
667634
socket.write(securityType.toBytes().asUint8List());
668635
},

lib/src/protocol/server_init_message.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import 'dart:convert';
22
import 'dart:io';
33
import 'dart:typed_data';
44

5+
import 'package:dart_rfb/src/client/remote_frame_buffer_client.dart';
56
import 'package:dart_rfb/src/constants.dart';
67
import 'package:dart_rfb/src/protocol/pixel_format.dart';
78
import 'package:fpdart/fpdart.dart';
89
import 'package:freezed_annotation/freezed_annotation.dart';
9-
import 'package:logging/logging.dart';
1010

1111
part 'server_init_message.freezed.dart';
1212

@@ -16,8 +16,6 @@ part 'server_init_message.freezed.dart';
1616
@freezed
1717
class RemoteFrameBufferServerInitMessage
1818
with _$RemoteFrameBufferServerInitMessage {
19-
static final Logger _logger = Logger('RemoteFrameBufferServerInitMessage');
20-
2119
const factory RemoteFrameBufferServerInitMessage({
2220
required final int frameBufferHeightInPixels,
2321
required final int frameBufferWidthInPixels,
@@ -58,7 +56,7 @@ class RemoteFrameBufferServerInitMessage
5856
bytes: ByteData.sublistView(bytes, 4, 20),
5957
),
6058
);
61-
_logger.log(Level.INFO, '< $serverInitMessage');
59+
RemoteFrameBufferClient.logger.info('< $serverInitMessage');
6260
return serverInitMessage;
6361
},
6462
(final Object error, final _) => error,

test/client_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import 'package:dart_rfb/dart_rfb.dart';
2+
import 'package:logging/logging.dart';
23
import 'package:test/test.dart';
34

45
void main() {
56
test('Connect to VNC running on localhost', skip: true, () async {
6-
final RemoteFrameBufferClient client = RemoteFrameBufferClient()
7-
..loggingEnabled = true;
7+
Logger.root.onRecord.listen((final LogRecord logRecord) {
8+
// ignore: avoid_print
9+
print('${logRecord.loggerName}: ${logRecord.message}');
10+
});
11+
final RemoteFrameBufferClient client = RemoteFrameBufferClient();
812
await client.connect(
913
hostname: '127.0.0.1',
1014
password: 'password',

0 commit comments

Comments
 (0)