Skip to content

Commit 9643952

Browse files
committed
Refactor StdoutEventHandler buffering logic and null handling
1 parent 4452283 commit 9643952

1 file changed

Lines changed: 67 additions & 50 deletions

File tree

packages/devtools_app/lib/src/screens/logging/logging_controller.dart

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
// Copyright 2019 The Flutter Authors
24
// Use of this source code is governed by a BSD-style license that can be
35
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
@@ -849,55 +851,13 @@ class StdoutEventHandler {
849851
final String name;
850852
final bool isError;
851853

852-
LogData? buffer;
853-
Timer? timer;
854+
LogData? _buffer;
855+
Timer? _timer;
854856

855857
void handle(Event e) {
856858
final message = decodeBase64(e.bytes!);
857859

858-
if (buffer != null) {
859-
timer?.cancel();
860-
861-
if (message == '\n') {
862-
loggingController.log(
863-
LogData(
864-
buffer!.kind,
865-
buffer!.details! + message,
866-
buffer!.timestamp,
867-
summary: buffer!.summary! + message,
868-
isError: buffer!.isError,
869-
isolateRef: e.isolateRef,
870-
),
871-
);
872-
buffer = null;
873-
return;
874-
}
875-
876-
// If the buffered message ends with a newline, the next message is a
877-
// continuation of the same print statement (e.g. debugPrint('line1\nline2')
878-
// is sent by the VM as two events: 'line1\n' and 'line2'). Combine them
879-
// into a single log entry.
880-
// See: https://github.com/flutter/devtools/issues/9557
881-
if (buffer!.details!.endsWith('\n')) {
882-
final combined = LogData(
883-
buffer!.kind,
884-
buffer!.details! + message,
885-
buffer!.timestamp,
886-
summary: buffer!.summary,
887-
isError: buffer!.isError,
888-
isolateRef: e.isolateRef,
889-
);
890-
buffer = combined;
891-
timer = Timer(const Duration(milliseconds: 1), () {
892-
loggingController.log(buffer!);
893-
buffer = null;
894-
});
895-
return;
896-
}
897-
898-
loggingController.log(buffer!);
899-
buffer = null;
900-
}
860+
if (_handleBufferedMessage(message, e)) return;
901861

902862
const maxLength = 200;
903863

@@ -918,13 +878,70 @@ class StdoutEventHandler {
918878
if (message == '\n') {
919879
loggingController.log(data);
920880
} else {
921-
buffer = data;
922-
timer = Timer(const Duration(milliseconds: 1), () {
923-
loggingController.log(buffer!);
924-
buffer = null;
925-
});
881+
_setBuffer(data);
926882
}
927883
}
884+
885+
bool _handleBufferedMessage(String message, Event e) {
886+
if (_buffer case final currentBuffer?) {
887+
_timer?.cancel();
888+
889+
if (message == '\n') {
890+
loggingController.log(
891+
LogData(
892+
currentBuffer.kind,
893+
currentBuffer.details! + message,
894+
currentBuffer.timestamp,
895+
summary: currentBuffer.summary! + message,
896+
isError: currentBuffer.isError,
897+
isolateRef: e.isolateRef,
898+
),
899+
);
900+
_buffer = null;
901+
return true;
902+
}
903+
904+
// If the buffered message ends with a newline, the next message is a
905+
// continuation of the same print statement (e.g. debugPrint('line1\nline2')
906+
// is sent by the VM as two events: 'line1\n' and 'line2'). Combine them
907+
// into a single log entry.
908+
// See: https://github.com/flutter/devtools/issues/9557
909+
if (currentBuffer.details!.endsWith('\n')) {
910+
_setBuffer(
911+
LogData(
912+
currentBuffer.kind,
913+
currentBuffer.details! + message,
914+
currentBuffer.timestamp,
915+
summary: currentBuffer.summary,
916+
isError: currentBuffer.isError,
917+
isolateRef: e.isolateRef,
918+
),
919+
);
920+
return true;
921+
}
922+
923+
loggingController.log(currentBuffer);
924+
_buffer = null;
925+
}
926+
return false;
927+
}
928+
929+
void _setBuffer(LogData data) {
930+
_buffer = data;
931+
_timer?.cancel();
932+
_timer = Timer(const Duration(milliseconds: 1), () {
933+
if (_buffer case final currentBuffer?) {
934+
loggingController.log(currentBuffer);
935+
_buffer = null;
936+
}
937+
});
938+
}
939+
940+
@visibleForTesting
941+
LogData? get buffer => _buffer;
942+
943+
@visibleForTesting
944+
Timer? get timer => _timer;
928945
}
929946

930947
bool _isNotNull(InstanceRef? serviceRef) {

0 commit comments

Comments
 (0)