Skip to content

Commit 247e193

Browse files
committed
修复低 Android 版本无法过滤指定应用的 Logcat 的问题
1 parent bd35462 commit 247e193

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

shell/device-tools/DisplayLogcat.sh

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ displayLogcatSingleDevice() {
4545
local deviceId=$1
4646
if [[ -z ${packageName} ]]; then
4747
adb -s "${deviceId}" logcat < /dev/null
48-
else
48+
return
49+
fi
50+
51+
local androidVersionCode
52+
androidVersionCode=$(getAndroidVersionCodeByAdb "${deviceId}")
53+
54+
if (( androidVersionCode >= 24 )); then
4955
local uid
50-
if (( $(getAndroidVersionCodeByAdb "${deviceId}") >= 26 )); then
56+
if (( androidVersionCode >= 26 )); then
5157
uid=$(adb -s "${deviceId}" shell pm list packages -U < /dev/null 2>/dev/null | grep "${packageName}" | awk -F 'uid:' '{print $2}')
5258
fi
5359

@@ -64,10 +70,23 @@ displayLogcatSingleDevice() {
6470
echo "📝 设备支持 uid 过滤,使用原生过滤的方式(UID: ${uid}"
6571
adb -s "${deviceId}" logcat --uid "${uid}" < /dev/null
6672
else
67-
echo "💡 设备不支持 uid 过滤,使用文本过滤的方式(UID: ${uid}"
73+
echo "📝 设备不支持 uid 过滤,使用文本过滤的方式(UID: ${uid}"
6874
adb -s "${deviceId}" logcat -v uid < /dev/null | grep -F " ${uid} "
6975
fi
76+
return
77+
fi
78+
79+
local pid
80+
pid=$(adb -s "${deviceId}" shell ps < /dev/null 2>/dev/null | tr -d '\r' | awk -v pkg="${packageName}" '$NF ~ ("^" pkg "(:.*)?$") {print $2; exit}')
81+
if [[ -z "${pid}" || ! "${pid}" =~ ^[0-9]+$ ]]; then
82+
pid=$(adb -s "${deviceId}" shell ps -A < /dev/null 2>/dev/null | tr -d '\r' | awk -v pkg="${packageName}" '$NF ~ ("^" pkg "(:.*)?$") {print $2; exit}')
83+
fi
84+
if [[ -z "${pid}" || ! "${pid}" =~ ^[0-9]+$ ]]; then
85+
echo "❌ 无法解析该应用的 PID,请检查 ${packageName} 应用是否正在运行"
86+
return 1
7087
fi
88+
echo "📝 低版本设备,使用 pid 文本过滤的方式(PID: ${pid}"
89+
adb -s "${deviceId}" logcat -v threadtime < /dev/null | awk -v pid="${pid}" '$3==pid'
7190
}
7291

7392
displayLogcatForDevice() {

0 commit comments

Comments
 (0)