OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 import json | |
5 import logging | |
6 | |
7 class InspectorConsole(object): | |
8 def __init__(self, inspector_backend): | |
9 self._inspector_backend = inspector_backend | |
10 self._inspector_backend.RegisterDomain( | |
11 'Console', | |
12 self._OnNotification, | |
13 self._OnClose) | |
14 self._message_output_stream = None | |
15 self._last_message = None | |
16 self._console_enabled = False | |
17 | |
18 def _OnNotification(self, msg): | |
19 logging.debug('Notification: %s', json.dumps(msg, indent=2)) | |
20 if msg['method'] == 'Console.messageAdded': | |
21 if msg['params']['message']['url'] == 'chrome://newtab/': | |
22 return | |
23 self._last_message = 'At %s:%i: %s' % ( | |
24 msg['params']['message']['url'], | |
25 msg['params']['message']['line'], | |
26 msg['params']['message']['text']) | |
27 if self._message_output_stream: | |
28 self._message_output_stream.write( | |
29 '%s\n' % self._last_message) | |
30 | |
31 elif msg['method'] == 'Console.messageRepeatCountUpdated': | |
32 if self._message_output_stream: | |
33 self._message_output_stream.write( | |
34 '%s\n' % self._last_message) | |
35 | |
36 def _OnClose(self): | |
37 pass | |
38 | |
39 # False positive in PyLint 0.25.1: http://www.logilab.org/89092 | |
40 @property | |
41 def message_output_stream(self): # pylint: disable=E0202 | |
42 return self._message_output_stream | |
43 | |
44 @message_output_stream.setter | |
45 def message_output_stream(self, stream): # pylint: disable=E0202 | |
46 self._message_output_stream = stream | |
47 self._UpdateConsoleEnabledState() | |
48 | |
49 def _UpdateConsoleEnabledState(self): | |
50 enabled = self._message_output_stream != None | |
51 if enabled == self._console_enabled: | |
52 return | |
53 | |
54 if enabled: | |
55 method_name = 'enable' | |
56 else: | |
57 method_name = 'disable' | |
58 self._inspector_backend.SyncRequest({ | |
59 'method': 'Console.%s' % method_name | |
60 }) | |
61 self._console_enabled = enabled | |
OLD | NEW |