idl_log.py revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
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#
13import sys
14
15
16class IDLLog(object):
17  def __init__(self, name, out):
18    if name:
19      self.name = '%s : ' % name
20    else:
21      self.name = ''
22
23    self.out = out
24    self.capture = False
25    self.console = True
26    self.log = []
27
28  def Log(self, msg):
29    line = "%s\n" % (msg)
30    if self.console: self.out.write(line)
31    if self.capture:
32      self.log.append(msg)
33
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
40  def LogLine(self, filename, lineno, pos, msg):
41    line = "%s(%d) : %s%s\n" % (filename, lineno, self.name, msg)
42    if self.console: self.out.write(line)
43    if self.capture: self.log.append(msg)
44
45  def SetConsole(self, enable, out = None):
46    self.console = enable
47    if out: self.out = out
48
49  def SetCapture(self, enable):
50    self.capture = enable
51
52  def DrainLog(self):
53    out = self.log
54    self.log = []
55    return out
56
57ErrOut  = IDLLog('Error', sys.stderr)
58WarnOut = IDLLog('Warning', sys.stdout)
59InfoOut = IDLLog('', sys.stdout)
60