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