Class: Tracee::Preprocessors::Colorize

Inherits:
Base
  • Object
show all
Defined in:
lib/tracee/preprocessors/colorize.rb

Constant Summary collapse

COLOR_MAP =
{
  head: :white,
  action: :yellow,
  params: :yellowish,
  redirect: :purple,
  render: :greenish,
  complete: :white,
  raise: :red
}

Instance Method Summary collapse

Methods inherited from Base

#halt!, #inspect

Constructor Details

#initialize(color_map = COLOR_MAP) ⇒ Colorize

Returns a new instance of Colorize.



13
14
15
16
17
# File 'lib/tracee/preprocessors/colorize.rb', line 13

def initialize(color_map=COLOR_MAP)
  @color_map = color_map
  exception_classes = Exception.descendants
  @exception_classes_re = Exception.descendants.map(&:name).join '|'
end

Instance Method Details

#call(msg_level, datetime, progname, msg, caller_slice = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tracee/preprocessors/colorize.rb', line 19

def call(msg_level, datetime, progname, msg, caller_slice=[])
  case msg
  when %r{^Started (?<method>[A-Z]+) "(?<path>/[^"]*)" for (?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) at (?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?: \+\d{4})?)}
    m = $~
    %{Started #{m[:method].send @color_map[:head]} "#{m[:path].send @color_map[:head]}" for #{m[:ip].send @color_map[:head]} at #{m[:time].send @color_map[:head]}}
  when %r{^Processing by (?<class>[A-Z][\w:]+)#(?<method>\w+) as (?<format>[A-Z]+)$}
    m = $~
    %{Processing by #{m[:class].send @color_map[:action]}##{m[:method].send @color_map[:action]} as #{m[:format].send @color_map[:action]}}
  when %r{^  Parameters: (.+)$}
    m = $~
    "  Parameters: #{m[1].send @color_map[:params]}"
  when %r{^Redirected to (\S+)$}
    m = $~
    "Redirected to #{m[1].send @color_map[:redirect]}"
  when %r{^  Rendered .+ \(\d+\.\dms\)$}
    msg.send @color_map[:render]
  when %r{^Completed (?<code>\d{3}) (?<codename>[A-Z][\w ]+) in (?<time>\d+ms) (?<times>.+)$}
    m = $~
    #"\e[4mCompleted #{m[:code].send @color_map[:complete]}\e[4m #{m[:codename].send @color_map[:complete]}\e[4m in #{m[:time].send @color_map[:complete]}\e[4mms #{m[:times]}\e[0m"
    "Completed #{m[:code].send @color_map[:complete]} #{m[:codename].send @color_map[:complete]} in #{m[:time].send @color_map[:complete]} #{m[:times]}"
  when %r{^(BetterErrors::RaisedException|#@exception_classes_re) (.+)}
    m = $~
    "#{m[1].send @color_map[:raise]} #{m[2]}"
  else
    msg
  end
end