Skip to content

Commit

Permalink
Supporting coloring logcat output from file.
Browse files Browse the repository at this point in the history
If passed one argument which is a file then logcat will read/colorize
that files contents and write to stdout. Otherwise will behave as
before by colorizing output of "adb logcat".
  • Loading branch information
Chris Mumford committed Jun 10, 2013
1 parent ece3655 commit 83dd233
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions adb-logcat-color.rb
Expand Up @@ -3,32 +3,44 @@
require 'rubygems'
require 'smart_colored/extend'

patterns = {"V/" => {:fg => "black", :bg => "blue", :label => "[ VERBOSE ]"},
$patterns = {"V/" => {:fg => "black", :bg => "blue", :label => "[ VERBOSE ]"},
"D/" => {:fg => "black", :bg => "cyan", :label => "[ DEBUG ]"},
"I/" => {:fg => "black", :bg => "green", :label => "[ INFO ]"},
"W/" => {:fg => "black", :bg => "yellow", :label => "[ WARNING ]"},
"E/" => {:fg => "black", :bg => "red", :label => "[ ERROR ]"},
"F/" => {:fg => "black", :bg => "magenta", :label => "[ FATAL ]"},
"S/" => {:fg => "black", :bg => "white", :label => "[ SILENT ]"}}

def colorize_line(line)
line.sub!(/\(\s*\d+\)/, "")
line.gsub!("\r\n", "")

# Ruby 1.9 support passing an array - move to this eventually
IO.popen("adb logcat #{ARGV.join(' ')}") do |f|
while line = f.gets
line.sub!(/\(\s*\d+\)/, "")
line.gsub!("\r\n", "")
match = $patterns.keys.find do |x|
line.start_with?(x)
end

match = patterns.keys.find do |x|
line.start_with?(x)
end
if match
line.sub!(match, " => ")
label = $patterns[match][:label]
fg = $patterns[match][:fg]
bg = $patterns[match][:bg]
print label.send("#{fg}_on_#{bg}")
puts line.send("#{bg}_on_#{fg}")
end
end

if match
line.sub!(match, " => ")
label = patterns[match][:label]
fg = patterns[match][:fg]
bg = patterns[match][:bg]
print label.send("#{fg}_on_#{bg}")
puts line.send("#{bg}_on_#{fg}")
use_argf=ARGV.length == 1 and File.exists?(ARGV[0])

if use_argf then
ARGF.each do |line|
colorize_line(line)
end
else
# Ruby 1.9 support passing an array - move to this eventually
IO.popen("adb logcat #{ARGV.join(' ')}") do |f|
while line = f.gets
colorize_line(line)
end
end
end

0 comments on commit 83dd233

Please sign in to comment.