Python實(shí)現(xiàn)過(guò)濾單個(gè)Android程序日志腳本分享
在Android軟件開(kāi)發(fā)中,增加日志的作用很重要,便于我們了解程序的執(zhí)行情況和數(shù)據(jù)。Eclipse開(kāi)發(fā)工具會(huì)提供了可視化的工具,但是還是感覺(jué)終端效率會(huì)高一些,于是自己寫(xiě)了一個(gè)python的腳本來(lái)通過(guò)包名來(lái)過(guò)濾某一程序的日志。
原理
通過(guò)包名得到對(duì)應(yīng)的進(jìn)程ID(可能多個(gè)),然后使用adb logcat 過(guò)濾進(jìn)程ID即可得到對(duì)應(yīng)程序的日志。
源碼
#!/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)
不足
當(dāng)腳本執(zhí)行后,Android程序如果關(guān)閉或者重新啟動(dòng),導(dǎo)致進(jìn)程ID變化,無(wú)法自動(dòng)繼續(xù)輸出日志,只能再次執(zhí)行此腳本。
- 通過(guò)Python 獲取Android設(shè)備信息的輕量級(jí)框架
- 基于Python的Android圖形解鎖程序詳解
- python獲取android設(shè)備的GPS信息腳本分享
- Python實(shí)現(xiàn)刪除Android工程中的冗余字符串
- python服務(wù)器與android客戶端socket通信實(shí)例
- 使用python編寫(xiě)批量卸載手機(jī)中安裝的android應(yīng)用腳本
- 使用python編寫(xiě)android截屏腳本雙擊運(yùn)行即可
- python讀取Android permission文件
- 用Python腳本生成Android SALT擾碼的方法
- python搭建服務(wù)器實(shí)現(xiàn)兩個(gè)Android客戶端間收發(fā)消息
相關(guān)文章
用Python解決計(jì)數(shù)原理問(wèn)題的方法
計(jì)數(shù)原理是數(shù)學(xué)中的重要研究對(duì)象之一,分類(lèi)加法計(jì)數(shù)原理、分步乘法計(jì)數(shù)原理是解決計(jì)數(shù)問(wèn)題的最基本、最重要的方法,也稱(chēng)為基本計(jì)數(shù)原理,它們?yōu)榻鉀Q很多實(shí)際問(wèn)題提供了思想和工具。本文教大家怎么用Python解決在數(shù)學(xué)中遇到的計(jì)數(shù)原理問(wèn)題。2016-08-08
Python中PyQt5可視化界面通過(guò)拖拽來(lái)上傳文件的實(shí)現(xiàn)
本文主要介紹了Python中PyQt5可視化界面通過(guò)拖拽來(lái)上傳文件的實(shí)現(xiàn),通過(guò)構(gòu)建一個(gè)可接受拖拽的區(qū)域,并重寫(xiě)相關(guān)事件處理函數(shù),可以方便地實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
python 使用Tensorflow訓(xùn)練BP神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)鳶尾花分類(lèi)
這篇文章主要介紹了python 使用Tensorflow訓(xùn)練BP神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)鳶尾花分類(lèi),幫助大家更好的利用python進(jìn)行深度學(xué)習(xí),感興趣的朋友可以了解下2021-05-05
Django生成數(shù)據(jù)庫(kù)及添加用戶報(bào)錯(cuò)解決方案
這篇文章主要介紹了Django生成數(shù)據(jù)庫(kù)及添加用戶報(bào)錯(cuò)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
python中subprocess實(shí)例用法及知識(shí)點(diǎn)詳解
在本篇文章里小編給大家分享的是關(guān)于python中subprocess實(shí)例用法及知識(shí)點(diǎn)詳解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)下。2021-10-10
使用Python進(jìn)行中文繁簡(jiǎn)轉(zhuǎn)換的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用Python進(jìn)行中文繁簡(jiǎn)轉(zhuǎn)換的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python入門(mén)教程(二十五)Python的作用域
這篇文章主要介紹了Python入門(mén)教程(二十五)Python的作用域,Python是一門(mén)非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門(mén)教程,需要的朋友可以參考下2023-04-04

