|
| 1 | +# |
| 2 | +# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license. |
| 3 | +# |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import re |
| 7 | + |
| 8 | +class ColorizingStreamHandler(logging.StreamHandler): |
| 9 | + # color names to indices |
| 10 | + color_map = { |
| 11 | + 'black': 0, |
| 12 | + 'red': 1, |
| 13 | + 'green': 2, |
| 14 | + 'yellow': 3, |
| 15 | + 'blue': 4, |
| 16 | + 'magenta': 5, |
| 17 | + 'cyan': 6, |
| 18 | + 'white': 7, |
| 19 | + } |
| 20 | + |
| 21 | + # levels to (background, foreground, bold/intense) |
| 22 | + if os.name == 'nt': |
| 23 | + level_map = { |
| 24 | + logging.DEBUG: (None, 'blue', False), |
| 25 | + logging.INFO: (None, 'green', False), |
| 26 | + logging.WARNING: (None, 'yellow', False), |
| 27 | + logging.ERROR: (None, 'red', False), |
| 28 | + logging.CRITICAL: ('red', 'white', False) |
| 29 | + } |
| 30 | + else: |
| 31 | + level_map = { |
| 32 | + logging.DEBUG: (None, 'blue', False), |
| 33 | + logging.INFO: (None, 'green', False), |
| 34 | + logging.WARNING: (None, 'yellow', False), |
| 35 | + logging.ERROR: (None, 'red', False), |
| 36 | + logging.CRITICAL: ('red', 'white', False) |
| 37 | + } |
| 38 | + csi = '\x1b[' |
| 39 | + reset = '\x1b[0m' |
| 40 | + |
| 41 | + @property |
| 42 | + def is_tty(self): |
| 43 | + isatty = getattr(self.stream, 'isatty', None) |
| 44 | + return isatty and isatty() |
| 45 | + |
| 46 | + def emit(self, record): |
| 47 | + try: |
| 48 | + message = self.format(record) |
| 49 | + stream = self.stream |
| 50 | + |
| 51 | + if not self.is_tty: |
| 52 | + stream.write(message) |
| 53 | + else: |
| 54 | + self.output_colorized(message) |
| 55 | + stream.write(getattr(self, 'terminator', '\n')) |
| 56 | + |
| 57 | + self.flush() |
| 58 | + except (KeyboardInterrupt, SystemExit): |
| 59 | + raise |
| 60 | + except: |
| 61 | + self.handleError(record) |
| 62 | + |
| 63 | + if os.name != 'nt': |
| 64 | + def output_colorized(self, message): |
| 65 | + self.stream.write(message) |
| 66 | + else: |
| 67 | + import re |
| 68 | + ansi_esc = re.compile(r'\x1b\[((?:\d+)(?:;(?:\d+))*)m') |
| 69 | + |
| 70 | + nt_color_map = { |
| 71 | + 0: 0x00, # black |
| 72 | + 1: 0x04, # red |
| 73 | + 2: 0x02, # green |
| 74 | + 3: 0x06, # yellow |
| 75 | + 4: 0x01, # blue |
| 76 | + 5: 0x05, # magenta |
| 77 | + 6: 0x03, # cyan |
| 78 | + 7: 0x07, # white |
| 79 | + } |
| 80 | + |
| 81 | + def output_colorized(self, message): |
| 82 | + import ctypes |
| 83 | + |
| 84 | + parts = self.ansi_esc.split(message) |
| 85 | + write = self.stream.write |
| 86 | + h = None |
| 87 | + fd = getattr(self.stream, 'fileno', None) |
| 88 | + |
| 89 | + if fd is not None: |
| 90 | + fd = fd() |
| 91 | + |
| 92 | + if fd in (1, 2): # stdout or stderr |
| 93 | + h = ctypes.windll.kernel32.GetStdHandle(-10 - fd) |
| 94 | + |
| 95 | + while parts: |
| 96 | + text = parts.pop(0) |
| 97 | + |
| 98 | + if text: |
| 99 | + write(text) |
| 100 | + |
| 101 | + if parts: |
| 102 | + params = parts.pop(0) |
| 103 | + |
| 104 | + if h is not None: |
| 105 | + params = [int(p) for p in params.split(';')] |
| 106 | + color = 0 |
| 107 | + |
| 108 | + for p in params: |
| 109 | + if 40 <= p <= 47: |
| 110 | + color |= self.nt_color_map[p - 40] << 4 |
| 111 | + elif 30 <= p <= 37: |
| 112 | + color |= self.nt_color_map[p - 30] |
| 113 | + elif p == 1: |
| 114 | + color |= 0x08 # foreground intensity on |
| 115 | + elif p == 0: # reset to default color |
| 116 | + color = 0x07 |
| 117 | + else: |
| 118 | + pass # error condition ignored |
| 119 | + |
| 120 | + ctypes.windll.kernel32.SetConsoleTextAttribute(h, color) |
| 121 | + |
| 122 | + def colorize(self, message, record): |
| 123 | + if record.levelno in self.level_map: |
| 124 | + bg, fg, bold = self.level_map[record.levelno] |
| 125 | + params = [] |
| 126 | + |
| 127 | + if bg in self.color_map: |
| 128 | + params.append(str(self.color_map[bg] + 40)) |
| 129 | + |
| 130 | + if fg in self.color_map: |
| 131 | + params.append(str(self.color_map[fg] + 30)) |
| 132 | + |
| 133 | + if bold: |
| 134 | + params.append('1') |
| 135 | + |
| 136 | + if params and message: |
| 137 | + if message.lstrip() != message: |
| 138 | + prefix = re.search(r"\s+", message).group(0) |
| 139 | + message = message[len(prefix):] |
| 140 | + else: |
| 141 | + prefix = "" |
| 142 | + |
| 143 | + message = "%s%s" % (prefix, ''.join((self.csi, ';'.join(params), |
| 144 | + 'm', message, self.reset))) |
| 145 | + |
| 146 | + return message |
| 147 | + |
| 148 | + def format(self, record): |
| 149 | + message = logging.StreamHandler.format(self, record) |
| 150 | + return self.colorize(message, record) |
0 commit comments