/
coloredlogcat.py
executable file
·127 lines (100 loc) · 3.97 KB
/
coloredlogcat.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
#!/usr/bin/python
'''
Copyright 2009, The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
# script to highlight adb logcat output for console
# written by jeff sharkey, http://jsharkey.org/
# piping detection and popen() added by other android team members
# modified by Josh Guilfoyle <jasta@devtcg.org> to simplify formatting back to
# adb logcat original. I just want colorized logcat output, nothing fancier.
import os, sys, re, StringIO
import fcntl, termios, struct
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
def format(fg=None, bg=None, bright=False, bold=False, dim=False, reset=False):
# manually derived from http://en.wikipedia.org/wiki/ANSI_escape_code#Codes
codes = []
if reset: codes.append("0")
else:
if not fg is None: codes.append("3%d" % (fg))
if not bg is None:
if not bright: codes.append("4%d" % (bg))
else: codes.append("10%d" % (bg))
if bold: codes.append("1")
elif dim: codes.append("2")
else: codes.append("22")
return "\033[%sm" % (";".join(codes))
def colorToFormat(color):
return format(fg=color, bold=True)
LAST_USED = map(colorToFormat, [RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE])
KNOWN_TAGS = {
"dalvikvm": format(fg=BLACK, bold=True),
"Process": format(fg=YELLOW, bold=True),
"ActivityManager": format(fg=CYAN, bold=True),
"ActivityThread": format(fg=CYAN, bold=True),
"System.err": format(bg=BLUE),
"System.out": format(bg=BLUE),
}
def allocate_color(tag):
# this will allocate a unique format for the given tag
# since we dont have very many colors, we always keep track of the LRU
if not tag in KNOWN_TAGS:
KNOWN_TAGS[tag] = LAST_USED[0]
color = KNOWN_TAGS[tag]
if color in LAST_USED:
LAST_USED.remove(color)
LAST_USED.append(color)
return color
RULES = {
#re.compile(r"([\w\.@]+)=([\w\.@]+)"): r"%s\1%s=%s\2%s" % (format(fg=BLUE), format(fg=GREEN), format(fg=BLUE), format(reset=True)),
}
TAGTYPE_WIDTH = 1
TAG_WIDTH = 20
PROCESS_WIDTH = 8 # 8 or -1
HEADER_SIZE = TAGTYPE_WIDTH + 1 + TAG_WIDTH + 1 + PROCESS_WIDTH + 1
TAGTYPES = {
"V": "",
"D": format(fg=BLUE, bold=True),
"I": "",
"W": format(fg=YELLOW, bold=True),
"E": format(fg=RED, bold=True),
}
retag = re.compile("^([A-Z])/(.+?)\((\s*\d+)\): (.*)$")
# to pick up -d or -e
adb_args = ' '.join(sys.argv[1:])
# if someone is piping in to us, use stdin as input. if not, invoke adb logcat
if os.isatty(sys.stdin.fileno()):
input = os.popen("adb %s logcat" % adb_args)
else:
input = sys.stdin
while True:
try:
line = input.readline().rstrip()
except KeyboardInterrupt:
break
match = retag.match(line)
if not match is None:
tagtype, tag, owner, message = match.groups()
linebuf = StringIO.StringIO()
# write out tagtype colored edge
if not tagtype in TAGTYPES: break
linebuf.write("%s%s%s" % (TAGTYPES[tagtype], tagtype, format(reset=True)))
colorfmt = allocate_color(tag)
linebuf.write(" / %s%s%s" % (colorfmt, tag, format(reset=True)))
linebuf.write(" (%s): " % owner)
# format tag message using rules
for matcher in RULES:
replace = RULES[matcher]
message = matcher.sub(replace, message)
linebuf.write("%s%s%s" % (colorfmt, message, format(reset=True)))
line = linebuf.getvalue()
print line
if len(line) == 0: break