android 过滤日志,Android过滤具体应用日志的脚本

最近新修改完善了一个Android脚本,贴出来和大家分享一下。

功能:按照程序(包名)过滤某一程序的日志,便于更加准确定位问题。

原理:根据报名找到进程ID,然后根据得到的进程ID过滤

代码:

#!/usr/bin/env python

#coding:utf-8

#author:andrewallanwallace@gmail.com

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

假设上述脚本保存成logcatPkg.py 使用方法:

androidyue~/py_works (master)$ logcatPkg.py com.android.phone

结果:

E/ActivityManager(  190):  0% 268/com.android.phone: 0% user + 0% kernel / faults: 210 minor 1 major

I/klogd  (  169): [69429.226898] BATT: Charging

D/WindowManager(  255): MotionEvent obtain 1 : MotionEvent{2b0a3268 action=1 x=66.70892 y=21.861084 pressure=1.0 size=0.0}

D/WindowManager(  255): MotionEvent obtain 1 : MotionEvent{2b0a3268 action=0 x=449.95306 y=18.399263 pressure=1.0 size=0.0}

D/WindowManager(  255): MotionEvent obtain 1 : MotionEvent{2b0a3268 action=1 x=366.57278 y=231.83072 pressure=1.0 size=0.0}

D/PowerManagerService(  190): setTimeoutLocked now=69520831 timeoutOverride=-1 nextState=3 when=69526831

D/WindowManager(  255): MotionEvent obtain 1 : MotionEvent{2b0a3268 action=0 x=360.5634 y=87.862915 pressure=1.0 size=0.0}

D/WindowManager(  255): MotionEvent obtain 1 : MotionEvent{2b0a3268 action=1 x=403.3803 y=185.80405 pressure=1.0 size=0.0}

D/WindowManager(  255): MotionEvent obtain 1 : MotionEvent{2b0a3268 action=0 x=323.7559 y=58.424103 pressure=1.0 size=0.0}

D/jdwp    (  268): adbd disconnected

I/klogd  (  169): [69526.400268] [mlog] do_log_poweroncause()

D/jdwp    (  268): adbd disconnected

注意:由于是根据pid过滤,所以可能会过滤出一些数字相关的无相关信息。

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值