Add option to stream logs to file for flutter logs and way to use it in devicelab runs (#84008)
This commit is contained in:
@@ -152,6 +152,23 @@ abstract class Device {
|
||||
/// with some prefix.
|
||||
Stream<String> get logcat;
|
||||
|
||||
/// Whether this device supports calls to [startLoggingToSink]
|
||||
/// and [stopLoggingToSink].
|
||||
bool get canStreamLogs => false;
|
||||
|
||||
/// Starts logging to an [IOSink].
|
||||
///
|
||||
/// If `clear` is set to true, the log will be cleared before starting. This
|
||||
/// is not supported on all platforms.
|
||||
Future<void> startLoggingToSink(IOSink sink, {bool clear = true}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
/// Stops logging that was started by [startLoggingToSink].
|
||||
Future<void> stopLoggingToSink() {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
/// Stop a process.
|
||||
Future<void> stop(String packageName);
|
||||
|
||||
@@ -558,6 +575,52 @@ class AndroidDevice extends Device {
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
bool get canStreamLogs => true;
|
||||
|
||||
bool _abortedLogging/*!*/ = false;
|
||||
Process/*?*/ _loggingProcess;
|
||||
|
||||
@override
|
||||
Future<void> startLoggingToSink(IOSink sink, {bool clear = true}) async {
|
||||
if (clear) {
|
||||
await adb(<String>['logcat', '--clear'], silent: true);
|
||||
}
|
||||
_loggingProcess = await startProcess(
|
||||
adbPath,
|
||||
// Make logcat less chatty by filtering down to just ActivityManager
|
||||
// (to let us know when app starts), flutter (needed by tests to see
|
||||
// log output), and fatal messages (hopefully catches tombstones).
|
||||
// For local testing, this can just be:
|
||||
// <String>['-s', deviceId, 'logcat']
|
||||
// to view the whole log, or just run logcat alongside this.
|
||||
<String>['-s', deviceId, 'logcat', 'ActivityManager:I', 'flutter:V', '*:F'],
|
||||
);
|
||||
_loggingProcess.stdout
|
||||
.transform<String>(const Utf8Decoder(allowMalformed: true))
|
||||
.listen((String line) {
|
||||
sink.write(line);
|
||||
});
|
||||
_loggingProcess.stderr
|
||||
.transform<String>(const Utf8Decoder(allowMalformed: true))
|
||||
.listen((String line) {
|
||||
sink.write(line);
|
||||
});
|
||||
unawaited(_loggingProcess.exitCode.then<void>((int exitCode) {
|
||||
if (!_abortedLogging) {
|
||||
sink.writeln('adb logcat failed with exit code $exitCode.\n');
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> stopLoggingToSink() async {
|
||||
assert(_loggingProcess != null);
|
||||
_abortedLogging = true;
|
||||
_loggingProcess.kill();
|
||||
await _loggingProcess.exitCode;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<String> get logcat {
|
||||
final Completer<void> stdoutDone = Completer<void>();
|
||||
@@ -760,11 +823,66 @@ class IosDeviceDiscovery implements DeviceDiscovery {
|
||||
|
||||
/// iOS device.
|
||||
class IosDevice extends Device {
|
||||
const IosDevice({ @required this.deviceId });
|
||||
IosDevice({ @required this.deviceId });
|
||||
|
||||
@override
|
||||
final String deviceId;
|
||||
|
||||
String get idevicesyslogPath {
|
||||
return path.join(flutterDirectory.path, 'bin', 'cache', 'artifacts', 'libimobiledevice', 'idevicesyslog');
|
||||
}
|
||||
|
||||
String get dyldLibraryPath {
|
||||
final List<String> dylibsPaths = <String>[
|
||||
path.join(flutterDirectory.path, 'bin', 'cache', 'artifacts', 'libimobiledevice'),
|
||||
path.join(flutterDirectory.path, 'bin', 'cache', 'artifacts', 'openssl'),
|
||||
path.join(flutterDirectory.path, 'bin', 'cache', 'artifacts', 'usbmuxd'),
|
||||
path.join(flutterDirectory.path, 'bin', 'cache', 'artifacts', 'libplist'),
|
||||
];
|
||||
return dylibsPaths.join(':');
|
||||
}
|
||||
|
||||
@override
|
||||
bool get canStreamLogs => true;
|
||||
|
||||
bool _abortedLogging/*!*/ = false;
|
||||
Process/*?*/ _loggingProcess;
|
||||
|
||||
@override
|
||||
Future<void> startLoggingToSink(IOSink sink, {bool clear = true}) async {
|
||||
// Clear is not supported.
|
||||
_loggingProcess = await startProcess(
|
||||
idevicesyslogPath,
|
||||
<String>['-u', deviceId, '--quiet'],
|
||||
environment: <String, String>{
|
||||
'DYLD_LIBRARY_PATH': dyldLibraryPath,
|
||||
},
|
||||
);
|
||||
_loggingProcess.stdout
|
||||
.transform<String>(const Utf8Decoder(allowMalformed: true))
|
||||
.listen((String line) {
|
||||
sink.write(line);
|
||||
});
|
||||
_loggingProcess.stderr
|
||||
.transform<String>(const Utf8Decoder(allowMalformed: true))
|
||||
.listen((String line) {
|
||||
sink.write(line);
|
||||
});
|
||||
unawaited(_loggingProcess.exitCode.then<void>((int exitCode) {
|
||||
if (!_abortedLogging) {
|
||||
sink.writeln('idevicesyslog failed with exit code $exitCode.\n');
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> stopLoggingToSink() async {
|
||||
assert(_loggingProcess != null);
|
||||
_abortedLogging = true;
|
||||
_loggingProcess.kill();
|
||||
await _loggingProcess.exitCode;
|
||||
}
|
||||
|
||||
// The methods below are stubs for now. They will need to be expanded.
|
||||
// We currently do not have a way to lock/unlock iOS devices. So we assume the
|
||||
// devices are already unlocked. For now we'll just keep them at minimum
|
||||
@@ -14,7 +14,8 @@ import 'package:logging/logging.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
|
||||
import 'adb.dart';
|
||||
import 'devices.dart';
|
||||
import 'host_agent.dart';
|
||||
import 'running_processes.dart';
|
||||
import 'task_result.dart';
|
||||
import 'utils.dart';
|
||||
@@ -81,6 +82,14 @@ class _TaskRunner {
|
||||
|
||||
final TaskFunction task;
|
||||
|
||||
Future<Device/*?*/> _getWorkingDeviceIfAvailable() async {
|
||||
try {
|
||||
return await devices.workingDevice;
|
||||
} on DeviceException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(ianh): workaround for https://github.com/dart-lang/sdk/issues/23797
|
||||
RawReceivePort _keepAlivePort;
|
||||
Timer _startTaskTimeout;
|
||||
@@ -136,11 +145,27 @@ class _TaskRunner {
|
||||
print('Skipping enabling configs for macOS, Linux, Windows, and Web');
|
||||
}
|
||||
|
||||
Future<TaskResult> futureResult = _performTask();
|
||||
if (taskTimeout != null)
|
||||
futureResult = futureResult.timeout(taskTimeout);
|
||||
final Device/*?*/ device = await _getWorkingDeviceIfAvailable();
|
||||
/*late*/ TaskResult result;
|
||||
IOSink/*?*/ sink;
|
||||
try {
|
||||
if (device != null && device.canStreamLogs && hostAgent.dumpDirectory != null) {
|
||||
sink = File(path.join(hostAgent.dumpDirectory.path, '${device.deviceId}.log')).openWrite();
|
||||
await device.startLoggingToSink(sink);
|
||||
}
|
||||
|
||||
TaskResult result = await futureResult;
|
||||
Future<TaskResult> futureResult = _performTask();
|
||||
if (taskTimeout != null)
|
||||
futureResult = futureResult.timeout(taskTimeout);
|
||||
|
||||
result = await futureResult;
|
||||
} finally {
|
||||
if (device != null && device.canStreamLogs) {
|
||||
assert(sink != null);
|
||||
await device.stopLoggingToSink();
|
||||
await sink.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (runProcessCleanup) {
|
||||
section('Checking running Dart$exe processes after task...');
|
||||
|
||||
@@ -22,14 +22,11 @@ class HostAgent {
|
||||
final FileSystem _fileSystem;
|
||||
|
||||
/// Creates a directory to dump file artifacts.
|
||||
Directory get dumpDirectory {
|
||||
Directory/*?*/ get dumpDirectory {
|
||||
if (_dumpDirectory == null) {
|
||||
// Set in LUCI recipe.
|
||||
final String directoryPath = _platform.environment['FLUTTER_LOGS_DIR'];
|
||||
if (directoryPath == null) {
|
||||
_dumpDirectory = _fileSystem.systemTempDirectory.createTempSync('flutter_test_logs.');
|
||||
print('Created tmp dump directory ${_dumpDirectory.path}');
|
||||
} else {
|
||||
if (directoryPath != null) {
|
||||
_dumpDirectory = _fileSystem.directory(directoryPath)..createSync(recursive: true);
|
||||
print('Found FLUTTER_LOGS_DIR dump directory ${_dumpDirectory.path}');
|
||||
}
|
||||
@@ -37,7 +34,7 @@ class HostAgent {
|
||||
return _dumpDirectory;
|
||||
}
|
||||
|
||||
static Directory _dumpDirectory;
|
||||
static Directory/*?*/ _dumpDirectory;
|
||||
|
||||
@visibleForTesting
|
||||
void resetDumpDirectory() {
|
||||
|
||||
@@ -12,8 +12,8 @@ import 'package:flutter_devicelab/common.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:vm_service/vm_service.dart';
|
||||
|
||||
import 'adb.dart';
|
||||
import 'cocoon.dart';
|
||||
import 'devices.dart';
|
||||
import 'task_result.dart';
|
||||
import 'utils.dart';
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter_devicelab/common.dart';
|
||||
import 'package:flutter_devicelab/framework/adb.dart';
|
||||
import 'package:flutter_devicelab/framework/devices.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:process/process.dart';
|
||||
@@ -471,6 +471,18 @@ Future<int> flutter(String command, {
|
||||
canFail: canFail, environment: environment);
|
||||
}
|
||||
|
||||
Future<Process> startFlutter(String command, {
|
||||
List<String> options = const <String>[],
|
||||
Map<String, String> environment = const <String, String>{},
|
||||
}) {
|
||||
final List<String> args = flutterCommandArgs(command, options);
|
||||
return startProcess(
|
||||
path.join(flutterDirectory.path, 'bin', 'flutter'),
|
||||
args,
|
||||
environment: environment,
|
||||
);
|
||||
}
|
||||
|
||||
/// Runs a `flutter` command and returns the standard output as a string.
|
||||
Future<String> evalFlutter(String command, {
|
||||
List<String> options = const <String>[],
|
||||
|
||||
Reference in New Issue
Block a user