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
5""" Error and information logging for IDL """
6
7import sys
8
9
10class 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  """
16  def __init__(self, name, out):
17    if name:
18      self._name = '%s : ' % name
19    else:
20      self._name = ''
21
22    self._out = out
23    self._capture = False
24    self._console = True
25    self._log = []
26
27  def Log(self, msg):
28    if self._console:
29      line = "%s\n" % (msg)
30      self._out.write(line)
31    if self._capture:
32      self._log.append(msg)
33
34  def LogLine(self, filename, lineno, pos, msg):
35    if self._console:
36      line = "%s(%d) : %s%s\n" % (filename, lineno, self._name, msg)
37      self._out.write(line)
38    if self._capture:
39      self._log.append(msg)
40
41  def SetConsole(self, enable):
42    self._console = enable
43
44  def SetCapture(self, enable):
45    self._capture = enable
46
47  def DrainLog(self):
48    out = self._log
49    self._log = []
50    return out
51
52ErrOut  = IDLLog('Error', sys.stderr)
53WarnOut = IDLLog('Warning', sys.stdout)
54InfoOut = IDLLog('', sys.stdout)
55