Guest User

Untitled

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