This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Improve, test, and fix a bug related to adb logcat filtering.
#51012
Merged
matanlurey
merged 5 commits into
flutter:main
from
matanlurey:improve-and-test-adb-log-filtering
Feb 27, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import 'package:litetest/litetest.dart'; | ||
|
|
||
| import '../bin/utils/adb_logcat_filtering.dart'; | ||
| import 'src/fake_adb_logcat.dart'; | ||
|
|
||
| void main() { | ||
| /// Simulates the filtering of logcat output [lines]. | ||
| Iterable<String> filter(Iterable<String> lines, {int? filterProcessId}) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: does this really belong in its own testable class? Right now this looks like it's roughly copied from the run_android_test.dart
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll file a follow-up issue, as it really depends on how much more work I'm going to do on the CLI: |
||
| if (lines.isEmpty) { | ||
| throw StateError('No log lines to filter. This is unexpected.'); | ||
| } | ||
| return lines.where((String line) { | ||
| final AdbLogLine? logLine = AdbLogLine.tryParse(line); | ||
| if (logLine == null) { | ||
| throw StateError('Invalid log line: $line'); | ||
| } | ||
| final bool isVerbose = logLine.isVerbose(filterProcessId: filterProcessId?.toString()); | ||
| return !isVerbose; | ||
| }); | ||
| } | ||
|
|
||
| test('should always retain fatal logs', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| process.fatal('Something', 'A bad thing happened'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain()); | ||
| expect(filtered, hasLength(1)); | ||
| expect(filtered.first, contains('Something: A bad thing happened')); | ||
| }); | ||
|
|
||
| test('should never retain debug logs', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| final String tag = AdbLogLine.kKnownNoiseTags.first; | ||
| process.debug(tag, 'A debug message'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain()); | ||
| expect(filtered, isEmpty); | ||
| }); | ||
|
|
||
| test('should never retain logs from known "noise" tags', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| final String tag = AdbLogLine.kKnownNoiseTags.first; | ||
| process.info(tag, 'Flutter flutter flutter'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain()); | ||
| expect(filtered, isEmpty); | ||
| }); | ||
|
|
||
| test('should always retain logs from known "useful" tags', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| final String tag = AdbLogLine.kKnownUsefulGeneralTags.first; | ||
| process.info(tag, 'A useful message'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain()); | ||
| expect(filtered, hasLength(1)); | ||
| expect(filtered.first, contains('$tag: A useful message')); | ||
| }); | ||
|
|
||
| test('if a process ID is passed, retain the log', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| process.info('SomeTag', 'A message'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain(), filterProcessId: process.processId); | ||
| expect(filtered, hasLength(1)); | ||
| expect(filtered.first, contains('SomeTag: A message')); | ||
| }); | ||
|
|
||
| test('even if a process ID passed, retain logs containing "flutter"', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| process.info('SomeTag', 'A message with flutter'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain(), filterProcessId: process.processId); | ||
| expect(filtered, hasLength(1)); | ||
| expect(filtered.first, contains('SomeTag: A message with flutter')); | ||
| }); | ||
|
|
||
| test('should retain E-level flags from known "useful" error tags', () { | ||
| final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| final FakeAdbProcess process = logcat.withProcess(); | ||
| final String tag = AdbLogLine.kKnownUsefulErrorTags.first; | ||
| process.error(tag, 'An error message'); | ||
| process.info(tag, 'An info message'); | ||
|
|
||
| final Iterable<String> filtered = filter(logcat.drain()); | ||
| expect(filtered, hasLength(1)); | ||
| expect(filtered.first, contains('$tag: An error message')); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import '../../bin/utils/adb_logcat_filtering.dart'; | ||
|
|
||
| /// Simulates the output of `adb logcat`, i.e. for testing. | ||
| /// | ||
| /// ## Example | ||
| /// | ||
| /// ```dart | ||
| /// final FakeAdbLogcat logcat = FakeAdbLogcat(); | ||
| /// final FakeAdbProcess process = logcat.withProcess(); | ||
| /// process.info('ActivityManager', 'Force stopping dev.flutter.scenarios appid=10226 user=0: start instr'); | ||
| /// // ... | ||
| /// final List<String> logLines = logcat.drain(); | ||
| /// // ... | ||
| /// ``` | ||
| final class FakeAdbLogcat { | ||
| final List<String> _lines = <String>[]; | ||
| final Map<int, FakeAdbProcess> _processById = <int, FakeAdbProcess>{}; | ||
|
|
||
| /// The current date and time. | ||
| DateTime _now = DateTime.now(); | ||
|
|
||
| /// Returns the date and time for the next log line. | ||
| /// | ||
| /// Time is progressed by 1 second each time this method is called. | ||
| DateTime _progressTime({Duration by = const Duration(seconds: 1)}) { | ||
| _now = _now.add(by); | ||
| return _now; | ||
| } | ||
|
|
||
| /// `02-22 13:54:39.839` | ||
| static String _formatTime(DateTime time) { | ||
| return '${time.month.toString().padLeft(2, '0')}-' | ||
| '${time.day.toString().padLeft(2, '0')} ' | ||
| '${time.hour.toString().padLeft(2, '0')}:' | ||
| '${time.minute.toString().padLeft(2, '0')}:' | ||
| '${time.second.toString().padLeft(2, '0')}.' | ||
| '${time.millisecond.toString().padLeft(3, '0')}'; | ||
| } | ||
|
|
||
| void _write({ | ||
| required int processId, | ||
| required int threadId, | ||
| required String severity, | ||
| required String tag, | ||
| required String message, | ||
| }) { | ||
| final DateTime time = _progressTime(); | ||
| final String line = '${_formatTime(time)} $processId $threadId $severity $tag: $message'; | ||
| assert(AdbLogLine.tryParse(line) != null, 'Invalid log line: $line'); | ||
| _lines.add(line); | ||
| } | ||
|
|
||
| /// Drains the stored log lines and returns them. | ||
| List<String> drain() { | ||
| final List<String> result = List<String>.from(_lines); | ||
| _lines.clear(); | ||
| return result; | ||
| } | ||
|
|
||
| /// Creates a new process writing to this logcat. | ||
| /// | ||
| /// Optionally specify a [processId] to use for the process, otherwise a | ||
| /// simple default is used (sequential numbers starting from 1000). | ||
| FakeAdbProcess withProcess({int? processId}) { | ||
| processId ??= 1000 + _processById.length; | ||
| return _processById.putIfAbsent( | ||
| processId, | ||
| () => _createProcess(processId: processId!), | ||
| ); | ||
| } | ||
|
|
||
| FakeAdbProcess _createProcess({required int processId}) { | ||
| return FakeAdbProcess._(this, processId: processId); | ||
| } | ||
| } | ||
|
|
||
| /// A stateful fixture that represents a fake process writing to `adb logcat`. | ||
| /// | ||
| /// See [FakeAdbLogcat.withProcess] for how to create this fixture. | ||
| final class FakeAdbProcess { | ||
| const FakeAdbProcess._( | ||
| this._logcat, { | ||
| required this.processId, | ||
| }); | ||
|
|
||
| final FakeAdbLogcat _logcat; | ||
|
|
||
| /// The process ID of this process. | ||
| final int processId; | ||
|
|
||
| /// Writes a debug log message. | ||
| void debug(String tag, String message, {int threadId = 1}) { | ||
| _logcat._write( | ||
| processId: processId, | ||
| threadId: threadId, | ||
| severity: 'D', | ||
| tag: tag, | ||
| message: message, | ||
| ); | ||
| } | ||
|
|
||
| /// Writes an info log message. | ||
| void info(String tag, String message, {int threadId = 1}) { | ||
| _logcat._write( | ||
| processId: processId, | ||
| threadId: threadId, | ||
| severity: 'I', | ||
| tag: tag, | ||
| message: message, | ||
| ); | ||
| } | ||
|
|
||
| /// Writes a warning log message. | ||
| void warning(String tag, String message, {int threadId = 1}) { | ||
| _logcat._write( | ||
| processId: processId, | ||
| threadId: threadId, | ||
| severity: 'W', | ||
| tag: tag, | ||
| message: message, | ||
| ); | ||
| } | ||
|
|
||
| /// Writes an error log message. | ||
| void error(String tag, String message, {int threadId = 1}) { | ||
| _logcat._write( | ||
| processId: processId, | ||
| threadId: threadId, | ||
| severity: 'E', | ||
| tag: tag, | ||
| message: message, | ||
| ); | ||
| } | ||
|
|
||
| /// Writes a fatal log message. | ||
| void fatal(String tag, String message, {int threadId = 1}) { | ||
| _logcat._write( | ||
| processId: processId, | ||
| threadId: threadId, | ||
| severity: 'F', | ||
| tag: tag, | ||
| message: message, | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Is this really a noise tag? This gets dumped if a test hangs and tells you what the threads are doing right? Is it used otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼 I moved this from
kKnownNoisetokKnownUsefulError.