Adb / Display Matching Lines
Display Matching Lines
Use 'adb logcat -e {{regex}}' to filter Android logs with regex. Essential for debugging by isolating critical issues in log output.
adb logcat -e <regex> adb logcat -e <regex> #!/bin/bash
# Display Matching Lines
adb logcat -e {{regex}} import subprocess
# Display Matching Lines
# Make sure to replace <placeholders> with actual values
def run_command():
cmd = [
"adb",
"logcat",
"-e",
"<regex>"
]
try:
print(f"Executing: {' '.join(cmd)}")
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
except FileNotFoundError:
print("Error: adb not found. Please install it first.")
if __name__ == "__main__":
run_command() When To Use
Apply 'adb logcat -e {{regex}}' when diagnosing intermittent app crashes during field tests. This approach is crucial when specific log outputs are buried under extensive logs, allowing for rapid identification of error conditions or exceptions that match your regex pattern.
Pro Tip
Watch out for regex greediness when large logs are involved. Use non-greedy patterns carefully to prevent performance degradation.
Terminal Output
Expected runtime feedback
--------- beginning of main ---------\n07-17 12:30:52.123 1234 5678 D/MyApp: Value is 42\n07-17 12:30:53.456 1234 5678 I/MyApp: User clicked button\n07-17 12:30:54.789 1234 5678 W/MyApp: Deprecated method called\n--------- end of main --------- Anatomy of Output
Understanding the result
06-11 12:45:03.123 1234 1234 E MainActivity: Error occurred Log Entry Matches when regex captures 'Error' in log output.
06-11 12:45:03.125 1234 1234 I ActivityManager: Process started Non-matching Log Skipped as it does not match regex.
06-11 12:45:03.127 1234 1234 E System: Unexpected shutdown Critical Log Captured critical error logs as specified.
06-11 12:45:03.128 1234 1234 W BatteryStatsImpl: Battery low Warning Log This will be captured if the regex includes warnings.
06-11 12:45:03.130 1234 1234 W ConnectivityService: Lost network connection Warning Log Ensure regex pattern to capture network-related logs.
06-11 12:45:03.135 1234 1234 E FragmentManager: Fragment error Fragment Error Useful to debug fragment lifecycle issues.
Total logs processed: 10000 Summary Total number of logs evaluated against regex pattern.
Total logs matched: 150 Matched Logs Count Count of all logs that matched the regex.
Unix Pipeline
Shell combinations
adb logcat | grep 'ERROR' | tee error_log.txt Intermediate logs are streamed to console and saved to a file simultaneously.
adb logcat -e 'TimeOutException' | awk '/E/' Filters for Exception logs and processes additional filtering using awk.
adb shell 'logcat -e "OutOfMemoryError"' | parallel -j2 grep 'ActivityManager' Leverages 'parallel' to enhance processing speed across multiple log patterns.
Troubleshooting
Common pitfalls
Cannot compile regex: unexpected end of expression
Solution: Check for incomplete regex patterns, particularly when escaping characters.
Illegal byte sequence
Solution: Ensure text encoding is compatible between device and host; use UTF-8 by default.
Buffer overflow detected
Solution: Optimize regex to avoid processing logs in sizes exceeding buffer limits.
Command Breakdown
What each part is doing
-
adb - Base Command
- The executable that performs this operation. Here it runs Adb before the shell applies any redirect operators.
-
<regex> - regex
- The value supplied for regex.
-
-e - Command Option
- Tool-specific option used by this command invocation.
How To Run
Execution path
- Step 1
Run the command: adb logcat -e 'MyApp'
- Step 2
Check the output for relevant log entries that match 'MyApp'
Alternative Approaches
Comparable commands in other tools
Alternative observability tools for the same job.