OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Provides an interface to communicate with the device via the adb command. | 5 """Provides an interface to communicate with the device via the adb command. |
6 | 6 |
7 Assumes adb binary is currently on system path. | 7 Assumes adb binary is currently on system path. |
8 """ | 8 """ |
9 | 9 |
10 import collections | 10 import collections |
(...skipping 1240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1251 StopRecordingLogcat or SearchLogcatRecord following the tests. | 1251 StopRecordingLogcat or SearchLogcatRecord following the tests. |
1252 | 1252 |
1253 Args: | 1253 Args: |
1254 clear: True if existing log output should be cleared. | 1254 clear: True if existing log output should be cleared. |
1255 filters: A list of logcat filters to be used. | 1255 filters: A list of logcat filters to be used. |
1256 """ | 1256 """ |
1257 if clear: | 1257 if clear: |
1258 self._adb.SendCommand('logcat -c') | 1258 self._adb.SendCommand('logcat -c') |
1259 logcat_command = 'adb %s logcat -v threadtime %s' % (self._adb._target_arg, | 1259 logcat_command = 'adb %s logcat -v threadtime %s' % (self._adb._target_arg, |
1260 ' '.join(filters)) | 1260 ' '.join(filters)) |
1261 self._logcat_tmpoutfile = tempfile.TemporaryFile(bufsize=0) | 1261 self._logcat_tmpoutfile = tempfile.NamedTemporaryFile(bufsize=0) |
1262 self.logcat_process = subprocess.Popen(logcat_command, shell=True, | 1262 self.logcat_process = subprocess.Popen(logcat_command, shell=True, |
1263 stdout=self._logcat_tmpoutfile) | 1263 stdout=self._logcat_tmpoutfile) |
1264 | 1264 |
| 1265 def GetCurrentRecordedLogcat(self): |
| 1266 """Return the current content of the logcat being recorded. |
| 1267 Call this after StartRecordingLogcat() and before StopRecordingLogcat(). |
| 1268 This can be useful to perform timed polling/parsing. |
| 1269 Returns: |
| 1270 Current logcat output as a single string, or None if |
| 1271 StopRecordingLogcat() was already called. |
| 1272 """ |
| 1273 if not self._logcat_tmpoutfile: |
| 1274 return None |
| 1275 |
| 1276 with open(self._logcat_tmpoutfile.name) as f: |
| 1277 return f.read() |
| 1278 |
1265 def StopRecordingLogcat(self): | 1279 def StopRecordingLogcat(self): |
1266 """Stops an existing logcat recording subprocess and returns output. | 1280 """Stops an existing logcat recording subprocess and returns output. |
1267 | 1281 |
1268 Returns: | 1282 Returns: |
1269 The logcat output as a string or an empty string if logcat was not | 1283 The logcat output as a string or an empty string if logcat was not |
1270 being recorded at the time. | 1284 being recorded at the time. |
1271 """ | 1285 """ |
1272 if not self.logcat_process: | 1286 if not self.logcat_process: |
1273 return '' | 1287 return '' |
1274 # Cannot evaluate directly as 0 is a possible value. | 1288 # Cannot evaluate directly as 0 is a possible value. |
1275 # Better to read the self.logcat_process.stdout before killing it, | 1289 # Better to read the self.logcat_process.stdout before killing it, |
1276 # Otherwise the communicate may return incomplete output due to pipe break. | 1290 # Otherwise the communicate may return incomplete output due to pipe break. |
1277 if self.logcat_process.poll() is None: | 1291 if self.logcat_process.poll() is None: |
1278 self.logcat_process.kill() | 1292 self.logcat_process.kill() |
1279 self.logcat_process.wait() | 1293 self.logcat_process.wait() |
1280 self.logcat_process = None | 1294 self.logcat_process = None |
1281 self._logcat_tmpoutfile.seek(0) | 1295 self._logcat_tmpoutfile.seek(0) |
1282 output = self._logcat_tmpoutfile.read() | 1296 output = self._logcat_tmpoutfile.read() |
1283 self._logcat_tmpoutfile.close() | 1297 self._logcat_tmpoutfile.close() |
| 1298 self._logcat_tmpoutfile = None |
1284 return output | 1299 return output |
1285 | 1300 |
1286 def SearchLogcatRecord(self, record, message, thread_id=None, proc_id=None, | 1301 def SearchLogcatRecord(self, record, message, thread_id=None, proc_id=None, |
1287 log_level=None, component=None): | 1302 log_level=None, component=None): |
1288 """Searches the specified logcat output and returns results. | 1303 """Searches the specified logcat output and returns results. |
1289 | 1304 |
1290 This method searches through the logcat output specified by record for a | 1305 This method searches through the logcat output specified by record for a |
1291 certain message, narrowing results by matching them against any other | 1306 certain message, narrowing results by matching them against any other |
1292 specified criteria. It returns all matching lines as described below. | 1307 specified criteria. It returns all matching lines as described below. |
1293 | 1308 |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1610 """ | 1625 """ |
1611 def __init__(self, output): | 1626 def __init__(self, output): |
1612 self._output = output | 1627 self._output = output |
1613 | 1628 |
1614 def write(self, data): | 1629 def write(self, data): |
1615 data = data.replace('\r\r\n', '\n') | 1630 data = data.replace('\r\r\n', '\n') |
1616 self._output.write(data) | 1631 self._output.write(data) |
1617 | 1632 |
1618 def flush(self): | 1633 def flush(self): |
1619 self._output.flush() | 1634 self._output.flush() |
OLD | NEW |