1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

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().
This commit is contained in:
Javi Merino 2020-04-16 15:00:59 +01:00 committed by Marc Bonnici
parent 3709e06b5c
commit fcd2439b50

View File

@ -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