Let flutter attach find the service port by looking through old logs again (#53153)

This commit is contained in:
xster
2020-03-24 23:26:01 -07:00
committed by GitHub
parent 6563b0de34
commit 0c5ffdc9ef
15 changed files with 220 additions and 69 deletions

View File

@@ -213,6 +213,7 @@ class AndroidDevice extends Device {
Future<String> get apiVersion => _getProperty('ro.build.version.sdk');
AdbLogReader _logReader;
AdbLogReader _pastLogReader;
_AndroidDevicePortForwarder _portForwarder;
List<String> adbCommandForDevice(List<String> args) {
@@ -673,9 +674,23 @@ class AndroidDevice extends Device {
}
@override
FutureOr<DeviceLogReader> getLogReader({ AndroidApk app }) async {
// The Android log reader isn't app-specific.
return _logReader ??= await AdbLogReader.createLogReader(this, globals.processManager);
FutureOr<DeviceLogReader> getLogReader({
AndroidApk app,
bool includePastLogs = false,
}) async {
// The Android log reader isn't app-specific. The `app` parameter isn't used.
if (includePastLogs) {
return _pastLogReader ??= await AdbLogReader.createLogReader(
this,
globals.processManager,
includePastLogs: true,
);
} else {
return _logReader ??= await AdbLogReader.createLogReader(
this,
globals.processManager,
);
}
}
@override
@@ -724,6 +739,7 @@ class AndroidDevice extends Device {
@override
Future<void> dispose() async {
_logReader?._stop();
_pastLogReader?._stop();
await _portForwarder?.dispose();
}
}
@@ -901,6 +917,9 @@ class AdbLogReader extends DeviceLogReader {
static Future<AdbLogReader> createLogReader(
AndroidDevice device,
ProcessManager processManager,
{
bool includePastLogs = false,
}
) async {
// logcat -T is not supported on Android releases before Lollipop.
const int kLollipopVersionCode = 21;
@@ -915,7 +934,12 @@ class AdbLogReader extends DeviceLogReader {
'logcat',
'-v',
'time',
if (apiVersion != null && apiVersion >= kLollipopVersionCode) ...<String>[
// If we include logs from the past, filter for 'flutter' logs only.
if (includePastLogs) ...<String>[
'-s',
'flutter',
] else if (apiVersion != null && apiVersion >= kLollipopVersionCode) ...<String>[
// Otherwise, filter for logs appearing past the present.
// Empty `-T` means the timestamp of the logcat command invocation.
'-T',
device.lastLogcatTimestamp ?? '',

View File

@@ -6,6 +6,7 @@ import 'dart:async';
import 'package:meta/meta.dart';
import '../android/android_device.dart';
import '../artifacts.dart';
import '../base/common.dart';
import '../base/context.dart';
@@ -245,7 +246,9 @@ class AttachCommand extends FlutterCommand {
if (observatoryUri == null) {
final ProtocolDiscovery observatoryDiscovery =
ProtocolDiscovery.observatory(
await device.getLogReader(),
// If it's an Android device, attaching relies on past log searching
// to find the service protocol.
await device.getLogReader(includePastLogs: device is AndroidDevice),
portForwarder: device.portForwarder,
ipv6: ipv6,
devicePort: deviceVmservicePort,

View File

@@ -63,7 +63,11 @@ abstract class DesktopDevice extends Device {
Future<String> get sdkNameAndVersion async => globals.os.name;
@override
DeviceLogReader getLogReader({ ApplicationPackage app }) {
DeviceLogReader getLogReader({
ApplicationPackage app,
bool includePastLogs = false,
}) {
assert(!includePastLogs, 'Past log reading not supported on desktop.');
return _deviceLogReader;
}

View File

@@ -415,9 +415,17 @@ abstract class Device {
Future<String> get sdkNameAndVersion;
/// Get a log reader for this device.
/// If [app] is specified, this will return a log reader specific to that
///
/// If `app` is specified, this will return a log reader specific to that
/// application. Otherwise, a global log reader will be returned.
FutureOr<DeviceLogReader> getLogReader({ covariant ApplicationPackage app });
///
/// If `includePastLogs` is true and the device type supports it, the log
/// reader will also include log messages from before the invocation time.
/// Defaults to false.
FutureOr<DeviceLogReader> getLogReader({
covariant ApplicationPackage app,
bool includePastLogs = false,
});
/// Get the port forwarder for this device.
DevicePortForwarder get portForwarder;

View File

@@ -508,8 +508,13 @@ class FuchsiaDevice extends Device {
}
@override
DeviceLogReader getLogReader({ApplicationPackage app}) =>
_logReader ??= _FuchsiaLogReader(this, app);
DeviceLogReader getLogReader({
ApplicationPackage app,
bool includePastLogs = false,
}) {
assert(!includePastLogs, 'Past log reading not supported on Fuchsia.');
return _logReader ??= _FuchsiaLogReader(this, app);
}
_FuchsiaLogReader _logReader;
@override

View File

@@ -372,7 +372,11 @@ class IOSDevice extends Device {
Future<String> get sdkNameAndVersion async => 'iOS $_sdkVersion';
@override
DeviceLogReader getLogReader({ IOSApp app }) {
DeviceLogReader getLogReader({
IOSApp app,
bool includePastLogs = false,
}) {
assert(!includePastLogs, 'Past log reading not supported on iOS devices.');
_logReaders ??= <IOSApp, DeviceLogReader>{};
return _logReaders.putIfAbsent(app, () => IOSDeviceLogReader.create(
device: this,

View File

@@ -519,8 +519,12 @@ class IOSSimulator extends Device {
}
@override
DeviceLogReader getLogReader({ covariant IOSApp app }) {
DeviceLogReader getLogReader({
covariant IOSApp app,
bool includePastLogs = false,
}) {
assert(app is IOSApp);
assert(!includePastLogs, 'Past log reading not supported on iOS simulators.');
_logReaders ??= <ApplicationPackage, _IOSSimulatorLogReader>{};
return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app));
}

View File

@@ -79,7 +79,12 @@ class FlutterTesterDevice extends Device {
_FlutterTesterDeviceLogReader();
@override
DeviceLogReader getLogReader({ ApplicationPackage app }) => _logReader;
DeviceLogReader getLogReader({
ApplicationPackage app,
bool includePastLogs = false,
}) {
return _logReader;
}
@override
Future<bool> installApp(ApplicationPackage app) async => true;

View File

@@ -60,7 +60,10 @@ class ChromeDevice extends Device {
DeviceLogReader _logReader;
@override
DeviceLogReader getLogReader({ApplicationPackage app}) {
DeviceLogReader getLogReader({
ApplicationPackage app,
bool includePastLogs = false,
}) {
return _logReader ??= NoOpDeviceLogReader(app?.name);
}
@@ -221,7 +224,10 @@ class WebServerDevice extends Device {
DeviceLogReader _logReader;
@override
DeviceLogReader getLogReader({ApplicationPackage app}) {
DeviceLogReader getLogReader({
ApplicationPackage app,
bool includePastLogs = false,
}) {
return _logReader ??= NoOpDeviceLogReader(app?.name);
}