在Android軟件開發(fā)中,增加日志的作用很重要,便于我們了解程序的執(zhí)行情況和數(shù)據(jù)。Eclipse開發(fā)工具會(huì)提供了可視化的工具,但是還是感覺終端效率會(huì)高一些,于是自己寫了一個(gè)python的腳本來(lái)通過包名來(lái)過濾某一程序的日志。
原理
通過包名得到對(duì)應(yīng)的進(jìn)程ID(可能多個(gè)),然后使用adb logcat 過濾進(jìn)程ID即可得到對(duì)應(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)
不足
當(dāng)腳本執(zhí)行后,Android程序如果關(guān)閉或者重新啟動(dòng),導(dǎo)致進(jìn)程ID變化,無(wú)法自動(dòng)繼續(xù)輸出日志,只能再次執(zhí)行此腳本。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

