Skip to content

Commit 751539c

Browse files
Manually filter old android device log messages instead of relying on -T. (flutter#6630)
Fixes flutter#6280
1 parent 75fec89 commit 751539c

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

packages/flutter_tools/lib/src/android/android_device.dart

+39-17
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,16 @@ class AndroidDevice extends Device {
528528
return _portForwarder;
529529
}
530530

531+
static RegExp _timeRegExp = new RegExp(r'^\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}', multiLine: true);
532+
531533
/// Return the most recent timestamp in the Android log or `null` if there is
532534
/// no available timestamp. The format can be passed to logcat's -T option.
533535
String get lastLogcatTimestamp {
534536
String output = runCheckedSync(adbCommandForDevice(<String>[
535537
'logcat', '-v', 'time', '-t', '1'
536538
]));
537539

538-
RegExp timeRegExp = new RegExp(r'^\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}', multiLine: true);
539-
Match timeMatch = timeRegExp.firstMatch(output);
540+
Match timeMatch = _timeRegExp.firstMatch(output);
540541
return timeMatch?.group(0);
541542
}
542543

@@ -726,24 +727,24 @@ class _AdbLogReader extends DeviceLogReader {
726727
@override
727728
String get name => device.name;
728729

730+
DateTime _timeOrigin;
731+
732+
DateTime _adbTimestampToDateTime(String adbTimestamp) {
733+
// The adb timestamp format is: mm-dd hours:minutes:seconds.milliseconds
734+
// Dart's DateTime parse function accepts this format so long as we provide
735+
// the year, resulting in:
736+
// yyyy-mm-dd hours:minutes:seconds.milliseconds.
737+
return DateTime.parse('${new DateTime.now().year}-$adbTimestamp');
738+
}
739+
729740
void _start() {
730741
// Start the adb logcat process.
731-
List<String> args = <String>['logcat', '-v', 'tag'];
742+
List<String> args = <String>['logcat', '-v', 'time'];
732743
String lastTimestamp = device.lastLogcatTimestamp;
733-
if (lastTimestamp != null) {
734-
bool supportsLastTimestamp = false;
735-
736-
// Check to see if this copy of adb supports -T.
737-
try {
738-
// "logcat: invalid option -- T", "Unrecognized Option"
739-
// logcat -g will finish immediately; it will print an error to stdout if -T isn't supported.
740-
String result = runSync(device.adbCommandForDevice(<String>['logcat', '-g', '-T', lastTimestamp]));
741-
supportsLastTimestamp = !result.contains('logcat: invalid option') && !result.contains('Unrecognized Option');
742-
} catch (_) { }
743-
744-
if (supportsLastTimestamp)
745-
args.addAll(<String>['-T', lastTimestamp]);
746-
}
744+
if (lastTimestamp != null)
745+
_timeOrigin = _adbTimestampToDateTime(lastTimestamp);
746+
else
747+
_timeOrigin = null;
747748
runCommand(device.adbCommandForDevice(args)).then((Process process) {
748749
_process = process;
749750
_process.stdout.transform(UTF8.decoder).transform(const LineSplitter()).listen(_onLine);
@@ -771,7 +772,28 @@ class _AdbLogReader extends DeviceLogReader {
771772
// we default to true in case none of the log lines match
772773
bool _acceptedLastLine = true;
773774

775+
// The format of the line is controlled by the '-v' parameter passed to
776+
// adb logcat. We are currently passing 'time', which has the format:
777+
// mm-dd hh:mm:ss.milliseconds Priority/Tag( PID): ....
774778
void _onLine(String line) {
779+
final Match timeMatch = AndroidDevice._timeRegExp.firstMatch(line);
780+
if (timeMatch == null) {
781+
return;
782+
}
783+
if (_timeOrigin != null) {
784+
final String timestamp = timeMatch.group(0);
785+
DateTime time = _adbTimestampToDateTime(timestamp);
786+
if (time.isBefore(_timeOrigin)) {
787+
// Ignore log messages before the origin.
788+
printTrace('skipped old log line: $line');
789+
return;
790+
}
791+
}
792+
if (line.length == timeMatch.end) {
793+
return;
794+
}
795+
// Chop off the time.
796+
line = line.substring(timeMatch.end + 1);
775797
if (_logFormat.hasMatch(line)) {
776798
// Filter on approved names and levels.
777799
for (RegExp regex in _whitelistedTags) {

packages/flutter_tools/lib/src/hot.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,13 @@ class HotRunner extends ResidentRunner {
249249
}
250250

251251
_observatoryPort = result.observatoryPort;
252-
await connectToServiceProtocol(_observatoryPort);
252+
try {
253+
await connectToServiceProtocol(_observatoryPort);
254+
} catch (error) {
255+
printError('Error connecting to the service protocol: $error');
256+
return 2;
257+
}
258+
253259

254260
try {
255261
Uri baseUri = await _initDevFS();

0 commit comments

Comments
 (0)