-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcolorize.py
executable file
·199 lines (164 loc) · 4.57 KB
/
colorize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/python -u
# -*- coding: utf-8 -*-
""" Template python program
Usage
Usage Text
Help
Help extract
Requirements
python-argparse
"""
import sys
import re
import time
class Color:
black = 0
red = 160
green = 28
yellow = 220
blue = 12
purple = 126
cyan = 45
grey = 239
normal = 0
bold = 1
def colorize(s,color):
if type(color) is tuple:
color,modif = color
return "\033[%dm\x1b[38;5;%dm%s\x1b[0m\033[0m" % (modif,color,s)
else:
return "\x1b[38;5;%dm%s\x1b[0m" % (color,s)
DBG = 0x0f
DBG1 = 0x0e
DBG2 = 0x0d
INFO = 0x10
NOTICE = 0x20
WRN = 0x30
ERR = 0x40
ASSERT = 0xFF
SPEC = 0xFE
class Log:
def is_my_log(self,l):
return True
def sanitize(self,l):
return l.strip()
def output(self,l):
l = self.sanitize(l)
if not self.is_my_log(l):
pass
else:
out = self.parse(l)
if out is not None:
self.write("%s\n" % (out,))
self.flush()
def parse(self,l):
return l
def write(self,s):
sys.stdout.write(s)
def flush(self):
sys.stdout.flush()
class ProxyLog(Log):
def is_my_log(self,l):
return "Proxy Gadget" in l
class USBMonLog(Log):
STYLE = {
"Ci":Color.red,
"Co":Color.purple,
"Ii":Color.blue,
"Io":Color.cyan,
"Bi":Color.green,
"Bo":Color.yellow,
"Zi":47,
"Zo":172,
}
def parse(self,l):
for t,c in USBMonLog.STYLE.iteritems():
if t in l:
return colorize(l,c)
else:
return None
class USBMITMLog(Log):
PATTERN = "\[\s*(?P<lvl>\d+)\]\s+(?P<module>[^\s]+)\s*(?P<log>.*$)"
MASTER_STYLE = {
("GADGET",DBG) : Color.green,
("DRIVER",DBG) : Color.blue,
("GADGET",INFO) : (Color.green,Color.bold),
("DRIVER",INFO) : (Color.blue,Color.bold),
}
SECOND_STYLE = {
ASSERT : (Color.red,Color.bold),
SPEC : (214,Color.bold),
ERR : (Color.red,Color.bold),
WRN : Color.red,
INFO : Color.yellow,
"musb" : Color.purple
}
def __init__(self):
self.pattern = re.compile(USBMITMLog.PATTERN)
def is_my_log(self,l):
return (len(l) > 53 and l[self.START_LOG] == "[" and l[self.START_LOG+4] == "]") or "musb" in l or "B64" in l
def parse(self,l):
l = l[self.START_LOG:]
m = self.pattern.match(l)
if not m:
if "musb" in l or "B64" in l:
lvl = "musb"
module = "MUSB"
log = l
else:
return None
else:
lvl = int(m.group("lvl"))
module = m.group("module")
log = m.group("log")
return self.colorize(lvl,module,log)
def colorize(self,lvl,module,log):
if (module,lvl) in USBMITMLog.MASTER_STYLE:
return colorize(log,USBMITMLog.MASTER_STYLE[(module,lvl)])
elif lvl in USBMITMLog.SECOND_STYLE:
return colorize("%s %s" % (module,log), USBMITMLog.SECOND_STYLE[lvl])
else:
return "[%s] %s %s" % (str(lvl).rjust(3," "),module,log)
class IGEPMITMLog(USBMITMLog):
START_LOG=48
class BEAGLEMITMLog(USBMITMLog):
START_LOG=50
class NetconsoleLog(USBMITMLog):
START_LOG=15
def parse_args():
""" Parse command line arguments """
try:
import argparse
except:
print "python-argparse is needed"
sys.exit(0)
parser = argparse.ArgumentParser(description="Description of python program")
parser.add_argument("data",metavar="INTEGERS",nargs="*")
parser.add_argument("--param","-p",metavar="PARAM",type=int,default=1,help="Example of parameter argument")
parser.add_argument("--proxy",action="store_true",help="Log for proxy")
parser.add_argument("--usbmon",action="store_true",help="Log for usbmon")
parser.add_argument("--igep",action="store_true",help="Log for igep")
parser.add_argument("--netconsole",action="store_true",help="Colorize netconsole")
return parser.parse_args()
def main():
""" Entry Point Program """
args = parse_args()
if args.proxy:
log = ProxyLog
elif args.usbmon:
log = USBMonLog
elif args.igep:
log = IGEPMITMLog
elif args.netconsole:
log = NetconsoleLog
else:
log = BEAGLEMITMLog
while 1:
line = sys.stdin.readline()
if not line:
time.sleep(1)
else:
log().output(line)
return 0
if __name__ == "__main__":
sys.exit(main())