时间:2021-05-22
在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变化,无法自动继续输出日志,只能再次执行此脚本。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了python实现的分析并统计nginx日志数据功能。分享给大家供大家参考,具体如下:利用python脚本分析nginx日志内容,默认统计ip、访问
本文实例讲述了Java程序中实现调用Python脚本的方法。分享给大家供大家参考,具体如下:在程序开发中,有时候需要Java程序中调用相关Python脚本,以下
本文实例讲述了Python实现监控程序执行时间并将其写入日志的方法。分享给大家供大家参考。具体实现方法如下:#/usr/bin/python#-*-coding
这篇文章主要介绍了Oracle中实现MySQLshowindexfromtable命令SQL脚本分享,本文只是模拟了Mysql中的showindexfromta
前言最近经常用到一个.py程序,但是每次在不同电脑上用,希望能把Python脚本发布为脱离Python平台运行的可执行程序,比如单个exe。PyInstalle