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 """ Error and information logging for IDL """ | 5 """ Error and information logging for IDL """ |
6 | 6 |
7 # | |
8 # IDL Log | |
9 # | |
10 # And IDLLog object provides a mechanism for capturing logging output | |
11 # and/or sending out via a file handle (usually stdout or stderr). | |
12 # | |
13 import sys | 7 import sys |
14 | 8 |
15 | 9 |
16 class IDLLog(object): | 10 class IDLLog(object): |
| 11 """Captures and routes logging output. |
| 12 |
| 13 Caputres logging output and/or sends out via a file handle, typically |
| 14 stdout or stderr. |
| 15 """ |
17 def __init__(self, name, out): | 16 def __init__(self, name, out): |
18 if name: | 17 if name: |
19 self.name = '%s : ' % name | 18 self._name = '%s : ' % name |
20 else: | 19 else: |
21 self.name = '' | 20 self._name = '' |
22 | 21 |
23 self.out = out | 22 self._out = out |
24 self.capture = False | 23 self._capture = False |
25 self.console = True | 24 self._console = True |
26 self.log = [] | 25 self._log = [] |
27 | 26 |
28 def Log(self, msg): | 27 def Log(self, msg): |
29 line = "%s\n" % (msg) | 28 if self._console: |
30 if self.console: self.out.write(line) | 29 line = "%s\n" % (msg) |
31 if self.capture: | 30 self._out.write(line) |
32 self.log.append(msg) | 31 if self._capture: |
33 | 32 self._log.append(msg) |
34 def LogTag(self, msg): | |
35 line = "%s%s\n" % (self.name, msg) | |
36 if self.console: self.out.write(line) | |
37 if self.capture: | |
38 self.log.append(msg) | |
39 | 33 |
40 def LogLine(self, filename, lineno, pos, msg): | 34 def LogLine(self, filename, lineno, pos, msg): |
41 line = "%s(%d) : %s%s\n" % (filename, lineno, self.name, msg) | 35 if self._console: |
42 if self.console: self.out.write(line) | 36 line = "%s(%d) : %s%s\n" % (filename, lineno, self._name, msg) |
43 if self.capture: self.log.append(msg) | 37 self._out.write(line) |
| 38 if self._capture: |
| 39 self._log.append(msg) |
44 | 40 |
45 def SetConsole(self, enable, out = None): | 41 def SetConsole(self, enable): |
46 self.console = enable | 42 self._console = enable |
47 if out: self.out = out | |
48 | 43 |
49 def SetCapture(self, enable): | 44 def SetCapture(self, enable): |
50 self.capture = enable | 45 self._capture = enable |
51 | 46 |
52 def DrainLog(self): | 47 def DrainLog(self): |
53 out = self.log | 48 out = self._log |
54 self.log = [] | 49 self._log = [] |
55 return out | 50 return out |
56 | 51 |
57 ErrOut = IDLLog('Error', sys.stderr) | 52 ErrOut = IDLLog('Error', sys.stderr) |
58 WarnOut = IDLLog('Warning', sys.stdout) | 53 WarnOut = IDLLog('Warning', sys.stdout) |
59 InfoOut = IDLLog('', sys.stdout) | 54 InfoOut = IDLLog('', sys.stdout) |
OLD | NEW |