UTF-8 decode and trim each line.

Closes #18.
This commit is contained in:
Jake Wharton
2013-06-19 14:18:52 -07:00
parent 106a4ceacd
commit 571826e8c0
+57 -55
View File
@@ -127,11 +127,11 @@ TAGTYPES = {
'F': colorize(' F ', fg=BLACK, bg=RED), 'F': colorize(' F ', fg=BLACK, bg=RED),
} }
PID_START = re.compile(r'^Start proc ([a-zA-Z0-9._:]+) for ([a-z]+ [^:]+): pid=(\d+) uid=(\d+) gids=(.*)\r?$') PID_START = re.compile(r'^Start proc ([a-zA-Z0-9._:]+) for ([a-z]+ [^:]+): pid=(\d+) uid=(\d+) gids=(.*)$')
PID_KILL = re.compile(r'^Killing (\d+):([a-zA-Z0-9._]+)/[^:]+: (.*)\r?$') PID_KILL = re.compile(r'^Killing (\d+):([a-zA-Z0-9._]+)/[^:]+: (.*)$')
PID_LEAVE = re.compile(r'^No longer want ([a-zA-Z0-9._]+) \(pid (\d+)\): .*\r?$') PID_LEAVE = re.compile(r'^No longer want ([a-zA-Z0-9._]+) \(pid (\d+)\): .*$')
PID_DEATH = re.compile(r'^Process ([a-zA-Z0-9._]+) \(pid (\d+)\) has died.?\r$') PID_DEATH = re.compile(r'^Process ([a-zA-Z0-9._]+) \(pid (\d+)\) has died.?$')
LOG_LINE = re.compile(r'^([A-Z])/([^\(]+)\( *(\d+)\): (.*?)\r?$') LOG_LINE = re.compile(r'^([A-Z])/([^\(]+)\( *(\d+)\): (.*)$')
BUG_LINE = re.compile(r'^(?!.*(nativeGetEnabledTags)).*$') BUG_LINE = re.compile(r'^(?!.*(nativeGetEnabledTags)).*$')
adb_command = ['adb'] adb_command = ['adb']
@@ -169,7 +169,7 @@ def parse_death(tag, message):
while adb.poll() is None: while adb.poll() is None:
try: try:
line = adb.stdout.readline() line = adb.stdout.readline().decode('utf-8').strip()
except KeyboardInterrupt: except KeyboardInterrupt:
break break
if len(line) == 0: if len(line) == 0:
@@ -180,62 +180,64 @@ while adb.poll() is None:
continue continue
log_line = LOG_LINE.match(line) log_line = LOG_LINE.match(line)
if not log_line is None: if log_line is None:
level, tag, owner, message = log_line.groups() continue
start = PID_START.match(message) level, tag, owner, message = log_line.groups()
if start is not None:
line_package, target, line_pid, line_uid, line_gids = start.groups()
if match_packages(line_package): start = PID_START.match(message)
pids.add(line_pid) if start is not None:
line_package, target, line_pid, line_uid, line_gids = start.groups()
linebuf = '\n' if match_packages(line_package):
linebuf += colorize(' ' * (header_size - 1), bg=WHITE) pids.add(line_pid)
linebuf += indent_wrap(' Process created for %s\n' % target)
linebuf += colorize(' ' * (header_size - 1), bg=WHITE)
linebuf += ' PID: %s UID: %s GIDs: %s' % (line_pid, line_uid, line_gids)
linebuf += '\n'
print(linebuf)
last_tag = None # Ensure next log gets a tag printed
dead_pid = parse_death(tag, message)
if dead_pid:
pids.remove(dead_pid)
linebuf = '\n' linebuf = '\n'
linebuf += colorize(' ' * (header_size - 1), bg=RED) linebuf += colorize(' ' * (header_size - 1), bg=WHITE)
linebuf += ' Process %s ended' % dead_pid linebuf += indent_wrap(' Process created for %s\n' % target)
linebuf += colorize(' ' * (header_size - 1), bg=WHITE)
linebuf += ' PID: %s UID: %s GIDs: %s' % (line_pid, line_uid, line_gids)
linebuf += '\n' linebuf += '\n'
print(linebuf) print(linebuf)
last_tag = None # Ensure next log gets a tag printed last_tag = None # Ensure next log gets a tag printed
if owner not in pids: dead_pid = parse_death(tag, message)
continue if dead_pid:
if LOG_LEVELS_MAP[level] < min_level: pids.remove(dead_pid)
continue linebuf = '\n'
linebuf += colorize(' ' * (header_size - 1), bg=RED)
linebuf = '' linebuf += ' Process %s ended' % dead_pid
linebuf += '\n'
# right-align tag title and allocate color if needed
tag = tag.strip()
if tag != last_tag:
last_tag = tag
color = allocate_color(tag)
tag = tag[-args.tag_width:].rjust(args.tag_width)
linebuf += colorize(tag, fg=color)
else:
linebuf += ' ' * args.tag_width
linebuf += ' '
# write out level colored edge
if level not in TAGTYPES: break
linebuf += TAGTYPES[level]
linebuf += ' '
# format tag message using rules
for matcher in RULES:
replace = RULES[matcher]
message = matcher.sub(replace, message)
linebuf += indent_wrap(message)
print(linebuf) print(linebuf)
last_tag = None # Ensure next log gets a tag printed
if owner not in pids:
continue
if LOG_LEVELS_MAP[level] < min_level:
continue
linebuf = ''
# right-align tag title and allocate color if needed
tag = tag.strip()
if tag != last_tag:
last_tag = tag
color = allocate_color(tag)
tag = tag[-args.tag_width:].rjust(args.tag_width)
linebuf += colorize(tag, fg=color)
else:
linebuf += ' ' * args.tag_width
linebuf += ' '
# write out level colored edge
if level not in TAGTYPES: break
linebuf += TAGTYPES[level]
linebuf += ' '
# format tag message using rules
for matcher in RULES:
replace = RULES[matcher]
message = matcher.sub(replace, message)
linebuf += indent_wrap(message)
print(linebuf)