国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-23 06:18:32
字體:
來源:轉載
供稿:網友

在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變化,無法自動繼續輸出日志,只能再次執行此腳本。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临安市| 河池市| 永清县| 迁安市| 澄迈县| 武安市| 屏东市| 进贤县| 金湖县| 鹤峰县| 吉林市| 林周县| 镇安县| 大庆市| 安国市| 恩施市| 丘北县| 凌源市| 玉林市| 镶黄旗| 松原市| 福泉市| 常熟市| 永年县| 罗城| 集贤县| 锡林浩特市| 岐山县| 洪湖市| 确山县| 马公市| 孝昌县| 临西县| 武汉市| 聊城市| 丹棱县| 浏阳市| 云林县| 潍坊市| 观塘区| 淮安市|