OLD | NEW |
1 # Copyright (C) 2010, 2012 Google Inc. All rights reserved. | 1 # Copyright (C) 2010, 2012 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 self._last_partial_line = txt[txt.rfind('\n') + 1:] | 92 self._last_partial_line = txt[txt.rfind('\n') + 1:] |
93 | 93 |
94 def write(self, txt, now=None, pid=None): | 94 def write(self, txt, now=None, pid=None): |
95 now = now or self._time_fn() | 95 now = now or self._time_fn() |
96 pid = pid or self._pid | 96 pid = pid or self._pid |
97 self._last_write_time = now | 97 self._last_write_time = now |
98 if self._last_partial_line: | 98 if self._last_partial_line: |
99 self._erase_last_partial_line() | 99 self._erase_last_partial_line() |
100 if self._verbose: | 100 if self._verbose: |
101 now_tuple = time.localtime(now) | 101 now_tuple = time.localtime(now) |
102 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)
) | 102 msg = '%02d:%02d:%02d.%03d %d %s' % (now_tuple.tm_hour, now_tuple.tm
_min, now_tuple.tm_sec, |
| 103 int((now * 1000) % 1000), pid,
self._ensure_newline(txt)) |
103 elif self._isatty: | 104 elif self._isatty: |
104 msg = txt | 105 msg = txt |
105 else: | 106 else: |
106 msg = self._ensure_newline(txt) | 107 msg = self._ensure_newline(txt) |
107 | 108 |
108 # This is the easiest way to make sure a byte stream is printable as asc
ii | 109 # This is the easiest way to make sure a byte stream is printable as asc
ii |
109 # with all non-ascii characters replaced. | 110 # with all non-ascii characters replaced. |
110 uni_msg = msg if isinstance(msg, unicode) else msg.decode('ascii', error
s='replace') | 111 uni_msg = msg if isinstance(msg, unicode) else msg.decode('ascii', error
s='replace') |
111 self._stream.write(uni_msg.encode('ascii', errors='replace')) | 112 self._stream.write(uni_msg.encode('ascii', errors='replace')) |
112 | 113 |
113 def writeln(self, txt, now=None, pid=None): | 114 def writeln(self, txt, now=None, pid=None): |
114 self.write(self._ensure_newline(txt), now, pid) | 115 self.write(self._ensure_newline(txt), now, pid) |
115 | 116 |
116 def _erase_last_partial_line(self): | 117 def _erase_last_partial_line(self): |
117 num_chars = len(self._last_partial_line) | 118 num_chars = len(self._last_partial_line) |
118 self._stream.write(self._erasure(self._last_partial_line)) | 119 self._stream.write(self._erasure(self._last_partial_line)) |
119 self._last_partial_line = '' | 120 self._last_partial_line = '' |
120 | 121 |
121 def flush(self): | 122 def flush(self): |
122 if self._last_partial_line: | 123 if self._last_partial_line: |
123 self._stream.write('\n') | 124 self._stream.write('\n') |
124 self._last_partial_line = '' | 125 self._last_partial_line = '' |
125 self._stream.flush() | 126 self._stream.flush() |
126 | 127 |
127 def number_of_columns(self): | 128 def number_of_columns(self): |
128 return self._number_of_columns | 129 return self._number_of_columns |
129 | 130 |
130 | 131 |
131 class _LogHandler(logging.Handler): | 132 class _LogHandler(logging.Handler): |
| 133 |
132 def __init__(self, meter): | 134 def __init__(self, meter): |
133 logging.Handler.__init__(self) | 135 logging.Handler.__init__(self) |
134 self._meter = meter | 136 self._meter = meter |
135 self.name = LOG_HANDLER_NAME | 137 self.name = LOG_HANDLER_NAME |
136 | 138 |
137 def emit(self, record): | 139 def emit(self, record): |
138 self._meter.writeln(record.getMessage(), record.created, record.process) | 140 self._meter.writeln(record.getMessage(), record.created, record.process) |
OLD | NEW |