adb logcat二进制(-B)模式是否忽略filterspecs?能否应用过滤规则?
Hey there, let's break down your question about logcat's binary mode and filter specs.
You mentioned running these two commands in adb shell:
logcat -B -s MyTag logcat -B MyTag:I *:S
And you noticed that even though you intended to only receive logs tagged MyTag in the binary stream, it seems like the filterspecs are being ignored in binary mode. Plus you didn't find any related logic in logcat.cpp that would explain this behavior.
核心结论
The short answer is yes, filterspecs should work with binary mode (-B)—but the filtering logic isn't where you might expect it to be.
背后的逻辑说明
Here's what's happening under the hood:
- When you use
-B, logcat switches to outputting logs in the compact binary format used by Android's internal logging system (this format preserves more metadata than plain text). - The filtering doesn't happen in
logcat.cppfor binary mode. Instead, the logd daemon (Android's system logging service) handles applying the filters before sending logs to logcat. That's why you won't see the filtering code in logcat.cpp itself.
为什么你可能觉得过滤没生效?
If you're seeing unfiltered logs in binary mode, it could be due to a few edge cases:
- Case sensitivity: Double-check that the tag
MyTagexactly matches what's being logged—tags are case-sensitive, somytagorMYTAGwon't match. - Android version bugs: Some older Android SDK versions had issues where binary mode filtering didn't work as intended. Try testing on a newer device or emulator to rule this out.
- Post-capture parsing issues: If you're saving the binary logs to a file and parsing them later, make sure your parsing tool isn't ignoring the filtered results (though this is less likely if you're viewing output directly).
验证过滤是否生效的方法
To confirm if your filters are actually being applied:
- First run the same filter without
-Bto verify it works in text mode (e.g.,logcat -s MyTag). - Capture binary logs to a file:
logcat -B MyTag:I *:S > binary_logs.bin - Convert the binary file back to text:
logcat -d -B -f binary_logs.bin
You should only see logs taggedMyTagin the converted text output if the filter worked correctly.
内容的提问来源于stack exchange,提问作者Youngjae

