Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/thirdparty/ansistrm/ansistrm.py
2992 views
1
#
2
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
3
# (Note: 2018 modifications by @stamparm)
4
#
5
6
import logging
7
import re
8
import sys
9
10
from lib.core.settings import IS_WIN
11
12
if IS_WIN:
13
import ctypes
14
import ctypes.wintypes
15
16
# Reference: https://gist.github.com/vsajip/758430
17
# https://github.com/ipython/ipython/issues/4252
18
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
19
ctypes.windll.kernel32.SetConsoleTextAttribute.argtypes = [ctypes.wintypes.HANDLE, ctypes.wintypes.WORD]
20
ctypes.windll.kernel32.SetConsoleTextAttribute.restype = ctypes.wintypes.BOOL
21
22
def stdoutEncode(data): # Cross-referenced function
23
return data
24
25
class ColorizingStreamHandler(logging.StreamHandler):
26
# color names to indices
27
color_map = {
28
'black': 0,
29
'red': 1,
30
'green': 2,
31
'yellow': 3,
32
'blue': 4,
33
'magenta': 5,
34
'cyan': 6,
35
'white': 7,
36
}
37
38
# levels to (background, foreground, bold/intense)
39
level_map = {
40
logging.DEBUG: (None, 'blue', False),
41
logging.INFO: (None, 'green', False),
42
logging.WARNING: (None, 'yellow', False),
43
logging.ERROR: (None, 'red', False),
44
logging.CRITICAL: ('red', 'white', False)
45
}
46
csi = '\x1b['
47
reset = '\x1b[0m'
48
bold = "\x1b[1m"
49
disable_coloring = False
50
51
@property
52
def is_tty(self):
53
isatty = getattr(self.stream, 'isatty', None)
54
return isatty and isatty() and not self.disable_coloring
55
56
def emit(self, record):
57
try:
58
message = stdoutEncode(self.format(record))
59
stream = self.stream
60
61
if not self.is_tty:
62
if message and message[0] == "\r":
63
message = message[1:]
64
stream.write(message)
65
else:
66
self.output_colorized(message)
67
stream.write(getattr(self, 'terminator', '\n'))
68
69
self.flush()
70
except (KeyboardInterrupt, SystemExit):
71
raise
72
except IOError:
73
pass
74
except:
75
self.handleError(record)
76
77
if not IS_WIN:
78
def output_colorized(self, message):
79
self.stream.write(message)
80
else:
81
ansi_esc = re.compile(r'\x1b\[((?:\d+)(?:;(?:\d+))*)m')
82
83
nt_color_map = {
84
0: 0x00, # black
85
1: 0x04, # red
86
2: 0x02, # green
87
3: 0x06, # yellow
88
4: 0x01, # blue
89
5: 0x05, # magenta
90
6: 0x03, # cyan
91
7: 0x07, # white
92
}
93
94
def output_colorized(self, message):
95
parts = self.ansi_esc.split(message)
96
h = None
97
fd = getattr(self.stream, 'fileno', None)
98
99
if fd is not None:
100
fd = fd()
101
102
if fd in (1, 2): # stdout or stderr
103
h = ctypes.windll.kernel32.GetStdHandle(-10 - fd)
104
105
while parts:
106
text = parts.pop(0)
107
108
if text:
109
self.stream.write(text)
110
self.stream.flush()
111
112
if parts:
113
params = parts.pop(0)
114
115
if h is not None:
116
params = [int(p) for p in params.split(';')]
117
color = 0
118
119
for p in params:
120
if 40 <= p <= 47:
121
color |= self.nt_color_map[p - 40] << 4
122
elif 30 <= p <= 37:
123
color |= self.nt_color_map[p - 30]
124
elif p == 1:
125
color |= 0x08 # foreground intensity on
126
elif p == 0: # reset to default color
127
color = 0x07
128
else:
129
pass # error condition ignored
130
131
ctypes.windll.kernel32.SetConsoleTextAttribute(h, color)
132
133
def _reset(self, message):
134
if not message.endswith(self.reset):
135
reset = self.reset
136
elif self.bold in message: # bold
137
reset = self.reset + self.bold
138
else:
139
reset = self.reset
140
141
return reset
142
143
def colorize(self, message, levelno):
144
if levelno in self.level_map and self.is_tty:
145
bg, fg, bold = self.level_map[levelno]
146
params = []
147
148
if bg in self.color_map:
149
params.append(str(self.color_map[bg] + 40))
150
151
if fg in self.color_map:
152
params.append(str(self.color_map[fg] + 30))
153
154
if bold:
155
params.append('1')
156
157
if params and message:
158
if message.lstrip() != message:
159
prefix = re.search(r"\s+", message).group(0)
160
message = message[len(prefix):]
161
else:
162
prefix = ""
163
164
message = "%s%s" % (prefix, ''.join((self.csi, ';'.join(params),
165
'm', message, self.reset)))
166
167
return message
168
169
def format(self, record):
170
message = logging.StreamHandler.format(self, record)
171
return self.colorize(message, record.levelno)
172
173