Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.mkdn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
What is this?
-------------

This is a set of scripts to enhance and filter Android's logcat output.

Usually you'll want to run:

`./super-logcat.sh <interesting-process-name-regex>`

So, if your process name is `com.example.myapp`, then running `./super-logcat.sh example` will show all messages related to your application.

Running the `super-logcat.sh` script with no arguments will display the logcat output from every process, piped through the same formatting and coloring algorithm.
22 changes: 9 additions & 13 deletions coloredlogcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def allocate_color(tag):
"E": format(fg=RED, bold=True),
}

TAGTYPENAMES = {
"V": "Verb ",
"D": "Debug",
"I": "Info ",
"W": "WARN ",
"E": "ERROR"
}

retag = re.compile("^([A-Z])/([^\(]+)\(([^\)]+)\): (.*)$")

# to pick up -d or -e
Expand All @@ -104,7 +112,7 @@ def allocate_color(tag):

# write out tagtype colored edge
if not tagtype in TAGTYPES: break
linebuf.write("%s%s%s" % (TAGTYPES[tagtype], tagtype, format(reset=True)))
linebuf.write("%s%s%s" % (TAGTYPES[tagtype], TAGTYPENAMES[tagtype], format(reset=True)))

color = allocate_color(tag)
linebuf.write(" / %s%s%s" % (format(fg=color, bold=True), tag, format(reset=True)))
Expand All @@ -121,15 +129,3 @@ def allocate_color(tag):

print line
if len(line) == 0: break












137 changes: 68 additions & 69 deletions proclogcat
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ use Data::Dumper;

###############################################################################

@ARGV > 0 or usage($0);
@ARGV > 0 and not -t STDIN or usage($0);

# We support tracking multiple process names. Fill %trackingInfo keys with the
# process names, where the values are to be filled with the current pid for
# that process or -1 if it is presumed dead.
my $trackingInfo = { map { $_ => -1 } @ARGV };
# We support tracking multiple process names. Fill @trackedNames with the
# process names to match (via regular expressions). The process IDs that we
# are currently tracking will be stored in %trackedPids.
my @trackedNames = @ARGV;
my %trackedPids = ();

# Flush all writes immediately. This is necessary as we expect this script to
# be placed between two other programs in a pipe which outputs text very slowly
Expand All @@ -38,84 +39,82 @@ $| = 1;

# Lookup the pids of the processes before we start. From then on, rely on
# the ActivityManager to tell us as the processes dies and starts.
my $numPids = get_pids($trackingInfo);
my $numPids = get_pids();
if ($numPids == 0) {
print "- waiting for process ", join(' or ', keys %$trackingInfo), " -\n";
print "- waiting for process ", join(' or ', @trackedNames), " -\n";
}

while (<STDIN>) {
my $line = $_;
my ($level, $tag, $pid, $message) = $line =~
m/^([A-Z])\/(.*?)\(\s*(\d+)\s*\): (.*)$/;

chomp $message;

if ($tag eq 'ActivityManager') {
if ($message =~ m/^Start proc (.*?) .*?: pid=(\d+) /) {
if (exists $trackingInfo->{$1}) {
$trackingInfo->{$1} = $2;
print $line;
}
} elsif ($message =~ m/Process (.*?) \(pid (\d+)\) has died./) {
if (exists $trackingInfo->{$1}) {
$trackingInfo->{$1} = -1;
print $line;
}
}
} elsif (in_list($pid, values %$trackingInfo)) {
print $line;
}
my $line = $_;
my ($level, $tag, $pid, $message) = $line =~
m/^([A-Z])\/(.*?)\(\s*(\d+)\s*\): (.*)$/;

chomp $message;

if ($tag eq 'ActivityManager') {
if ($message =~ m/^Start proc (.*?) .*?: pid=(\d+) /) {
if (match_name($1)) {
$trackedPids{$2} = 1;
print $line;
}
} elsif ($message =~ m/Process (.*?) \(pid (\d+)\) has died./) {
if (match_name($1)) {
$trackedPids{$2} = 0;
print $line;
}
}
} elsif ($trackedPids{$pid}) {
print $line;
}
}

###############################################################################

sub in_list($@) {
my $needle = shift;
my @haystack = @_;

foreach my $hay (@haystack) {
if ($hay eq $needle) {
return 1;
}
}
return 0;
sub get_pids {
my @ps = qx{adb shell ps};
if (@ps == 0) {
return -1;
}
my @columns = split /\s+/, (shift @ps);

# There's a "STATE" column slipped in between WCHAN and NAME that has no
# room for a column...
splice @columns, $#columns, 0, 'STATE';

my $numFound = 0;

foreach (@ps) {
s/\s+$//;
my @data = split /\s+/, $_, scalar @columns;
my %row = map { $_ => (shift @data) } @columns;

if (match_name($row{NAME})) {
$trackedPids{$row{PID}} = 1;
$numFound++;
}
}

return $numFound;
}

sub get_pids {
my $info = shift;
my @ps = qx{adb shell ps};
if (@ps == 0) {
return -1;
}
my @columns = split /\s+/, (shift @ps);

# There's a "STATE" column slipped in between WCHAN and NAME that has no
# room for a column...
splice @columns, $#columns, 0, 'STATE';

my $numFound = 0;

foreach (@ps) {
s/\s+$//;
my @data = split /\s+/, $_, scalar @columns;
my %row = map { $_ => (shift @data) } @columns;

if (exists $info->{$row{NAME}}) {
$info->{$row{NAME}} = $row{PID};
$numFound++;
}
}

return $numFound;
sub match_name {
my $name = shift;
foreach (@trackedNames) {
return 1 if $_ and $name =~ $_;
}
return 0;
}

sub usage {
my $prog = shift;
die <<"EOF"
Usage: adb logcat | $0 <process-name>
my $prog = shift;
die <<"EOF"
Usage: adb logcat | $0 <process-name-regex> [<process2-name-regex> ...]

Process names will be matched against the given regular expression(s),
and only the matching processes will be shown in the output.

Usually, `process-name' is usually the same as your package, but not
necessarily. To make sure, type `adb shell ps' and look through the list.
To find processes you're interested in, type `adb shell ps' and look
through the list.
EOF
}

11 changes: 11 additions & 0 deletions super-logcat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

me=$0
link=$(readlink "$me")
[ ! -z "$link" ] && me=$link

cd "$(dirname "$me")"

[ -z "$1" ] && "$0" '.*' && exit

adb logcat | ./proclogcat "$@" | ./coloredlogcat.py