From 6bda0934ad97a48236c0d9545b4d17f5a2571547 Mon Sep 17 00:00:00 2001 From: Valentin Schneider Date: Tue, 12 Sep 2017 15:44:25 +0100 Subject: [PATCH] utils/android: LogcatMonitor fixes host.kill_children() is used to properly kill the logcat process when it is IO blocked. The logcat regexp argument is now within double quotes, as having parenthesis within the regexp could break the command. LogcatMonitor.search() has been renamed to wait_for() to make the behaviour of the method more explicit. A non-blocking version of this method has been added and is named search(). --- devlib/utils/android.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/devlib/utils/android.py b/devlib/utils/android.py index 48d6cf5..4916246 100644 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -32,6 +32,7 @@ from collections import defaultdict from devlib.exception import TargetError, HostError, DevlibError from devlib.utils.misc import check_output, which, memoized, ABI_MAP from devlib.utils.misc import escape_single_quotes, escape_double_quotes +from devlib import host logger = logging.getLogger('android') @@ -555,18 +556,21 @@ class LogcatMonitor(threading.Thread): regexp = '{}'.format('|'.join(self._regexps)) if len(self._regexps) > 1: regexp = '({})'.format(regexp) - logcat_cmd = '{} -e {}'.format(logcat_cmd, regexp) + logcat_cmd = '{} -e "{}"'.format(logcat_cmd, regexp) logger.debug('logcat command ="{}"'.format(logcat_cmd)) self._logcat = self.target.background(logcat_cmd) while not self._stopped.is_set(): line = self._logcat.stdout.readline(1024) - self._add_line(line) + if line: + self._add_line(line) def stop(self): - # Popen can be stuck on readline() so send it a SIGKILL - self._logcat.terminate() + # Kill the underlying logcat process + # This will unblock self._logcat.stdout.readline() + host.kill_children(self._logcat.pid) + self._logcat.kill() self._stopped.set() self.join() @@ -609,9 +613,10 @@ class LogcatMonitor(threading.Thread): return res - def search(self, regexp, timeout=30): + def search(self, regexp): """ Search a line that matches a regexp in the logcat log + Return immediatly """ res = [] @@ -623,8 +628,18 @@ class LogcatMonitor(threading.Thread): if re.match(regexp, line): res.append(line) + return res + + def wait_for(self, regexp, timeout=30): + """ + Search a line that matches a regexp in the logcat log + Wait for it to appear if it's not found + """ + res = self.search(regexp) + # Found some matches, return them - if len(res) > 0: + # Also return if thread not running + if len(res) > 0 or not self.is_alive(): return res # Did not find any match, wait for one to pop up