Python无法捕获完整的adb日志

时间:2014-09-24 09:54:42

标签: android python logging adb

我想要实现的目标 - 运行adb日志(日志正在运行), 在android上做一些活动, 停止/结束日志捕获(ctrl + c), 后处理日志。

只发出我面对的问题 - 无法捕获完整的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()

当我运行此代码时,一切都在运行但日志大小非常小。 我搜索并了解"proc.communicate()"可能是解决方案。 我试过'communicate'但无法解决问题。 任何帮助指针。

1 个答案:

答案 0 :(得分:-1)

您可以使用 adb shell logcat 代替

中的abd logcat

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

它将提供所有日志。

我希望它能奏效。