原理
通过包名得到对应的进程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变化,无法自动继续输出日志,只能再次执行此脚本。
