sqlmap/extra/ansistrm/ansistrm.py

151 lines
4.5 KiB
Python
Raw Normal View History

2012-07-10 05:47:35 +04:00
#
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
#
import logging
import os
import re
2012-07-10 05:47:35 +04:00
class ColorizingStreamHandler(logging.StreamHandler):
# color names to indices
color_map = {
'black': 0,
'red': 1,
'green': 2,
'yellow': 3,
'blue': 4,
'magenta': 5,
'cyan': 6,
'white': 7,
}
# levels to (background, foreground, bold/intense)
2012-07-10 05:47:35 +04:00
if os.name == 'nt':
level_map = {
2012-07-10 05:58:49 +04:00
logging.DEBUG: (None, 'blue', False),
2012-07-10 05:47:35 +04:00
logging.INFO: (None, 'green', False),
logging.WARNING: (None, 'yellow', False),
logging.ERROR: (None, 'red', False),
logging.CRITICAL: ('red', 'white', False)
2012-07-10 05:47:35 +04:00
}
else:
level_map = {
2012-07-10 05:58:49 +04:00
logging.DEBUG: (None, 'blue', False),
2012-07-10 05:47:35 +04:00
logging.INFO: (None, 'green', False),
logging.WARNING: (None, 'yellow', False),
logging.ERROR: (None, 'red', False),
logging.CRITICAL: ('red', 'white', False)
2012-07-10 05:47:35 +04:00
}
csi = '\x1b['
reset = '\x1b[0m'
@property
def is_tty(self):
isatty = getattr(self.stream, 'isatty', None)
return isatty and isatty()
def emit(self, record):
try:
message = self.format(record)
stream = self.stream
2012-07-10 05:47:35 +04:00
if not self.is_tty:
stream.write(message)
else:
self.output_colorized(message)
stream.write(getattr(self, 'terminator', '\n'))
2012-07-10 05:47:35 +04:00
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
if os.name != 'nt':
def output_colorized(self, message):
self.stream.write(message)
else:
import re
ansi_esc = re.compile(r'\x1b\[((?:\d+)(?:;(?:\d+))*)m')
nt_color_map = {
0: 0x00, # black
1: 0x04, # red
2: 0x02, # green
3: 0x06, # yellow
4: 0x01, # blue
5: 0x05, # magenta
6: 0x03, # cyan
7: 0x07, # white
}
def output_colorized(self, message):
2012-07-10 16:13:21 +04:00
import ctypes
2012-07-10 05:47:35 +04:00
parts = self.ansi_esc.split(message)
write = self.stream.write
h = None
fd = getattr(self.stream, 'fileno', None)
2012-07-10 05:47:35 +04:00
if fd is not None:
fd = fd()
2012-07-10 05:47:35 +04:00
if fd in (1, 2): # stdout or stderr
h = ctypes.windll.kernel32.GetStdHandle(-10 - fd)
2012-07-10 05:47:35 +04:00
while parts:
text = parts.pop(0)
2012-07-10 05:47:35 +04:00
if text:
write(text)
2012-07-10 05:47:35 +04:00
if parts:
params = parts.pop(0)
2012-07-10 05:47:35 +04:00
if h is not None:
params = [int(p) for p in params.split(';')]
color = 0
2012-07-10 05:47:35 +04:00
for p in params:
if 40 <= p <= 47:
color |= self.nt_color_map[p - 40] << 4
elif 30 <= p <= 37:
color |= self.nt_color_map[p - 30]
elif p == 1:
color |= 0x08 # foreground intensity on
elif p == 0: # reset to default color
color = 0x07
else:
pass # error condition ignored
2012-07-10 05:47:35 +04:00
ctypes.windll.kernel32.SetConsoleTextAttribute(h, color)
def colorize(self, message, record):
if record.levelno in self.level_map:
bg, fg, bold = self.level_map[record.levelno]
params = []
2012-07-10 05:47:35 +04:00
if bg in self.color_map:
params.append(str(self.color_map[bg] + 40))
2012-07-10 05:47:35 +04:00
if fg in self.color_map:
params.append(str(self.color_map[fg] + 30))
2012-07-10 05:47:35 +04:00
if bold:
params.append('1')
2012-07-10 05:47:35 +04:00
if params:
if message.lstrip() != message:
prefix = re.search(r"\s+", message).group(0)
message = message[len(prefix):]
else:
prefix = ""
message = "%s%s" % (prefix, ''.join((self.csi, ';'.join(params),
'm', message, self.reset)))
2012-07-10 05:47:35 +04:00
return message
def format(self, record):
message = logging.StreamHandler.format(self, record)
2012-07-11 15:18:48 +04:00
return self.colorize(message, record)