一,我们在ADB目录下,新建一个adb_logcat_monitoring.py文件,当我们发现日志出现空指针的时候,可以及时的做处理。
代码如下:
# coding=utf-8
# coding=utf-8
import os
import re
import subprocess
command = "adb logcat -v threadtime" # 具体命令
keyword = r".*java.lang.NullPointerException.*" # 正则表达式
class LogcatFilter(object):
def __init__(self):
# 开始执行adb命令
self.p_obj = subprocess.Popen(
args=command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def filter_logcat(self):
# 实时监控并过滤每一行生成的日志里的关键字
print("Logcat catching and filtering...")
with self.p_obj:
for line in self.p_obj.stdout:
if re.match(keyword, line.decode("utf-8")):
print("Found %s" % keyword)
print("running adb bugreport to pull releated logs...pls wait")
os.system("adb bugreport") # 导出一次bugreport log压缩包
if __name__ == '__main__':
logcat = LogcatFilter()
logcat.filter_logcat()
os.system("pause")