#!/usr/bin/python -u ''' 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 # Package name restriction by Jake Wharton, http://jakewharton.com import argparse import os import sys import re import subprocess from subprocess import PIPE parser = argparse.ArgumentParser(description='Filter logcat by package name') parser.add_argument('package', nargs='+', help='Application package name(s)') parser.add_argument('--tag-width', metavar='N', dest='tag_width', type=int, default=22, help='Width of log tag') parser.add_argument('--color-gc', dest='color_gc', action='store_true', help='Color garbage collection') args = parser.parse_args() header_size = args.tag_width + 1 + 3 + 1 # space, level, space width = -1 try: # Get the current terminal width import fcntl, termios, struct h, width = struct.unpack('hh', fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('hh', 0, 0))) except: pass BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) RESET = '\033[0m' def termcolor(fg=None, bg=None): codes = [] if fg is not None: codes.append('3%d' % fg) if bg is not None: codes.append('10%d' % bg) return '\033[%sm' % ';'.join(codes) if codes else '' def colorize(message, fg=None, bg=None): return termcolor(fg, bg) + message + RESET def indent_wrap(message): if width == -1: return message wrap_area = width - header_size messagebuf = '' current = 0 while current < len(message): next = min(current + wrap_area, len(message)) messagebuf += message[current:next] if next < len(message): messagebuf += '\n' messagebuf += ' ' * header_size current = next return messagebuf LAST_USED = [RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN] KNOWN_TAGS = { 'dalvikvm': WHITE, 'Process': WHITE, 'ActivityManager': WHITE, 'ActivityThread': WHITE, 'AndroidRuntime': CYAN, 'jdwp': WHITE, 'StrictMode': WHITE, } 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 tag not 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 = { # StrictMode policy violation; ~duration=319 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=31 violation=1 re.compile(r'^(StrictMode policy violation)(; ~duration=)(\d+ ms)') : r'%s\1%s\2%s\3%s' % (termcolor(RED), RESET, termcolor(YELLOW), RESET), } # Only enable GC coloring if the user opted-in if args.color_gc: # GC_CONCURRENT freed 3617K, 29% free 20525K/28648K, paused 4ms+5ms, total 85ms key = re.compile(r'^(GC_(?:CONCURRENT|FOR_M?ALLOC|EXTERNAL_ALLOC|EXPLICIT) )(freed