Unverified Commit aa65fa2f authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Remove await for syntax from fuchsia log scanner (#24263)

parent 2e33d08d
......@@ -29,7 +29,6 @@ class _FuchsiaLogReader extends DeviceLogReader {
_FuchsiaLogReader(this._device, [this._app]);
static final RegExp _flutterLogOutput = RegExp(r'INFO: \w+\(flutter\): ');
static final RegExp _utcDateOutput = RegExp(r'\d+\-\d+\-\d+ \d+:\d+:\d+');
FuchsiaDevice _device;
ApplicationPackage _app;
......@@ -43,7 +42,7 @@ class _FuchsiaLogReader extends DeviceLogReader {
return _logLines;
}
Stream<String> _processLogs(Stream<String> lines) async* {
Stream<String> _processLogs(Stream<String> lines) {
// Get the starting time of the log processor to filter logs from before
// the process attached.
final DateTime startTime = systemClock.now();
......@@ -52,26 +51,49 @@ class _FuchsiaLogReader extends DeviceLogReader {
final RegExp matchRegExp = _app == null
? _flutterLogOutput
: RegExp('INFO: ${_app.name}\\(flutter\\): ');
await for (String line in lines.where(matchRegExp.hasMatch)) {
// Read out the date string from the log and compare it to the current time:
// Example: 2018-11-09 01:27:45
final String rawDate = _utcDateOutput.firstMatch(line)?.group(0);
if (rawDate == null) {
continue;
}
final DateTime logTime = DateTime.parse(rawDate);
if (logTime.millisecondsSinceEpoch < startTime.millisecondsSinceEpoch) {
continue;
}
// Format log into a useful string:
yield '[${logTime.toLocal()}] Flutter: ${line.split(matchRegExp).last}';
}
return Stream<String>.eventTransformed(
lines,
(Sink<String> outout) => _FuchsiaLogSink(outout, matchRegExp, startTime),
);
}
@override
String toString() => name;
}
class _FuchsiaLogSink implements EventSink<String> {
_FuchsiaLogSink(this._outputSink, this._matchRegExp, this._startTime);
static final RegExp _utcDateOutput = RegExp(r'\d+\-\d+\-\d+ \d+:\d+:\d+');
final EventSink<String> _outputSink;
final RegExp _matchRegExp;
final DateTime _startTime;
@override
void add(String line) {
if (!_matchRegExp.hasMatch(line)) {
return;
}
final String rawDate = _utcDateOutput.firstMatch(line)?.group(0);
if (rawDate == null) {
return;
}
final DateTime logTime = DateTime.parse(rawDate);
if (logTime.millisecondsSinceEpoch < _startTime.millisecondsSinceEpoch) {
return;
}
_outputSink.add('[${logTime.toLocal()}] Flutter: ${line.split(_matchRegExp).last}');
}
@override
void addError(Object error, [StackTrace stackTrace]) {
_outputSink.addError(error, stackTrace);
}
@override
void close() { _outputSink.close(); }
}
class FuchsiaDevices extends PollingDeviceDiscovery {
FuchsiaDevices() : super('Fuchsia devices');
......
......@@ -126,11 +126,18 @@ d 2 0 .
final FuchsiaDevice device = FuchsiaDevice('id', name: 'tester');
final DeviceLogReader reader = device.getLogReader(app: FuchsiaModulePackage(name: 'example_app'));
final List<String> logLines = <String>[];
reader.logLines.listen(logLines.add);
final Completer<void> lock = Completer<void>();
reader.logLines.listen((String line) {
logLines.add(line);
if (logLines.length == 2) {
lock.complete();
}
});
expect(logLines, isEmpty);
stdout.add(utf8.encode(exampleUtcLogs));
await stdout.close();
await lock.future.timeout(const Duration(seconds: 1));
expect(logLines, <String>[
'[2018-11-09 01:27:45.000] Flutter: Error doing thing',
......@@ -154,7 +161,7 @@ d 2 0 .
stdout.add(utf8.encode(exampleUtcLogs));
await stdout.close();
await lock.future;
await lock.future.timeout(const Duration(seconds: 1));
expect(logLines, <String>[
'[2018-11-09 01:30:12.000] Flutter: Did thing this time',
......@@ -168,11 +175,18 @@ d 2 0 .
final FuchsiaDevice device = FuchsiaDevice('id', name: 'tester');
final DeviceLogReader reader = device.getLogReader();
final List<String> logLines = <String>[];
reader.logLines.listen(logLines.add);
final Completer<void> lock = Completer<void>();
reader.logLines.listen((String line) {
logLines.add(line);
if (logLines.length == 3) {
lock.complete();
}
});
expect(logLines, isEmpty);
stdout.add(utf8.encode(exampleUtcLogs));
await stdout.close();
await lock.future.timeout(const Duration(seconds: 1));
expect(logLines, <String>[
'[2018-11-09 01:27:45.000] Flutter: Error doing thing',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment