cjg123
cjg123

Reputation: 473

Capture output from adb logcat

I am automating an Android application and I need to be able to capture logs while I am running the automation tests. I have tried using a terminal emulator but this only seems to give console logs. Next I tried using

        log = subprocess.check_output(["adb", "logcat"])

But when I do this my automation script stops indefinitely (presumably because it is waiting to continue after the logcat capture is complete), which does not work for me because I need the logcat to run in the background, while my script is running. So maybe 'Popen' is the way to go, and then pipe the output? Thank you!

Upvotes: 2

Views: 4168

Answers (1)

TemporalWolf
TemporalWolf

Reputation: 7952

The logs are always "running in the background", it's just whether you're looking at them. I would suggest instead, when you need a log dump, using

adb logcat -d

which will dump what it currently has, then exit.

So at the start of your test run:

subprocess.call(shlex.split('adb logcat -c'))

to clear the logs.

So at the end (and before any restart), dump the logs:

log = subprocess.check_output(shlex.split('adb logcat -d'))
with open("loggy.file", "w") as f:
    f.write(log)

Upvotes: 4

Related Questions