diff --git a/pidcat.py b/pidcat.py index 4ee9922..fe0e022 100755 --- a/pidcat.py +++ b/pidcat.py @@ -127,11 +127,11 @@ TAGTYPES = { '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_KILL = re.compile(r'^Killing (\d+):([a-zA-Z0-9._]+)/[^:]+: (.*)\r?$') -PID_LEAVE = re.compile(r'^No longer want ([a-zA-Z0-9._]+) \(pid (\d+)\): .*\r?$') -PID_DEATH = re.compile(r'^Process ([a-zA-Z0-9._]+) \(pid (\d+)\) has died.?\r$') -LOG_LINE = re.compile(r'^([A-Z])/([^\(]+)\( *(\d+)\): (.*?)\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._]+)/[^:]+: (.*)$') +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.?$') +LOG_LINE = re.compile(r'^([A-Z])/([^\(]+)\( *(\d+)\): (.*)$') BUG_LINE = re.compile(r'^(?!.*(nativeGetEnabledTags)).*$') adb_command = ['adb'] @@ -169,7 +169,7 @@ def parse_death(tag, message): while adb.poll() is None: try: - line = adb.stdout.readline() + line = adb.stdout.readline().decode('utf-8').strip() except KeyboardInterrupt: break if len(line) == 0: @@ -180,62 +180,64 @@ while adb.poll() is None: continue log_line = LOG_LINE.match(line) - if not log_line is None: - level, tag, owner, message = log_line.groups() + if log_line is None: + continue - start = PID_START.match(message) - if start is not None: - line_package, target, line_pid, line_uid, line_gids = start.groups() + level, tag, owner, message = log_line.groups() - if match_packages(line_package): - pids.add(line_pid) + start = PID_START.match(message) + if start is not None: + line_package, target, line_pid, line_uid, line_gids = start.groups() - linebuf = '\n' - linebuf += colorize(' ' * (header_size - 1), bg=WHITE) - 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 + if match_packages(line_package): + pids.add(line_pid) - dead_pid = parse_death(tag, message) - if dead_pid: - pids.remove(dead_pid) linebuf = '\n' - linebuf += colorize(' ' * (header_size - 1), bg=RED) - linebuf += ' Process %s ended' % dead_pid + linebuf += colorize(' ' * (header_size - 1), bg=WHITE) + 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 - 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) + dead_pid = parse_death(tag, message) + if dead_pid: + pids.remove(dead_pid) + linebuf = '\n' + linebuf += colorize(' ' * (header_size - 1), bg=RED) + linebuf += ' Process %s ended' % dead_pid + linebuf += '\n' 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)