日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python實現過濾單個Android程序日志腳本分享

系統 1928 0

在Android軟件開發中,增加日志的作用很重要,便于我們了解程序的執行情況和數據。Eclipse開發工具會提供了可視化的工具,但是還是感覺終端效率會高一些,于是自己寫了一個python的腳本來通過包名來過濾某一程序的日志。

原理

通過包名得到對應的進程ID(可能多個),然后使用adb logcat 過濾進程ID即可得到對應程序的日志。

源碼

復制代碼 代碼如下:

#!/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)

不足

當腳本執行后,Android程序如果關閉或者重新啟動,導致進程ID變化,無法自動繼續輸出日志,只能再次執行此腳本。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 彭阳县| 根河市| 神农架林区| 内江市| 中西区| 新野县| 灌阳县| 磐石市| 德保县| 黑山县| 昌江| 巴彦县| 巴林右旗| 德安县| 塔城市| 界首市| 炎陵县| 开平市| 长顺县| 东至县| 卓资县| 林西县| 同心县| 平昌县| 锡林郭勒盟| 安庆市| 新宾| 横山县| 兰考县| 富川| 于田县| 梅州市| 山丹县| 房产| 特克斯县| 吴桥县| 陕西省| 齐齐哈尔市| 故城县| 渝北区| 嘉定区|