adbutils icon indicating copy to clipboard operation
adbutils copied to clipboard

adb_output() 方法,问题请教

Open abeelan opened this issue 3 years ago • 3 comments

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") 会更方便一些,请教下。

abeelan avatar Mar 19 '21 08:03 abeelan

这样用也可以 d.shell("logcat --clear")

codeskyblue avatar Mar 23 '21 07:03 codeskyblue

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 avatar Aug 09 '21 06:08 abeelan

@abeelan 你这个循环不结束不就可以了

codeskyblue avatar May 09 '22 06:05 codeskyblue

现在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

codeskyblue avatar Jan 29 '23 03:01 codeskyblue

棒!

abeelan avatar Feb 07 '23 02:02 abeelan