From fcd2439b504f6deb10ba148baf18752204e51e14 Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Thu, 16 Apr 2020 15:00:59 +0100 Subject: [PATCH] LogcatCollector: flush the log before terminating pexpect.spawn() Unless we tell pexpect to expect something it will not read from the process' buffer, or write anything to the logfile. If we follow the collector instructions from devlib's documentation: In [1]: from devlib import AndroidTarget, LogcatCollector In [2]: t = AndroidTarget() # Set up the collector on the Target. In [3]: collector = LogcatCollector(t) # Configure the output file path for the collector to use. In [4]: collector.set_output('adb_log.txt') # Reset the Collector to preform any required configuration or # preparation. In [5]: collector.reset() # Start Collecting In [6]: collector.start() # Wait for some output to be generated In [7]: sleep(10) # Stop Collecting In [8]: collector.stop() # Retrieved the collected data In [9]: output = collector.get_data() adb_log.txt will be empty because between collector.start() and collector.stop() there were no expect() calls to LogcatMonitor._logcat. As the get_log() function already has code to flush the log, abstract it to a function and call it in stop() before terminating the pexpect.spawn(). --- devlib/utils/android.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/devlib/utils/android.py b/devlib/utils/android.py index e6f5f11..bee4ed1 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -756,6 +756,7 @@ class LogcatMonitor(object): self._logcat = pexpect.spawn(logcat_cmd, logfile=self._logfile, encoding='utf-8') def stop(self): + self.flush_log() self._logcat.terminate() self._logfile.close() @@ -763,6 +764,12 @@ class LogcatMonitor(object): """ Return the list of lines found by the monitor """ + self.flush_log() + + with open(self._logfile.name) as fh: + return [line for line in fh] + + def flush_log(self): # Unless we tell pexect to 'expect' something, it won't read from # logcat's buffer or write into our logfile. We'll need to force it to # read any pending logcat output. @@ -793,9 +800,6 @@ class LogcatMonitor(object): # printed anything since pexpect last read from its buffer. break - with open(self._logfile.name) as fh: - return [line for line in fh] - def clear_log(self): with open(self._logfile.name, 'w') as _: pass