Class: Cabin::NiceOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/cabin/nice_output.rb

Constant Summary collapse

CODEMAP =
{
  :normal => "\e[0m",
  :red => "\e[1;31m",
  :green => "\e[1;32m",
  :yellow => "\e[0;33m",
  :white => "\e[0;37m",
  :blue => "\e[1;34m"
}
DIM_CODEMAP =
{
  red:   "\e[0;31m",
  green: "\e[0;32m",
  white: "\e[1;30m",
  yellow: "\e[33m",
  blue: "\e[0;34m"
}
LEVELMAP =
{
  :fatal => :red,
  :error => :red,
  :warn => :yellow,
  :info => :green,
  :hint => :blue,
  :debug => :white,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ NiceOutput

Returns a new instance of NiceOutput.



32
33
34
# File 'lib/cabin/nice_output.rb', line 32

def initialize(io)
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



30
31
32
# File 'lib/cabin/nice_output.rb', line 30

def io
  @io
end

Instance Method Details

#<<(event) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cabin/nice_output.rb', line 36

def <<(event)
  data = event.clone
  if data[:exception].respond_to? :data
    ed = data[:exception].data
    if ed.kind_of? Hash
      data = ed.merge( data )
    else
      data[:exception_data] = ed.inspect
    end
  end
  data.delete(:line)
  data.delete(:file)
  level = data.delete(:level) || :normal
  data.delete(:message)
  ts = data.delete(:timestamp)

  color = data.delete(:color)
  # :bold is expected to be truthy
  bold = data.delete(:bold) ? :bold : nil

  backtrace = data.delete(:backtrace)
  if !backtrace && data[:exception].respond_to?(:backtrace)
    backtrace = data[:exception].backtrace
  end

  # Make 'error' and other log levels have color
  if color.nil?
    color = LEVELMAP[level]
  end

  message = [event[:level] ? '====> ' : '      ',event[:message]]
  message.unshift(CODEMAP[color.to_sym]) if !color.nil?
  message << DIM_CODEMAP[color] if !color.nil?
  if documentation = data.delete(:documentation)
    message << "\n\tRead more on this topic here: #{documentation}"
  end
  if data.any?
    message << "\n" <<  pp(data)
  end
  if backtrace
    message << "\n\t--backtrace---------------\n\t" << backtrace.join("\n\t")
  end
  message << CODEMAP[:normal]  if !color.nil?
  @io.puts(message.join(""))
  @io.flush
end