Skip to content

Instantly share code, notes, and snippets.

@Red-Eyed
Last active December 18, 2022 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Red-Eyed/1e9e5130afccdb3c7ef406299ad4cec3 to your computer and use it in GitHub Desktop.
Save Red-Eyed/1e9e5130afccdb3c7ef406299ad4cec3 to your computer and use it in GitHub Desktop.
logcat
#!/usr/bin/env python3
from pathlib import Path
import os
import sys
from subprocess import Popen, PIPE, getoutput
from shlex import split
from contextlib import redirect_stdout
import argparse
CURRENT_DIR = Path(__file__).parent
os.environ["PATH"] = f"{CURRENT_DIR.as_posix()}{os.pathsep}" + os.environ["PATH"]
ENV = os.environ
class MultiOut(object):
def __init__(self, *files):
self.files = files
def write(self, s):
for f in self.files:
f.write(s)
def get_pid(app_name: str):
while True:
cmd = f"adb shell 'pidof {app_name}'"
print(cmd)
pid: str = getoutput(split(cmd))
if len(pid) > 0:
print(pid)
return pid
def logcat(pid, output: Path):
with output.open("tw") as f:
out = MultiOut(f, sys.stdout)
with redirect_stdout(out):
cmd = f"adb logcat -v color --pid={pid}"
print(cmd)
args = split(cmd)
proc = Popen(args, universal_newlines=True, stdout=PIPE, text=True, env=ENV)
for line in proc.stdout:
out.write(line)
proc.wait()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--app_name", required=True)
parser.add_argument("--output", type=Path, default=CURRENT_DIR / "logcat.txt")
args = parser.parse_args()
pid = get_pid(args.app_name)
logcat(pid, args.output)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment