Skip to content

Commit 477ca01

Browse files
authored
Support --[no-]adb-log-filtering CLI option (flutter#187252)
Support --[no-]adb-log-filtering CLI option and adds command integration tests Add command integration tests to verify that the logs command correctly parses the --[no-]adb-log-filtering option and propagates it to the target device. Fixes: flutter#120711 ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing.
1 parent 20c2d6b commit 477ca01

13 files changed

Lines changed: 201 additions & 19 deletions

File tree

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,12 @@ class AndroidDevice extends Device {
615615
// Avoid using getLogReader, which returns a singleton instance, because the
616616
// VM Service discovery will dispose at the end. creating a new logger here allows
617617
// logs to be surfaced normally during `flutter drive`.
618-
await AdbLogReader.createLogReader(this, _processManager, _logger),
618+
await AdbLogReader.createLogReader(
619+
this,
620+
_processManager,
621+
_logger,
622+
adbLogFiltering: debuggingOptions.adbLogFiltering,
623+
),
619624
portForwarder: portForwarder,
620625
hostPort: debuggingOptions.hostVmServicePort,
621626
devicePort: debuggingOptions.deviceVmServicePort,
@@ -740,6 +745,7 @@ class AndroidDevice extends Device {
740745
FutureOr<DeviceLogReader> getLogReader({
741746
ApplicationPackage? app,
742747
bool includePastLogs = false,
748+
bool adbLogFiltering = true,
743749
}) async {
744750
// The Android log reader isn't app-specific. The `app` parameter isn't used.
745751
if (includePastLogs) {
@@ -748,9 +754,15 @@ class AndroidDevice extends Device {
748754
_processManager,
749755
_logger,
750756
includePastLogs: true,
757+
adbLogFiltering: adbLogFiltering,
751758
);
752759
} else {
753-
return _logReader ??= await AdbLogReader.createLogReader(this, _processManager, _logger);
760+
return _logReader ??= await AdbLogReader.createLogReader(
761+
this,
762+
_processManager,
763+
_logger,
764+
adbLogFiltering: adbLogFiltering,
765+
);
754766
}
755767
}
756768

@@ -999,17 +1011,25 @@ class AndroidMemoryInfo extends MemoryInfo {
9991011

10001012
/// A log reader that logs from `adb logcat`.
10011013
class AdbLogReader extends DeviceLogReader {
1002-
AdbLogReader._(this._adbProcess, this.name, this._logger);
1014+
AdbLogReader._(this._adbProcess, this.name, this._logger, {this.adbLogFiltering = true});
10031015

10041016
@visibleForTesting
1005-
factory AdbLogReader.test(Process adbProcess, String name, Logger logger) = AdbLogReader._;
1017+
factory AdbLogReader.test(
1018+
Process adbProcess,
1019+
String name,
1020+
Logger logger, {
1021+
bool adbLogFiltering = true,
1022+
}) {
1023+
return AdbLogReader._(adbProcess, name, logger, adbLogFiltering: adbLogFiltering);
1024+
}
10061025

10071026
/// Create a new [AdbLogReader] from an [AndroidDevice] instance.
10081027
static Future<AdbLogReader> createLogReader(
10091028
AndroidDevice device,
10101029
ProcessManager processManager,
10111030
Logger logger, {
10121031
bool includePastLogs = false,
1032+
bool adbLogFiltering = true,
10131033
}) async {
10141034
// logcat -T is not supported on Android releases before Lollipop.
10151035
const kLollipopVersionCode = 21;
@@ -1037,7 +1057,7 @@ class AdbLogReader extends DeviceLogReader {
10371057
]);
10381058
}
10391059
final Process process = await processManager.start(device.adbCommandForDevice(args));
1040-
return AdbLogReader._(process, device.displayName, logger);
1060+
return AdbLogReader._(process, device.displayName, logger, adbLogFiltering: adbLogFiltering);
10411061
}
10421062

10431063
int? _appPid;
@@ -1046,6 +1066,8 @@ class AdbLogReader extends DeviceLogReader {
10461066

10471067
final Logger _logger;
10481068

1069+
final bool adbLogFiltering;
1070+
10491071
@override
10501072
final String name;
10511073

@@ -1159,7 +1181,9 @@ class AdbLogReader extends DeviceLogReader {
11591181
if (logMatch != null) {
11601182
var acceptLine = false;
11611183

1162-
if (_fatalCrash) {
1184+
if (!adbLogFiltering) {
1185+
acceptLine = true;
1186+
} else if (_fatalCrash) {
11631187
// While a fatal crash is going on, only accept lines from the crash
11641188
// Otherwise the crash log in the console may get interrupted
11651189

‎packages/flutter_tools/lib/src/commands/attach.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class AttachCommand extends FlutterCommand {
139139
addDevToolsOptions(verboseHelp: verboseHelp);
140140
usesDeviceTimeoutOption();
141141
usesDeviceConnectionOption();
142+
usesAdbLogFilteringOption(hide: !verboseHelp);
142143
}
143144

144145
final HotRunnerFactory _hotRunnerFactory;
@@ -375,6 +376,8 @@ known, it can be explicitly provided to attach via the command-line, e.g.
375376
enableDevTools: boolArg(FlutterCommand.kEnableDevTools),
376377
ipv6: ipv6!,
377378
printDtd: boolArg(FlutterGlobalOptions.kPrintDtd, global: true),
379+
adbLogFiltering:
380+
argParser.options.containsKey('adb-log-filtering') && boolArg('adb-log-filtering'),
378381
);
379382

380383
return buildInfo.isDebug

‎packages/flutter_tools/lib/src/commands/logs.dart‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import '../android/android_device.dart';
78
import '../application_package.dart';
89
import '../base/common.dart';
910
import '../base/io.dart';
@@ -21,6 +22,7 @@ class LogsCommand extends FlutterCommand {
2122
);
2223
usesDeviceTimeoutOption();
2324
usesDeviceConnectionOption();
25+
usesAdbLogFilteringOption(hide: false);
2426
}
2527

2628
@override
@@ -62,7 +64,14 @@ class LogsCommand extends FlutterCommand {
6264
await cachedDevice.targetPlatform,
6365
);
6466

65-
final DeviceLogReader logReader = await cachedDevice.getLogReader(app: app);
67+
final bool filtering =
68+
argParser.options.containsKey('adb-log-filtering') && boolArg('adb-log-filtering');
69+
final DeviceLogReader logReader;
70+
if (cachedDevice is AndroidDevice) {
71+
logReader = await cachedDevice.getLogReader(app: app, adbLogFiltering: filtering);
72+
} else {
73+
logReader = await cachedDevice.getLogReader(app: app);
74+
}
6675

6776
globals.printStatus('Showing $logReader logs:');
6877

‎packages/flutter_tools/lib/src/commands/run.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
258258
addEnableEmbedderApiFlag(verboseHelp: verboseHelp);
259259
addEnableHcppFlag(verboseHelp: verboseHelp);
260260
addTestFlag(verboseHelp: verboseHelp);
261+
usesAdbLogFilteringOption(hide: !verboseHelp);
261262
}
262263

263264
bool get traceStartup => boolArg('trace-startup');
@@ -357,6 +358,8 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
357358
disableServiceOriginCheck: boolArg('disable-service-origin-check'),
358359
cacheStartupProfile: cacheStartupProfile,
359360
enableDds: enableDds,
361+
adbLogFiltering:
362+
argParser.options.containsKey('adb-log-filtering') && boolArg('adb-log-filtering'),
360363
dartEntrypointArgs: stringsArg('dart-entrypoint-args'),
361364
dartFlags: stringArg('dart-flags') ?? '',
362365
useTestFonts: argParser.options.containsKey('use-test-fonts') && boolArg('use-test-fonts'),

‎packages/flutter_tools/lib/src/device.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ class DebuggingOptions {
999999
this.printDtd = false,
10001000
this.webDevServerConfig,
10011001
this.testFlag = false,
1002+
this.adbLogFiltering = true,
10021003
this.iosProfileDebugger,
10031004
}) : debuggingEnabled = true,
10041005
webCrossOriginIsolation = webCrossOriginIsolation ?? webUseWasm,
@@ -1035,6 +1036,7 @@ class DebuggingOptions {
10351036
this.iosProfileDebugger,
10361037
this.traceSystrace = false,
10371038
}) : debuggingEnabled = false,
1039+
adbLogFiltering = true,
10381040
useTestFonts = false,
10391041
startPaused = false,
10401042
dartFlags = '',
@@ -1122,6 +1124,7 @@ class DebuggingOptions {
11221124
required this.ipv6,
11231125
required this.google3WorkspaceRoot,
11241126
required this.printDtd,
1127+
required this.adbLogFiltering,
11251128
this.webDevServerConfig,
11261129
this.iosProfileDebugger,
11271130
}) : testFlag = false;
@@ -1172,6 +1175,7 @@ class DebuggingOptions {
11721175
final bool printDtd;
11731176
final WebDevServerConfig? webDevServerConfig;
11741177
final bool testFlag;
1178+
final bool adbLogFiltering;
11751179

11761180
/// Whether to attach the LLDB debugger when running in profile mode on a physical iOS device.
11771181
final bool? iosProfileDebugger;
@@ -1336,6 +1340,7 @@ class DebuggingOptions {
13361340
'ipv6': ipv6,
13371341
'google3WorkspaceRoot': google3WorkspaceRoot,
13381342
'printDtd': printDtd,
1343+
'adbLogFiltering': adbLogFiltering,
13391344
// TODO(jsimmons): This field is required for backward compatibility with
13401345
// the flutter_tools binary that is currently checked into Google3.
13411346
// Remove this when that binary has been updated.
@@ -1406,6 +1411,7 @@ class DebuggingOptions {
14061411
ipv6: (json['ipv6'] as bool?) ?? false,
14071412
google3WorkspaceRoot: json['google3WorkspaceRoot'] as String?,
14081413
printDtd: (json['printDtd'] as bool?) ?? false,
1414+
adbLogFiltering: (json['adbLogFiltering'] as bool?) ?? true,
14091415
webDevServerConfig: WebDevServerConfig(
14101416
port: json['port'] is int ? json['port']! as int : 8080,
14111417
host: json['hostname'] is String ? json['hostname']! as String : 'localhost',

‎packages/flutter_tools/lib/src/resident_runner.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
88
import 'package:package_config/package_config.dart';
99
import 'package:vm_service/vm_service.dart' as vm_service;
1010

11+
import 'android/android_device.dart';
1112
import 'application_package.dart';
1213
import 'asset.dart';
1314
import 'base/command_help.dart';
@@ -311,6 +312,11 @@ class FlutterDevice {
311312
logStream = (device! as IOSDevice)
312313
.getLogReader(app: package as IOSApp?, usingCISystem: debuggingOptions.usingCISystem)
313314
.logLines;
315+
} else if (device is AndroidDevice) {
316+
logStream = (await (device! as AndroidDevice).getLogReader(
317+
app: package,
318+
adbLogFiltering: debuggingOptions.adbLogFiltering,
319+
)).logLines;
314320
} else {
315321
logStream = (await device!.getLogReader(app: package)).logLines;
316322
}

‎packages/flutter_tools/lib/src/runner/flutter_command.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ abstract final class FlutterOptions {
161161
static const kEnableImpeller = 'enable-impeller';
162162
static const kCodesignIdentity = 'codesign-identity';
163163
static const kCodesign = 'codesign';
164+
static const kAdbLogFiltering = 'adb-log-filtering';
164165
}
165166

166167
/// flutter command categories for usage.
@@ -819,6 +820,15 @@ abstract class FlutterCommand extends Command<void> {
819820
);
820821
}
821822

823+
void usesAdbLogFilteringOption({required bool hide}) {
824+
argParser.addFlag(
825+
FlutterOptions.kAdbLogFiltering,
826+
defaultsTo: true,
827+
help: 'Whether to filter ADB logcat tags/messages on the Dart side.',
828+
hide: hide,
829+
);
830+
}
831+
822832
void usesApplicationBinaryOption() {
823833
argParser.addOption(
824834
FlutterOptions.kUseApplicationBinary,

‎packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,11 @@ class FakeAndroidDevice extends Fake implements AndroidDevice {
19061906
DeviceLogReader Function()? onGetLogReader;
19071907

19081908
@override
1909-
FutureOr<DeviceLogReader> getLogReader({ApplicationPackage? app, bool includePastLogs = false}) {
1909+
FutureOr<DeviceLogReader> getLogReader({
1910+
ApplicationPackage? app,
1911+
bool includePastLogs = false,
1912+
bool adbLogFiltering = true,
1913+
}) {
19101914
if (onGetLogReader == null) {
19111915
throw UnimplementedError(
19121916
'Called getLogReader but no onGetLogReader callback was supplied in the constructor to FakeAndroidDevice.',

‎packages/flutter_tools/test/commands.shard/hermetic/daemon_test.dart‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,11 @@ class FakeAndroidDevice extends Fake implements AndroidDevice {
12181218

12191219
late DeviceLogReader logReader;
12201220
@override
1221-
FutureOr<DeviceLogReader> getLogReader({ApplicationPackage? app, bool includePastLogs = false}) =>
1222-
logReader;
1221+
FutureOr<DeviceLogReader> getLogReader({
1222+
ApplicationPackage? app,
1223+
bool includePastLogs = false,
1224+
bool adbLogFiltering = true,
1225+
}) => logReader;
12231226

12241227
ApplicationPackage? startAppPackage;
12251228
late LaunchResult launchResult;

‎packages/flutter_tools/test/commands.shard/hermetic/logs_test.dart‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,64 @@ void main() {
4242
);
4343
});
4444

45+
testUsingContext('does not try to complete exitCompleter multiple times', () async {
46+
final fakeDevice = FakeDevice('phone', deviceId);
47+
deviceManager.attachedDevices.add(fakeDevice);
48+
final termSignal = FakeProcessSignal();
49+
final intSignal = FakeProcessSignal();
50+
final command = LogsCommand(sigterm: termSignal, sigint: intSignal);
51+
final Future<void> commandFuture = createTestCommandRunner(
52+
command,
53+
).run(<String>['-d', deviceId, 'logs']);
54+
intSignal.send(1);
55+
termSignal.send(1);
56+
await pumpEventQueue(times: 5);
57+
await commandFuture;
58+
}, overrides: <Type, Generator>{Platform: () => platform, DeviceManager: () => deviceManager});
59+
4560
testUsingContext(
46-
'does not try to complete exitCompleter multiple times',
61+
'logs command parses --no-adb-log-filtering and disables filtering on device',
4762
() async {
48-
final fakeDevice = FakeDevice('phone', deviceId);
63+
final fakeDevice = FakeAndroidDevice('phone', deviceId);
4964
deviceManager.attachedDevices.add(fakeDevice);
65+
66+
expect(fakeDevice.lastPassedAdbLogFiltering, isNull);
67+
5068
final termSignal = FakeProcessSignal();
5169
final intSignal = FakeProcessSignal();
5270
final command = LogsCommand(sigterm: termSignal, sigint: intSignal);
71+
5372
final Future<void> commandFuture = createTestCommandRunner(
5473
command,
55-
).run(<String>['-d', deviceId, 'logs']);
74+
).run(<String>['-d', deviceId, 'logs', '--no-adb-log-filtering']);
75+
5676
intSignal.send(1);
57-
termSignal.send(1);
58-
await pumpEventQueue(times: 5);
5977
await commandFuture;
78+
79+
expect(fakeDevice.lastPassedAdbLogFiltering, isFalse);
6080
},
6181
overrides: <Type, Generator>{Platform: () => platform, DeviceManager: () => deviceManager},
6282
);
83+
84+
testUsingContext('logs command defaults to filtering enabled on device', () async {
85+
final fakeDevice = FakeAndroidDevice('phone', deviceId);
86+
deviceManager.attachedDevices.add(fakeDevice);
87+
88+
expect(fakeDevice.lastPassedAdbLogFiltering, isNull);
89+
90+
final termSignal = FakeProcessSignal();
91+
final intSignal = FakeProcessSignal();
92+
final command = LogsCommand(sigterm: termSignal, sigint: intSignal);
93+
94+
final Future<void> commandFuture = createTestCommandRunner(
95+
command,
96+
).run(<String>['-d', deviceId, 'logs']);
97+
98+
intSignal.send(1);
99+
await commandFuture;
100+
101+
expect(fakeDevice.lastPassedAdbLogFiltering, isTrue);
102+
}, overrides: <Type, Generator>{Platform: () => platform, DeviceManager: () => deviceManager});
63103
});
64104
}
65105

0 commit comments

Comments
 (0)