Android過濾具體應用日誌的指令碼

來源:互聯網
上載者:User

最近新修改完善了一個指令碼,貼出來和大家分享一下。

功能:按照程式(包名)過濾某一程式的日誌,便於更加準確定位問題。

原理:根據報名找到進程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 osimport syspackageName=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 idpid = p.readline().strip()filters = pidwhile(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 majorI/klogd   (  169): [69429.226898] BATT: ChargingD/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=69526831D/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 disconnectedI/klogd   (  169): [69526.400268] [mlog] do_log_poweroncause()D/jdwp    (  268): adbd disconnected

注意:由於是根據pid過濾,所以可能會過濾出一些數字相關的無相關資訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.