相干学习保举:python教程
本文次要引见应用Python挪用ADB饬令完成及时监控logcat要害字的性能
采纳多过程,可同时监控多个设施,监控多个要害字。
需求设置装备摆设ADB环境,详细设置装备摆设就没有多引见,随意搜一下一年夜把,间接上代码
经过一个全局变量管制开启以及封闭监控性能, INSTRUCTION 用于依据指令猎取对应的办法名
import os, threading, datetime # 猎取以后文件所正在目次,拼接出LOG门路 LOG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "log") # 设置装备摆设需求监控的要害字 KEYWORDS = ["ANR ", "NullPointerException", "CRASH", "Force Closed"] # 管制开启以及封闭 STOP_LOGCAT = True # 指令对应详细操作 INSTRUCTION = { "1": "filter_keywords", "2": "stop_filter_keywords", "3": "exit" } def filter_keywords(): global STOP_LOGCAT STOP_LOGCAT = False devices = get_devices() # 先猎取一切衔接的设施 print("开端监控要害字") 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("不在执行的义务\n") else: STOP_LOGCAT = True print("在中止要害字监控\n")
监控要害字主函数,
def filter_keyword(device): print("设施%s要害字监控已开启" % str(device)) sub = logcat(device) with sub: for line in sub.stdout: # 子过程会继续输入日记,对子过程工具.stdout进行轮回读取 for key in KEYWORDS: if line.decode("utf-8").find(key) != -1: # stdout输入为字节类型,需求转码 message = "设施:%s 检测到:%s\n" % (device, key)# 设施:192.168.56.104:5555 检测到:ANR path = get_log_path("bugreport") # 依据工夫创立文件夹 bugreport(device, path)# 拉取完好日记紧缩包到创立的文件夹内 send_message(message) # 这里能够换成本人要做的事件,比方发送邮件或钉钉告诉 if STOP_LOGCAT: break print("设施%s要害字监控已中止" % str(device)) sub.kill()
经过 subprocess.Popen 创立过程执行饬令,继续输入日记到 stdout
# logcat继续输入日记 def logcat(device): co妹妹and = "adb -s " + str(device) + " logcat -v time" sub = subprocess.Popen(co妹妹and, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return sub
猎取一切已衔接设施的办法,执行"adb devices"后输入以下,经过对饬令执行拿到的字符串切割猎取一切设施号以列表形式存储
# 猎取一切device def get_devices(): co妹妹and = "adb devices" res = os.popen(co妹妹and).read() devices = [] res = res.split("\n") for i in res: if i.endswith("device"): devices.append(i.split('\t')[0]) return devices
# 打包下载一切日记到以后目次 def bugreport(device, path): os.chdir(path)# bugreport会下载日记到以后文件夹,以是需求先切换到曾经创立的目次 co妹妹and = "adb -s " + str(device) + " bugreport" subprocess.Popen(co妹妹and, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1) print("设施:%s 日记门路:%s" % (str(device), path))
以 以后文件所正在目次/年/月/日 格局猎取日记门路,假如没有存正在主动创立
# 猎取日记寄存门路,假如没有存正在则按日期创立 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函数,轮回接纳指令,依据接纳的指令拿到办法名,并经过eval()办法执行。
def main(): while True: print("-" * 100) print("1:开启要害字监控\n2:中止要害字监控\n3:加入") print("-" * 100) instruction = str(input("\n\n请输出要进行的操作号:\n")) print("-" * 100) while instruction not in INSTRUCTION.keys(): instruction = str(input("\n\n输出有效,请从新输出:")) if int(instruction) == 9: exit() # TODO 加入前需求判别能否有在执行的monkey义务以及要害字监控义务 eval(INSTRUCTION[str(instruction)] + "()") if __name__ == '__main__': main()
代码分段之后有点混乱,看没有明确能够把代码复制到一个文件里捋一下就明确了
想理解更多编程学习,敬请存眷php培训栏目!
以上就是监控python logcat要害字的具体内容,更多请存眷资源魔其它相干文章!
标签: Python python教程 python编程 python使用问题 logcat
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!