1# Copyright 2016 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import collections 6import re 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib import utils 10 11LogcatLine = collections.namedtuple('LogcatLine', ['pid', 'tag', 'message']) 12 13def wait_for_logcat_log(message_tag, message_pattern, 14 process_id=None, timeout_seconds=30, host=None): 15 """Wait for a line to show up in logcat. 16 17 @param message_tag: string "tag" of the line, as understood by logcat. 18 @param message_pattern: regular expression pattern that describes the 19 entire text of the message to look for (e.g. '.*' matches all 20 messages). This is in grep's regex language. 21 @param process_id: optional integer process id to match on. 22 @param timeout_seconds: number of seconds to wait for the log line. 23 @param host: host object to look for the log line on. Defaults to 24 our local host. 25 26 """ 27 run = host.run if host is not None else utils.run 28 29 process_id_option = '' 30 if process_id is not None: 31 process_id_option = '--pid %d' % process_id 32 33 result = run('logcat %s --format=process -e %s -m 1' % ( 34 process_id_option, message_pattern), 35 timeout=timeout_seconds, 36 ignore_timeout=True) 37 if result is None: 38 raise error.TestFail('Timed out waiting for a log with message "%s"' % 39 message_pattern) 40 41 lines = result.stdout.strip().splitlines() 42 43 if len(lines) == 0: 44 raise error.TestError('Logcat did not return any lines') 45 46 line = lines[-1] 47 48 match = re.match(r'^.\( *(\d+)\) (.*) \(([^(]+)\)$', line) 49 if match: 50 return LogcatLine(pid=match.group(1), 51 message=match.group(2), 52 tag=match.group(3)) 53 raise error.TestError('Failed to match logcat line "%s"' % line) 54