Skip to content

Commit 0f4c960

Browse files
authored
Merge pull request #7 from Goddchen/feature/rework-logging-integration
feat: rework logging integration
2 parents fac010d + 024ef20 commit 0f4c960

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
@@ -22,8 +22,10 @@ import 'package:dart_rfb/src/protocol/server_init_message.dart';
2222
import 'package:fpdart/fpdart.dart';
2323
import 'package:logging/logging.dart';
2424

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

2830
final StreamController<RemoteFrameBufferClientUpdate>
2931
_updateStreamController =
@@ -33,10 +35,6 @@ class RemoteFrameBufferClient {
3335

3436
Option<RawSocket> _socket = none();
3537

36-
bool _loggingEnabled = false;
37-
38-
Option<StreamSubscription<LogRecord>> _loggingSubscription = none();
39-
4038
bool _readLoopRunning = false;
4139

4240
Option<String> _password = none();
@@ -45,35 +43,9 @@ class RemoteFrameBufferClient {
4543

4644
Option<RemoteFrameBufferSecurityType> _selectedSecurityType = none();
4745

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

65-
/// Wether logging messages should be printed.
66-
bool get loggingEnabled => _loggingEnabled;
67-
68-
set loggingEnabled(final bool enabled) {
69-
_loggingEnabled = enabled;
70-
if (enabled) {
71-
Logger.root.level = Level.ALL;
72-
} else {
73-
Logger.root.level = Level.OFF;
74-
}
75-
}
76-
7749
/// A [Stream] that will give access to all incoming framebuffer updates.
7850
Stream<RemoteFrameBufferClientUpdate> get updateStream =>
7951
_updateStreamController.stream;
@@ -82,11 +54,6 @@ class RemoteFrameBufferClient {
8254
Future<void> close() async {
8355
_readLoopRunning = false;
8456
await _updateStreamController.close();
85-
await _loggingSubscription.match(
86-
() {},
87-
(final StreamSubscription<LogRecord> subscription) =>
88-
subscription.cancel(),
89-
);
9057
}
9158

9259
/// Connect to [hostname] on [port] and perform the protocol handshake,
@@ -133,7 +100,7 @@ class RemoteFrameBufferClient {
133100
x: 0,
134101
y: 0,
135102
);
136-
_logger.log(Level.INFO, '> $requestMessage');
103+
logger.log(Level.INFO, '> $requestMessage');
137104
socket.write(
138105
requestMessage.toBytes().asUint8List(),
139106
);
@@ -197,7 +164,7 @@ class RemoteFrameBufferClient {
197164
'Error reading incoming message type',
198165
),
199166
);
200-
_logger.log(Level.INFO, '< messageType: $messageType');
167+
logger.log(Level.INFO, '< messageType: $messageType');
201168
switch (messageType) {
202169
case 0:
203170
while (socket.available() < 1) {
@@ -213,15 +180,15 @@ class RemoteFrameBufferClient {
213180
.readFromSocket(config: config, socket: socket)
214181
.run())
215182
.match(
216-
(final Object error) => _logger.log(
183+
(final Object error) => logger.log(
217184
Level.INFO,
218185
'Error reading and handling update message: $error',
219186
),
220187
(
221188
final RemoteFrameBufferFrameBufferUpdateMessage
222189
updateMessage,
223190
) {
224-
_logger.log(
191+
logger.log(
225192
Level.INFO,
226193
'< ${updateMessage.rectangles.length} update rectangles',
227194
);
@@ -296,7 +263,7 @@ class RemoteFrameBufferClient {
296263
);
297264
break;
298265
default:
299-
_logger.log(
266+
logger.log(
300267
Level.INFO,
301268
'Receive unsupported message type: $messageType',
302269
);
@@ -328,7 +295,7 @@ class RemoteFrameBufferClient {
328295
() => throw Exception('Error reading security challenge'),
329296
),
330297
);
331-
_logger.info('< Security challenge');
298+
logger.info('< Security challenge');
332299
final ByteData encodedAndTruncatedPassword = ByteData.sublistView(
333300
Uint8List.fromList(ascii.encode(password).take(8).toList()),
334301
);
@@ -354,7 +321,7 @@ class RemoteFrameBufferClient {
354321
0,
355322
16,
356323
);
357-
_logger.info('> Security challenge response');
324+
logger.info('> Security challenge response');
358325
socket.write(
359326
response.buffer
360327
.asUint8List(response.offsetInBytes, response.lengthInBytes),
@@ -400,7 +367,7 @@ class RemoteFrameBufferClient {
400367
),
401368
),
402369
);
403-
_logger.log(Level.INFO, '< $protocolVersionHandshakeMessage');
370+
logger.log(Level.INFO, '< $protocolVersionHandshakeMessage');
404371
if (protocolVersionHandshakeMessage.version
405372
is RemoteFrameBufferProtocolVersionUnknown) {
406373
throw Exception(
@@ -426,7 +393,7 @@ class RemoteFrameBufferClient {
426393
final int numberOfSecurityTypes = optionOf(socket.read(1)).getOrElse(
427394
() => throw Exception('Error reading number of security types'),
428395
)[0];
429-
_logger.log(
396+
logger.log(
430397
Level.INFO,
431398
'< numberOfSecurityTypes=$numberOfSecurityTypes',
432399
);
@@ -473,7 +440,7 @@ class RemoteFrameBufferClient {
473440
),
474441
),
475442
);
476-
_logger.log(Level.INFO, '< $securityResultHandshakeMessage');
443+
logger.log(Level.INFO, '< $securityResultHandshakeMessage');
477444
if (_password.isNone() &&
478445
securityResultHandshakeMessage.securityTypes.notElem(
479446
const RemoteFrameBufferSecurityType.none(),
@@ -508,7 +475,7 @@ class RemoteFrameBufferClient {
508475
),
509476
),
510477
);
511-
_logger.info('< $securityType');
478+
logger.info('< $securityType');
512479

513480
if (_password.isNone() &&
514481
securityType != const RemoteFrameBufferSecurityType.none()) {
@@ -559,7 +526,7 @@ class RemoteFrameBufferClient {
559526
),
560527
),
561528
);
562-
_logger.log(Level.INFO, '< $securityResultHandshakeMessage');
529+
logger.log(Level.INFO, '< $securityResultHandshakeMessage');
563530
if (!securityResultHandshakeMessage.success) {
564531
while (socket.available() < 4) {
565532
await Future<void>.delayed(Constants.socketReadWaitDuration);
@@ -600,7 +567,7 @@ class RemoteFrameBufferClient {
600567
),
601568
),
602569
);
603-
_logger.log(Level.INFO, '< $securityResultHandshakeMessage');
570+
logger.log(Level.INFO, '< $securityResultHandshakeMessage');
604571
if (!securityResultHandshakeMessage.success) {
605572
throw Exception('Security handshake failed');
606573
}
@@ -648,7 +615,7 @@ class RemoteFrameBufferClient {
648615
}) =>
649616
TaskEither<Object, void>.tryCatch(
650617
() async {
651-
_logger.log(
618+
logger.log(
652619
Level.INFO,
653620
'> ${const RemoteFrameBufferClientInitMessage(sharedFlag: true)}',
654621
);
@@ -672,7 +639,7 @@ class RemoteFrameBufferClient {
672639
() => throw Exception('Error selecting protocol version'),
673640
),
674641
);
675-
_logger.log(Level.INFO, '> $message');
642+
logger.log(Level.INFO, '> $message');
676643
socket.write(message.toBytes().asUint8List());
677644
},
678645
(final Object error, final _) => error,
@@ -688,7 +655,7 @@ class RemoteFrameBufferClient {
688655
() => const RemoteFrameBufferSecurityType.none(),
689656
(final _) => const RemoteFrameBufferSecurityType.vncAuthentication(),
690657
);
691-
_logger.info('> $securityType');
658+
logger.info('> $securityType');
692659
_selectedSecurityType = some(securityType);
693660
socket.write(securityType.toBytes().asUint8List());
694661
},

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)