OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium 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 |
| 5 import os |
| 6 import logging |
| 7 import sys |
| 8 |
| 9 from devil.android import logcat_monitor |
| 10 from devil.utils import reraiser_thread |
| 11 from pylib import constants |
| 12 |
| 13 sys.path.insert(0, os.path.abspath(os.path.join( |
| 14 constants.DIR_SOURCE_ROOT, 'tools', 'swarming_client'))) |
| 15 from libs.logdog import bootstrap # pylint: disable=import-error |
| 16 |
| 17 class LogdogLogcatMonitor(logcat_monitor.LogcatMonitor): |
| 18 """Logcat monitor that writes logcat to a logdog stream. |
| 19 The logdog stream client will return a url, where contains the logcat. |
| 20 """ |
| 21 def __init__(self, adb, stream_name, clear=True, filter_specs=None): |
| 22 super(LogdogLogcatMonitor, self).__init__(adb, clear, filter_specs) |
| 23 self._logcat_url = '' |
| 24 self._logdog_stream = None |
| 25 self._stream_client = None |
| 26 self._stream_name = stream_name |
| 27 try: |
| 28 self._stream_client = bootstrap.ButlerBootstrap.probe().stream_client() |
| 29 self._logdog_stream = self._stream_client.open_text(self._stream_name) |
| 30 except bootstrap.NotBootstrappedError as e: |
| 31 logging.exception( |
| 32 'Error not bootstrapped. Failed to start logdog: %s', e) |
| 33 except (KeyError, ValueError) as e: |
| 34 logging.exception('Error when creating stream_client/stream: %s.', e) |
| 35 |
| 36 def GetLogcatURL(self): |
| 37 """Return logcat url. |
| 38 |
| 39 The default logcat url is '', if failed to create stream_client. |
| 40 """ |
| 41 return self._logcat_url |
| 42 |
| 43 def Stop(self): |
| 44 """Stops the logcat monitor. |
| 45 |
| 46 Close the logdog stream as well. |
| 47 """ |
| 48 super(LogdogLogcatMonitor, self)._StopRecording() |
| 49 if self._logdog_stream: |
| 50 try: |
| 51 self._logcat_url = self._stream_client.get_viewer_url(self._stream_name) |
| 52 except (KeyError, ValueError) as e: |
| 53 logging.exception('Error cannot get viewer url: %s', e) |
| 54 self._logdog_stream.close() |
| 55 |
| 56 def Start(self): |
| 57 """Starts the logdog logcat monitor. |
| 58 |
| 59 Clears the logcat if |clear| was set in |__init__|. |
| 60 """ |
| 61 if self._clear: |
| 62 self._adb.Logcat(clear=True) |
| 63 self._StartRecording() |
| 64 |
| 65 def _StartRecording(self): |
| 66 """Starts recording logcat to file. |
| 67 |
| 68 Write logcat to stream at the same time. |
| 69 """ |
| 70 def record_to_stream(): |
| 71 if self._logdog_stream: |
| 72 for data in self._adb.Logcat(filter_specs=self._filter_specs, |
| 73 logcat_format='threadtime'): |
| 74 if self._stop_recording_event.isSet(): |
| 75 return |
| 76 self._logdog_stream.write(data + '\n') |
| 77 |
| 78 self._stop_recording_event.clear() |
| 79 if not self._record_thread: |
| 80 self._record_thread = reraiser_thread.ReraiserThread(record_to_stream) |
| 81 self._record_thread.start() |
| 82 |
| 83 def Close(self): |
| 84 """Override parent's close method.""" |
| 85 pass |
| 86 |
| 87 def __del__(self): |
| 88 """Override parent's delete method.""" |
| 89 pass |
OLD | NEW |