python 監(jiān)控logcat關(guān)鍵字功能
本文主要介紹使用Python調(diào)用ADB命令實(shí)現(xiàn)實(shí)時(shí)監(jiān)控logcat關(guān)鍵字的功能
采用多進(jìn)程,可同時(shí)監(jiān)控多個(gè)設(shè)備,監(jiān)控多個(gè)關(guān)鍵字。
需要配置ADB環(huán)境,具體配置就不多介紹,隨便搜一下一大把,直接上代碼
通過(guò)一個(gè)全局變量控制開(kāi)啟和關(guān)閉監(jiān)控功能, INSTRUCTION 用于根據(jù)指令獲取對(duì)應(yīng)的方法名
import os, threading, datetime # 獲取當(dāng)前文件所在目錄,拼接出LOG路徑LOG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'log') # 配置需要監(jiān)控的關(guān)鍵字KEYWORDS = ['ANR ', 'NullPointerException', 'CRASH', 'Force Closed'] # 控制開(kāi)啟和關(guān)閉STOP_LOGCAT = True # 指令對(duì)應(yīng)具體操作INSTRUCTION = { '1': 'filter_keywords', '2': 'stop_filter_keywords', '3': 'exit' } def filter_keywords(): global STOP_LOGCAT STOP_LOGCAT = False devices = get_devices() # 先獲取所有連接的設(shè)備 print('開(kāi)始監(jiān)控關(guān)鍵字') for device in devices: t = threading.Thread(target=filter_keyword, args=(device,)) t.start() def stop_filter_keywords(): global STOP_LOGCAT if STOP_LOGCAT: print('沒(méi)有正在執(zhí)行的任務(wù)n') else: STOP_LOGCAT = True print('正在停止關(guān)鍵字監(jiān)控n')
監(jiān)控關(guān)鍵字主函數(shù),
def filter_keyword(device): print('設(shè)備%s關(guān)鍵字監(jiān)控已開(kāi)啟' % str(device)) sub = logcat(device) with sub: for line in sub.stdout: # 子進(jìn)程會(huì)持續(xù)輸出日志,對(duì)子進(jìn)程對(duì)象.stdout進(jìn)行循環(huán)讀取 for key in KEYWORDS: if line.decode('utf-8').find(key) != -1: # stdout輸出為字節(jié)類(lèi)型,需要轉(zhuǎn)碼 message = '設(shè)備:%s 檢測(cè)到:%sn' % (device, key)# 設(shè)備:192.168.56.104:5555 檢測(cè)到:ANR path = get_log_path('bugreport') # 根據(jù)時(shí)間創(chuàng)建文件夾 bugreport(device, path)# 拉取完整日志壓縮包到創(chuàng)建的文件夾內(nèi) send_message(message) # 這里可以換成自己要做的事情,比如發(fā)送郵件或釘釘通知 if STOP_LOGCAT: break print('設(shè)備%s關(guān)鍵字監(jiān)控已停止' % str(device)) sub.kill()
通過(guò) subprocess.Popen 創(chuàng)建進(jìn)程執(zhí)行命令,持續(xù)輸出日志到 stdout
# logcat持續(xù)輸出日志def logcat(device): command = 'adb -s ' + str(device) + ' logcat -v time' sub = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return sub
獲取所有已連接設(shè)備的方法,執(zhí)行'adb devices'后輸出如下,通過(guò)對(duì)命令執(zhí)行拿到的字符串切割獲取所有設(shè)備號(hào)以列表方式存儲(chǔ)
# 獲取所有devicedef get_devices(): command = 'adb devices' res = os.popen(command).read() devices = [] res = res.split('n') for i in res: if i.endswith('device'): devices.append(i.split(’t’)[0]) return devices
# 打包下載所有日志到當(dāng)前目錄def bugreport(device, path): os.chdir(path)# bugreport會(huì)下載日志到當(dāng)前文件夾,所以需要先切換到已經(jīng)創(chuàng)建的目錄 command = 'adb -s ' + str(device) + ' bugreport' subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1) print('設(shè)備:%s 日志路徑:%s' % (str(device), path))
以 當(dāng)前文件所在目錄/年/月/日 格式獲取日志路徑,如果不存在自動(dòng)創(chuàng)建
# 獲取日志存放路徑,如果不存在則按日期創(chuàng)建def get_log_path(tag): year = datetime.datetime.now().strftime(’%Y’) month = datetime.datetime.now().strftime(’%m’) day = datetime.datetime.now().strftime(’%d’) path = os.path.join(LOG_PATH, tag, year, month, day) if not os.path.exists(path): os.makedirs(path) return path
main函數(shù),循環(huán)接收指令,根據(jù)接收的指令拿到方法名,并通過(guò)eval()方法執(zhí)行。
def main(): while True: print('-' * 100) print('1:開(kāi)啟關(guān)鍵字監(jiān)控n2:停止關(guān)鍵字監(jiān)控n3:退出') print('-' * 100) instruction = str(input('nn請(qǐng)輸入要進(jìn)行的操作號(hào):n')) print('-' * 100) while instruction not in INSTRUCTION.keys(): instruction = str(input('nn輸入無(wú)效,請(qǐng)重新輸入:')) if int(instruction) == 9: exit() # TODO 退出前需要判斷是否有正在執(zhí)行的monkey任務(wù)和關(guān)鍵字監(jiān)控任務(wù) eval(INSTRUCTION[str(instruction)] + '()') if __name__ == ’__main__’: main()
這里只寫(xiě)了開(kāi)啟日志監(jiān)控和關(guān)閉的方法,中間有些處理可以根據(jù)自己需要實(shí)現(xiàn),比如檢測(cè)到關(guān)鍵字之后除了拉取所有日志外,可以發(fā)送郵件、釘釘之類(lèi)的通知,根據(jù)自己需要去實(shí)現(xiàn)。
總結(jié)
到此這篇關(guān)于python 監(jiān)控logcat關(guān)鍵字功能的文章就介紹到這了,更多相關(guān)python 監(jiān)控logcat關(guān)鍵字內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Ajax引擎 ajax請(qǐng)求步驟詳細(xì)代碼2. JS語(yǔ)法也可以有C#的switch表達(dá)式3. JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析4. python 利用toapi庫(kù)自動(dòng)生成api5. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)6. 詳解Struts2中對(duì)未登錄jsp頁(yè)面實(shí)現(xiàn)攔截功能7. 老生常談.NET中的 COM 組件8. CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
