adbutils
adbutils copied to clipboard
adb_output() 方法,问题请教
def adb_output(self, *args, **kwargs):
"""Run adb command use subprocess and get its content
Returns:
string of output
Raises:
EnvironmentError
"""
cmds = [adb_path(), '-s', self._serial
] if self._serial else [adb_path()]
cmds.extend(args)
cmdline = subprocess.list2cmdline(map(str, cmds))
try:
return subprocess.check_output(cmdline,
stderr=subprocess.STDOUT,
shell=True).decode('utf-8')
except subprocess.CalledProcessError as e:
if kwargs.get('raise_error', True):
raise EnvironmentError(
"subprocess", cmdline,
e.output.decode('utf-8', errors='ignore'))
使用 adbutils 过程中,要使用 adb logcat -c
这样的命令,所以找到了 adb_output()
这个函数,但是发现他的传参是通过 args 接收的,调用: adb_output("logcat", "-c")
,这样感觉比较麻烦呀;
直接实现成类似 shell()
的调用方式:adb_output("logcat -c")
会更方便一些,请教下。
这样用也可以 d.shell("logcat --clear")
d.shell("logcat --clear")
stream = d.shell("logcat", stream=True)
with stream:
f = stream.conn.makefile()
for _ in range(100): # read 100 lines
line = f.readline()
print("Logcat:", line.rstrip())
f.close()
这个方法如何实现一直循环读取 logcat 日志呢
@abeelan 你这个循环不结束不就可以了
现在logcat多了一个新的方法了
Logcat
# filter logcat to file
logcat = d.logcat("logcat.txt", clear=True, re_filter=".*FA.*") # clear default False
# do something else
logcat.stop(timeout=3) # tell thread to stop write, wait for 3s, if not stopped, raise TimeoutError
logcat.stop_nowait() # tell thread to stop write and close file
棒!