class LogMuncher

Constants

LOGFILE
PROCESS_PATTERN
TIMESTAMP_PATTERN

Attributes

current_pid[RW]

Public Class Methods

new(repl) click to toggle source
# File lib/replicant/log_muncher.rb, line 11
def initialize(repl)
  @repl = repl
  @current_pid = nil
end

Public Instance Methods

munch_logs() { |o| ... } click to toggle source

Parses the device logs and reformats / recolors them

# File lib/replicant/log_muncher.rb, line 17
def munch_logs
  logcat = "logcat -v time"

  i = IO.popen(AdbCommand.new(@repl, logcat).command)
  o = File.open(LOGFILE, 'wt')

  yield o if block_given?

  Thread.new do
    begin
      i.each_line do |line|
        log_line(o, line)
      end
    rescue Exception => e
      puts e.inspect
      puts e.backtrace.join("\n")
      raise e
    end
  end
end

Private Instance Methods

log_line(o, line) click to toggle source
# File lib/replicant/log_muncher.rb, line 38
        def log_line(o, line)
  transform_line(line).each do |seg|
    text = seg.first
    styles = seg.last
    o.print(create_style(*styles))
    o.print(text)
    o.print(end_style)
  end
  o.flush
end
transform_line(line) click to toggle source
# File lib/replicant/log_muncher.rb, line 49
        def transform_line(line)
  segments = []
  
  timestamp = line[TIMESTAMP_PATTERN]

  if timestamp # found proper log line
    process = PROCESS_PATTERN.match(line)
    pid = process[3]

    if @current_pid.nil? || @current_pid == pid
      segments << [" #{timestamp} ", [:white_bg, :bold]]
      # log level
      level = process[1]
      level_fg = case level
      when "D" then :yellow_fg
      when "E" then :red_fg
      else :white_fg
      end
      segments << [" #{level} ", [:black_bg, :bold] << level_fg]
      # log tag
      tag = process[2].strip
      segments << ["#{tag} ", [:black_bg, :cyan_fg, :bold]]
      # log remaining line
      remainder = [TIMESTAMP_PATTERN, PROCESS_PATTERN].reduce(line) { |l,r| l.gsub(r, '') }.strip
      segments << [" #{remainder}\n", [:white_fg]]

    elsif @repl.debug?
      segments << [" #{timestamp} ", [:black_fg]]
      segments << [" [muted]\n", [:black_fg]]
    end
  end
  segments
end