Python實現(xiàn)過濾單個Android程序日志腳本分享
在Android軟件開發(fā)中,增加日志的作用很重要,便于我們了解程序的執(zhí)行情況和數(shù)據(jù)。Eclipse開發(fā)工具會提供了可視化的工具,但是還是感覺終端效率會高一些,于是自己寫了一個python的腳本來通過包名來過濾某一程序的日志。
原理
通過包名得到對應(yīng)的進程ID(可能多個),然后使用adb logcat 過濾進程ID即可得到對應(yīng)程序的日志。
源碼
#!/usr/bin/env python
#coding:utf-8
#This script is aimed to grep logs by application(User should input a packageName and then we look up for the process ids then separate logs by process ids).
import os
import sys
packageName=str(sys.argv[1])
command = "adb shell ps | grep %s | awk '{print $2}'"%(packageName)
p = os.popen(command)
##for some applications,there are multiple processes,so we should get all the process id
pid = p.readline().strip()
filters = pid
while(pid != ""):
pid = p.readline().strip()
if (pid != ''):
filters = filters + "|" + pid
#print 'command = %s;filters=%s'%(command, filters)
if (filters != '') :
cmd = 'adb logcat | grep --color=always -E "%s" '%(filters)
os.system(cmd)
使用方法
python logcatPkg.py com.mx.browser
最新代碼
#!/usr/bin/env python
#coding:utf-8
#This script is aimed to grep logs by application(User should input a packageName and then we look up for the process ids then separate logs by process ids).
import os
import sys
packageName=str(sys.argv[1])
command = "adb shell ps | grep %s | awk '{print $2}'"%(packageName)
p = os.popen(command)
##for some applications,there are multiple processes,so we should get all the process id
pid = p.readline().strip()
filters = pid
while(pid != ""):
pid = p.readline().strip()
if (pid != ''):
filters = filters + "|" + pid
#print 'command = %s;filters=%s'%(command, filters)
if (filters != '') :
cmd = 'adb logcat | grep --color=always -E "%s" '%(filters)
os.system(cmd)
不足
當腳本執(zhí)行后,Android程序如果關(guān)閉或者重新啟動,導(dǎo)致進程ID變化,無法自動繼續(xù)輸出日志,只能再次執(zhí)行此腳本。
- 通過Python 獲取Android設(shè)備信息的輕量級框架
- 基于Python的Android圖形解鎖程序詳解
- python獲取android設(shè)備的GPS信息腳本分享
- Python實現(xiàn)刪除Android工程中的冗余字符串
- python服務(wù)器與android客戶端socket通信實例
- 使用python編寫批量卸載手機中安裝的android應(yīng)用腳本
- 使用python編寫android截屏腳本雙擊運行即可
- python讀取Android permission文件
- 用Python腳本生成Android SALT擾碼的方法
- python搭建服務(wù)器實現(xiàn)兩個Android客戶端間收發(fā)消息
相關(guān)文章
Python中PyQt5可視化界面通過拖拽來上傳文件的實現(xiàn)
本文主要介紹了Python中PyQt5可視化界面通過拖拽來上傳文件的實現(xiàn),通過構(gòu)建一個可接受拖拽的區(qū)域,并重寫相關(guān)事件處理函數(shù),可以方便地實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的可以了解一下2023-12-12
python 使用Tensorflow訓(xùn)練BP神經(jīng)網(wǎng)絡(luò)實現(xiàn)鳶尾花分類
這篇文章主要介紹了python 使用Tensorflow訓(xùn)練BP神經(jīng)網(wǎng)絡(luò)實現(xiàn)鳶尾花分類,幫助大家更好的利用python進行深度學習,感興趣的朋友可以了解下2021-05-05
Django生成數(shù)據(jù)庫及添加用戶報錯解決方案
這篇文章主要介紹了Django生成數(shù)據(jù)庫及添加用戶報錯解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
使用Python進行中文繁簡轉(zhuǎn)換的實現(xiàn)代碼
這篇文章主要介紹了使用Python進行中文繁簡轉(zhuǎn)換的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10

