From 9cb914df171b3db12176177e3cd3d68bc38ed477 Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Wed, 16 Nov 2016 17:19:00 -0800 Subject: [PATCH] Allow for application-specific log readers. (#6898) * Allow for application-specific log readers. When running with prebuilt application binaries, those applications aren't guaranteed to be named "Runner" (as it is when we build the app locally in Flutter tools) --- .../lib/src/android/android_device.dart | 9 +-- .../flutter_tools/lib/src/commands/drive.dart | 6 +- .../flutter_tools/lib/src/commands/logs.dart | 2 +- packages/flutter_tools/lib/src/device.dart | 6 +- packages/flutter_tools/lib/src/hot.dart | 2 +- .../flutter_tools/lib/src/ios/devices.dart | 58 ++++++++++++------- .../flutter_tools/lib/src/ios/simulators.dart | 21 ++++--- .../lib/src/resident_runner.dart | 5 +- packages/flutter_tools/lib/src/run.dart | 2 +- 9 files changed, 68 insertions(+), 43 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 6478c58d04..05d7e14c39 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -294,6 +294,7 @@ class AndroidDevice extends Device { ProtocolDiscovery observatoryDiscovery; ProtocolDiscovery diagnosticDiscovery; + DeviceLogReader logReader = getLogReader(); if (options.debuggingEnabled) { observatoryDiscovery = new ProtocolDiscovery(logReader, ProtocolDiscovery.kObservatoryService); diagnosticDiscovery = new ProtocolDiscovery(logReader, ProtocolDiscovery.kDiagnosticService); @@ -439,9 +440,9 @@ class AndroidDevice extends Device { } @override - DeviceLogReader get logReader { - if (_logReader == null) - _logReader = new _AdbLogReader(this); + DeviceLogReader getLogReader({ApplicationPackage app}) { + // The Android log reader isn't app-specific. + _logReader ??= new _AdbLogReader(this); return _logReader; } @@ -487,7 +488,7 @@ class AndroidDevice extends Device { Future> discoverApps() { RegExp discoverExp = new RegExp(r'DISCOVER: (.*)'); List result = []; - StreamSubscription logs = logReader.logLines.listen((String line) { + StreamSubscription logs = getLogReader().logLines.listen((String line) { Match match = discoverExp.firstMatch(line); if (match != null) { Map app = JSON.decode(match.group(1)); diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index ece12f6e38..9970edb642 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -301,7 +301,11 @@ Future startApp(DriveCommand command) async { printTrace('Starting application.'); // Forward device log messages to the terminal window running the "drive" command. - command._deviceLogSubscription = command.device.logReader.logLines.listen(printStatus); + command._deviceLogSubscription = command + .device + .getLogReader(app: package) + .logLines + .listen(printStatus); LaunchResult result = await command.device.startApp( package, diff --git a/packages/flutter_tools/lib/src/commands/logs.dart b/packages/flutter_tools/lib/src/commands/logs.dart index 1b987ccf86..fc6173793b 100644 --- a/packages/flutter_tools/lib/src/commands/logs.dart +++ b/packages/flutter_tools/lib/src/commands/logs.dart @@ -41,7 +41,7 @@ class LogsCommand extends FlutterCommand { if (argResults['clear']) device.clearLogs(); - DeviceLogReader logReader = device.logReader; + DeviceLogReader logReader = device.getLogReader(); Cache.releaseLockEarly(); diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 952e05d421..1f9e7a2a43 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -164,8 +164,10 @@ abstract class Device { String get sdkNameAndVersion; - /// Get the log reader for this device. - DeviceLogReader get logReader; + /// Get a log reader for this device. + /// If [app] is specified, this will return a log reader specific to that + /// application. Otherwise, a global log reader will be returned. + DeviceLogReader getLogReader({ApplicationPackage app}); /// Get the port forwarder for this device. DevicePortForwarder get portForwarder; diff --git a/packages/flutter_tools/lib/src/hot.dart b/packages/flutter_tools/lib/src/hot.dart index 592a6e151b..e53b07b1d2 100644 --- a/packages/flutter_tools/lib/src/hot.dart +++ b/packages/flutter_tools/lib/src/hot.dart @@ -226,7 +226,7 @@ class HotRunner extends ResidentRunner { Map platformArgs = new Map(); - await startEchoingDeviceLog(); + await startEchoingDeviceLog(_package); printTrace('Launching loader on ${device.name}...'); diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 972c186325..396582f26a 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -19,6 +19,8 @@ const String _ideviceinstallerInstructions = 'To work with iOS devices, please install ideviceinstaller.\n' 'If you use homebrew, you can install it with "\$ brew install ideviceinstaller".'; +const Duration kPortForwardTimeout = const Duration(seconds: 10); + class IOSDevices extends PollingDeviceDiscovery { IOSDevices() : super('IOSDevices'); @@ -72,7 +74,7 @@ class IOSDevice extends Device { @override final String name; - _IOSDeviceLogReader _logReader; + Map _logReaders; _IOSDevicePortForwarder _portForwarder; @@ -251,11 +253,13 @@ class IOSDevice extends Device { // ports post launch. printTrace("Debugging is enabled, connecting to observatory and the diagnostic server"); - Future forwardObsPort = _acquireAndForwardPort(ProtocolDiscovery.kObservatoryService, + Future forwardObsPort = _acquireAndForwardPort(app, + ProtocolDiscovery.kObservatoryService, debuggingOptions.observatoryPort); Future forwardDiagPort; if (debuggingOptions.buildMode == BuildMode.debug) { - forwardDiagPort = _acquireAndForwardPort(ProtocolDiscovery.kDiagnosticService, + forwardDiagPort = _acquireAndForwardPort(app, + ProtocolDiscovery.kDiagnosticService, debuggingOptions.diagnosticPort); } else { forwardDiagPort = new Future.value(null); @@ -272,7 +276,13 @@ class IOSDevice extends Device { } printTrace("Application launched on the device. Attempting to forward ports."); - return Future.wait(>[forwardObsPort, forwardDiagPort]); + return await Future.wait(>[forwardObsPort, forwardDiagPort]) + .timeout( + kPortForwardTimeout, + onTimeout: () { + throw new TimeoutException('Timeout while waiting to acquire and forward ports.'); + }, + ); }); printTrace("Local Observatory Port: ${ports[0]}"); @@ -293,10 +303,13 @@ class IOSDevice extends Device { return new LaunchResult.succeeded(observatoryPort: localObsPort, diagnosticPort: localDiagPort); } - Future _acquireAndForwardPort(String serviceName, int localPort) async { + Future _acquireAndForwardPort( + ApplicationPackage app, + String serviceName, + int localPort) async { Duration stepTimeout = const Duration(seconds: 60); - Future remote = new ProtocolDiscovery(logReader, serviceName).nextPort(); + Future remote = new ProtocolDiscovery(getLogReader(app: app), serviceName).nextPort(); int remotePort = await remote.timeout(stepTimeout, onTimeout: () { @@ -365,11 +378,9 @@ class IOSDevice extends Device { String get _buildVersion => _getDeviceInfo(id, 'BuildVersion'); @override - DeviceLogReader get logReader { - if (_logReader == null) - _logReader = new _IOSDeviceLogReader(this); - - return _logReader; + DeviceLogReader getLogReader({ApplicationPackage app}) { + _logReaders ??= {}; + return _logReaders.putIfAbsent(app, () => new _IOSDeviceLogReader(this, app)); } @override @@ -398,11 +409,20 @@ class IOSDevice extends Device { } class _IOSDeviceLogReader extends DeviceLogReader { - _IOSDeviceLogReader(this.device) { + RegExp _lineRegex; + + _IOSDeviceLogReader(this.device, ApplicationPackage app) { _linesController = new StreamController.broadcast( - onListen: _start, - onCancel: _stop - ); + onListen: _start, + onCancel: _stop + ); + + // Match for lines for the runner in syslog. + // + // iOS 9 format: Runner[297] : + // iOS 10 format: Runner(libsystem_asl.dylib)[297] : + String appName = app == null ? '' : app.name.replaceAll('.app', ''); + _lineRegex = new RegExp(appName + r'(\(.*\))?\[[\d]+\] <[A-Za-z]+>: '); } final IOSDevice device; @@ -429,14 +449,8 @@ class _IOSDeviceLogReader extends DeviceLogReader { }); } - // Match for lines for the runner in syslog. - // - // iOS 9 format: Runner[297] : - // iOS 10 format: Runner(libsystem_asl.dylib)[297] : - static final RegExp _runnerRegex = new RegExp(r'Runner(\(.*\))?\[[\d]+\] <[A-Za-z]+>: '); - void _onLine(String line) { - Match match = _runnerRegex.firstMatch(line); + Match match = _lineRegex.firstMatch(line); if (match != null) { // Only display the log line after the initial device and executable information. diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 06fdb98e0f..d91f817611 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -315,7 +315,7 @@ class IOSSimulator extends Device { @override bool get supportsHotMode => true; - _IOSSimulatorLogReader _logReader; + Map _logReaders; _IOSSimulatorDevicePortForwarder _portForwarder; String get xcrunPath => path.join('/usr', 'bin', 'xcrun'); @@ -431,7 +431,8 @@ class IOSSimulator extends Device { ProtocolDiscovery observatoryDiscovery; if (debuggingOptions.debuggingEnabled) - observatoryDiscovery = new ProtocolDiscovery(logReader, ProtocolDiscovery.kObservatoryService); + observatoryDiscovery = new ProtocolDiscovery(getLogReader(app: app), + ProtocolDiscovery.kObservatoryService); // Prepare launch arguments. List args = []; @@ -554,11 +555,9 @@ class IOSSimulator extends Device { String get sdkNameAndVersion => category; @override - DeviceLogReader get logReader { - if (_logReader == null) - _logReader = new _IOSSimulatorLogReader(this); - - return _logReader; + DeviceLogReader getLogReader({ApplicationPackage app}) { + _logReaders ??= {}; + return _logReaders.putIfAbsent(app, () => new _IOSSimulatorLogReader(this, app)); } @override @@ -629,13 +628,16 @@ class IOSSimulator extends Device { } class _IOSSimulatorLogReader extends DeviceLogReader { - _IOSSimulatorLogReader(this.device) { + String _appName; + + _IOSSimulatorLogReader(this.device, ApplicationPackage app) { _linesController = new StreamController.broadcast( onListen: () { _start(); }, onCancel: _stop ); + _appName = app == null ? null : app.name.replaceAll('.app', ''); } final IOSSimulator device; @@ -712,8 +714,9 @@ class _IOSSimulatorLogReader extends DeviceLogReader { && content.endsWith(']: 0x1')) return null; - if (category == 'Runner') + if (_appName != null && category == _appName) { return content; + } return '$category: $content'; } match = _lastMessageSingleRegex.matchAsPrefix(string); diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 51dec37a2f..456e66d142 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -8,6 +8,7 @@ import 'dart:io'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; +import 'application_package.dart'; import 'base/logger.dart'; import 'build_info.dart'; import 'device.dart'; @@ -96,11 +97,11 @@ abstract class ResidentRunner { }); } - Future startEchoingDeviceLog() async { + Future startEchoingDeviceLog(ApplicationPackage app) async { if (_loggingSubscription != null) { return; } - _loggingSubscription = device.logReader.logLines.listen((String line) { + _loggingSubscription = device.getLogReader(app: app).logLines.listen((String line) { if (!line.contains('Observatory listening on http') && !line.contains('Diagnostic server listening on http')) printStatus(line); diff --git a/packages/flutter_tools/lib/src/run.dart b/packages/flutter_tools/lib/src/run.dart index 28f1da744e..11689f27d7 100644 --- a/packages/flutter_tools/lib/src/run.dart +++ b/packages/flutter_tools/lib/src/run.dart @@ -119,7 +119,7 @@ class RunAndStayResident extends ResidentRunner { if (traceStartup != null) platformArgs = { 'trace-startup': traceStartup }; - await startEchoingDeviceLog(); + await startEchoingDeviceLog(_package); if (_mainPath == null) { assert(prebuiltMode); printStatus('Running ${_package.displayName} on ${device.name}');