简体   繁体   English

Python无法捕获完整的ADB日志

[英]Python can not capture full adb logs

What I want to achieve - running adb log (logs are running), doing some activity on android, stop/end log capture (ctrl+c), post processing of logs. 我要实现的目标-运行adb日志(日志正在运行),在android上进行一些活动,停止/结束日志捕获(ctrl + c),日志的后处理。

Only issue which i face - can not capture full logcat log in file 我面对的唯一问题-无法捕获完整的logcat登录文件

import sys 
import subprocess
import time
import ctypes

# start

print "test start"
time.sleep(5)

# log capturing start, log does not stop it will keep on running
proc = subprocess.Popen("adb logcat -v time",stdout=subprocess.PIPE )
time.sleep(3) # just so it runs for a while


print "calc start"
time.sleep(5)
#START test************************************

Some code for testing



#CTRL C*************************************************

try:
    ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, 0)
    proc.wait()
except KeyboardInterrupt:
    print "ignoring ctrlc"
print "still running"


#********************adb log saving******************

text = proc.stdout.read()

f = open('C:\Python27\out_logs_dd\log.txt', 'w')

f.write(text)

with open('C:\Python27\out_logs_dd\log.txt', 'w') as f:
   f.write(text)

f.close()

When I run this code, everything is running but log size is so small. 当我运行此代码时,所有内容都在运行,但是日志大小很小。 I searched and came to know that "proc.communicate()" might be the solution. 经过搜索,我知道"proc.communicate()"可能是解决方案。 I tried 'communicate' but could not solve the issue. 我尝试'communicate'但无法解决问题。 any help pointer pls. 任何帮助指针请。

Replacing proc.stdout.read() with proc.stdout.readline() solved it for me. 更换proc.stdout.read()proc.stdout.readline()解决了这个问题对我来说。 stdout.read() reads everything until EOF - slowing down the speed drastically. stdout.read()读取所有内容,直到EOF为止-大大降低了速度。 A similar question: Communicate with subprocess without waiting for the subprocess to terminate on windows 一个类似的问题: 与子进程进行通信,而无需等待子进程在Windows上终止

you can use adb shell logcat instead of abd logcat in 您可以在中使用adb shell logcat而不是abd logcat

proc = subprocess.Popen("adb shell logcat -v time",stdout=subprocess.PIPE) proc = subprocess.Popen(“ adb shell logcat -v time”,stdout = subprocess.PIPE)

it will give all logs. 它将给出所有日志。

i hope it will work. 我希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM