Skip to content

Commit e3d6ca7

Browse files
committed
Resolve issue flutter#120711: Add --[no-]adb-log-filtering flag to bypass logcat tag/message filtering
1 parent 3e4e8b3 commit e3d6ca7

7 files changed

Lines changed: 88 additions & 5 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,15 @@ class AndroidMemoryInfo extends MemoryInfo {
10521052

10531053
/// A log reader that logs from `adb logcat`.
10541054
class AdbLogReader extends DeviceLogReader {
1055-
AdbLogReader._(this._adbProcess, this.name, this._logger);
1055+
AdbLogReader._(this._adbProcess, this.name, this._logger, {this.adbLogFiltering = true});
10561056

10571057
@visibleForTesting
1058-
factory AdbLogReader.test(Process adbProcess, String name, Logger logger) = AdbLogReader._;
1058+
factory AdbLogReader.test(
1059+
Process adbProcess,
1060+
String name,
1061+
Logger logger, {
1062+
bool adbLogFiltering,
1063+
}) = AdbLogReader._;
10591064

10601065
/// Create a new [AdbLogReader] from an [AndroidDevice] instance.
10611066
static Future<AdbLogReader> createLogReader(
@@ -1090,7 +1095,12 @@ class AdbLogReader extends DeviceLogReader {
10901095
]);
10911096
}
10921097
final Process process = await processManager.start(device.adbCommandForDevice(args));
1093-
return AdbLogReader._(process, device.displayName, logger);
1098+
return AdbLogReader._(
1099+
process,
1100+
device.displayName,
1101+
logger,
1102+
adbLogFiltering: device.adbLogFiltering,
1103+
);
10941104
}
10951105

10961106
int? _appPid;
@@ -1099,6 +1109,8 @@ class AdbLogReader extends DeviceLogReader {
10991109

11001110
final Logger _logger;
11011111

1112+
final bool adbLogFiltering;
1113+
11021114
@override
11031115
final String name;
11041116

@@ -1209,7 +1221,9 @@ class AdbLogReader extends DeviceLogReader {
12091221
if (logMatch != null) {
12101222
var acceptLine = false;
12111223

1212-
if (_fatalCrash) {
1224+
if (!adbLogFiltering) {
1225+
acceptLine = true;
1226+
} else if (_fatalCrash) {
12131227
// While a fatal crash is going on, only accept lines from the crash
12141228
// Otherwise the crash log in the console may get interrupted
12151229

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

Lines changed: 1 addition & 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;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class LogsCommand extends FlutterCommand {
2121
);
2222
usesDeviceTimeoutOption();
2323
usesDeviceConnectionOption();
24+
usesAdbLogFilteringOption(hide: false);
2425
}
2526

2627
@override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
244244
addEnableEmbedderApiFlag(verboseHelp: verboseHelp);
245245
addEnableHcppFlag(verboseHelp: verboseHelp);
246246
addTestFlag(verboseHelp: verboseHelp);
247+
usesAdbLogFilteringOption(hide: !verboseHelp);
247248
}
248249

249250
bool get traceStartup => boolArg('trace-startup');

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,10 @@ abstract class Device {
637637

638638
bool get isWirelesslyConnected => connectionInterface == DeviceConnectionInterface.wireless;
639639

640+
/// Whether to filter ADB logcat tags/messages on the Dart side.
641+
/// Only used by Android devices.
642+
bool adbLogFiltering = true;
643+
640644
String get name;
641645

642646
String get displayName {

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ abstract final class FlutterOptions {
153153
static const kEnableImpeller = 'enable-impeller';
154154
static const kCodesignIdentity = 'codesign-identity';
155155
static const kCodesign = 'codesign';
156+
static const kAdbLogFiltering = 'adb-log-filtering';
156157
}
157158

158159
/// flutter command categories for usage.
@@ -849,6 +850,15 @@ abstract class FlutterCommand extends Command<void> {
849850
);
850851
}
851852

853+
void usesAdbLogFilteringOption({required bool hide}) {
854+
argParser.addFlag(
855+
FlutterOptions.kAdbLogFiltering,
856+
defaultsTo: true,
857+
help: 'Whether to filter ADB logcat tags/messages on the Dart side.',
858+
hide: hide,
859+
);
860+
}
861+
852862
void usesApplicationBinaryOption() {
853863
argParser.addOption(
854864
FlutterOptions.kUseApplicationBinary,
@@ -2044,10 +2054,17 @@ abstract class FlutterCommand extends Command<void> {
20442054
Future<List<Device>?> findAllTargetDevices({
20452055
bool includeDevicesUnsupportedByProject = false,
20462056
}) async {
2047-
return _targetDevices.findAllTargetDevices(
2057+
final List<Device>? devices = await _targetDevices.findAllTargetDevices(
20482058
deviceDiscoveryTimeout: deviceDiscoveryTimeout,
20492059
includeDevicesUnsupportedByProject: includeDevicesUnsupportedByProject,
20502060
);
2061+
if (devices != null && argParser.options.containsKey(FlutterOptions.kAdbLogFiltering)) {
2062+
final bool filtering = boolArg(FlutterOptions.kAdbLogFiltering);
2063+
for (final Device device in devices) {
2064+
device.adbLogFiltering = filtering;
2065+
}
2066+
}
2067+
return devices;
20512068
}
20522069

20532070
/// Find and return the target [Device] based upon currently connected

‎packages/flutter_tools/test/general.shard/android/android_device_test.dart‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,51 @@ Uptime: 441088659 Realtime: 521464097
546546
},
547547
overrides: <Type, Generator>{Logger: () => BufferLogger.test()},
548548
);
549+
550+
testWithoutContext('AdbLogReader filters logs by default (adbLogFiltering = true)', () async {
551+
final logger = BufferLogger.test();
552+
final logReader = AdbLogReader.test(
553+
FakeProcess(
554+
stdout: utf8.encode(
555+
'05-12 00:00:00.000 I/some_tag( 123): secret log\n'
556+
'05-12 00:00:00.000 I/flutter( 123): allowed log\n',
557+
),
558+
),
559+
'foo',
560+
logger,
561+
);
562+
final List<String> receivedLines = [];
563+
final StreamSubscription<String> subscription = logReader.logLines.listen(receivedLines.add);
564+
565+
await Future<void>.delayed(Duration.zero);
566+
await subscription.cancel();
567+
568+
expect(receivedLines, isNot(contains('I/some_tag( 123): secret log')));
569+
expect(receivedLines, contains('I/flutter( 123): allowed log'));
570+
});
571+
572+
testWithoutContext('AdbLogReader does not filter logs if adbLogFiltering = false', () async {
573+
final logger = BufferLogger.test();
574+
final logReader = AdbLogReader.test(
575+
FakeProcess(
576+
stdout: utf8.encode(
577+
'05-12 00:00:00.000 I/some_tag( 123): secret log\n'
578+
'05-12 00:00:00.000 I/flutter( 123): allowed log\n',
579+
),
580+
),
581+
'foo',
582+
logger,
583+
adbLogFiltering: false,
584+
);
585+
final List<String> receivedLines = [];
586+
final StreamSubscription<String> subscription = logReader.logLines.listen(receivedLines.add);
587+
588+
await Future<void>.delayed(Duration.zero);
589+
await subscription.cancel();
590+
591+
expect(receivedLines, contains('I/some_tag( 123): secret log'));
592+
expect(receivedLines, contains('I/flutter( 123): allowed log'));
593+
});
549594
}
550595

551596
/// A mock VM Service that throws a generic [RPCErrorKind.kServerError] error

0 commit comments

Comments
 (0)