2 # Copyright (C) 2010, 2012 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 from webkitpy.common.memoized import memoized
38 LOG_HANDLER_NAME = 'MeteredStreamLogHandler'
41 class MeteredStream(object):
43 This class implements a stream wrapper that has 'meters' as well as
44 regular output. A 'meter' is a single line of text that can be erased
45 and rewritten repeatedly, without producing multiple lines of output. It
46 can be used to produce effects like progress bars.
52 return '\b' * num_chars + ' ' * num_chars + '\b' * num_chars
55 def _ensure_newline(txt):
56 return txt if txt.endswith('\n') else txt + '\n'
58 def __init__(self, stream=None, verbose=False, logger=None, time_fn=None, pid=None):
59 self._stream = stream or sys.stderr
60 self._verbose = verbose
61 self._time_fn = time_fn or time.time
62 self._pid = pid or os.getpid()
63 self._isatty = self._stream.isatty()
64 self._erasing = self._isatty and not verbose
65 self._last_partial_line = ''
66 self._last_write_time = 0.0
67 self._throttle_delay_in_secs = 0.066 if self._erasing else 10.0
70 self._log_handler = None
72 log_level = logging.DEBUG if verbose else logging.INFO
73 self._log_handler = _LogHandler(self)
74 self._log_handler.setLevel(log_level)
75 self._logger.addHandler(self._log_handler)
82 self._logger.removeHandler(self._log_handler)
83 self._log_handler = None
85 def write_throttled_update(self, txt):
87 if now - self._last_write_time >= self._throttle_delay_in_secs:
88 self.write_update(txt, now)
90 def write_update(self, txt, now=None):
93 self._last_partial_line = txt[txt.rfind('\n') + 1:]
95 def write(self, txt, now=None, pid=None):
96 now = now or self._time_fn()
97 pid = pid or self._pid
98 self._last_write_time = now
99 if self._last_partial_line:
100 self._erase_last_partial_line()
102 now_tuple = time.localtime(now)
103 msg = '%02d:%02d:%02d.%03d %d %s' % (now_tuple.tm_hour, now_tuple.tm_min, now_tuple.tm_sec, int((now * 1000) % 1000), pid, self._ensure_newline(txt))
107 msg = self._ensure_newline(txt)
109 self._stream.write(msg)
111 def writeln(self, txt, now=None, pid=None):
112 self.write(self._ensure_newline(txt), now, pid)
114 def _erase_last_partial_line(self):
115 num_chars = len(self._last_partial_line)
116 self._stream.write(self._erasure(self._last_partial_line))
117 self._last_partial_line = ''
120 if self._last_partial_line:
121 self._stream.write('\n')
122 self._last_partial_line = ''
126 def number_of_columns(self):
133 packed = fcntl.ioctl(self._stream.fileno(), termios.TIOCGWINSZ, '\0' * 8)
134 _, columns, _, _ = struct.unpack('HHHH', packed)
140 class _LogHandler(logging.Handler):
141 def __init__(self, meter):
142 logging.Handler.__init__(self)
144 self.name = LOG_HANDLER_NAME
146 def emit(self, record):
147 self._meter.writeln(record.getMessage(), record.created, record.process)