Class: Beaker::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/logger.rb

Constant Summary collapse

NORMAL =
"\e[00;00m"
BRIGHT_NORMAL =
"\e[00;01m"
BLACK =
"\e[00;30m"
RED =
"\e[00;31m"
GREEN =
"\e[00;32m"
YELLOW =
"\e[00;33m"
BLUE =
"\e[00;34m"
MAGENTA =
"\e[00;35m"
CYAN =
"\e[00;36m"
WHITE =
"\e[00;37m"
GREY =
"\e[01;30m"
BRIGHT_RED =
"\e[01;31m"
BRIGHT_GREEN =
"\e[01;32m"
BRIGHT_YELLOW =
"\e[01;33m"
BRIGHT_BLUE =
"\e[01;34m"
BRIGHT_MAGENTA =
"\e[01;35m"
BRIGHT_CYAN =
"\e[01;36m"
BRIGHT_WHITE =
"\e[01;37m"
LOG_LEVELS =
{
  :debug  => 1,
  :warn   => 2,
  :normal => 3,
  :info   => 4
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Logger

Returns a new instance of Logger.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/beaker/logger.rb', line 31

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  @color = options[:color]
  @log_level = options[:debug] ? :debug : :normal
  @destinations = []

  dests = args
  dests << STDOUT unless options[:quiet]
  dests.uniq!
  dests.each {|dest| add_destination(dest)}
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



29
30
31
# File 'lib/beaker/logger.rb', line 29

def color
  @color
end

#destinationsObject

Returns the value of attribute destinations.



29
30
31
# File 'lib/beaker/logger.rb', line 29

def destinations
  @destinations
end

#log_levelObject

Returns the value of attribute log_level.



29
30
31
# File 'lib/beaker/logger.rb', line 29

def log_level
  @log_level
end

Instance Method Details

#add_destination(dest) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/beaker/logger.rb', line 43

def add_destination(dest)
  case dest
  when IO
    @destinations << dest
  when String
    @destinations << File.open(dest, 'w')
  else
    raise "Unsuitable log destination #{dest.inspect}"
  end
end

#debug(*args) ⇒ Object



80
81
82
83
# File 'lib/beaker/logger.rb', line 80

def debug *args
  return unless is_debug?
  optionally_color WHITE, args
end

#error(*args) ⇒ Object



99
100
101
# File 'lib/beaker/logger.rb', line 99

def error *args
  optionally_color BRIGHT_RED, args
end

#host_output(*args) ⇒ Object



73
74
75
76
77
78
# File 'lib/beaker/logger.rb', line 73

def host_output *args
  return unless is_debug?
  strings = strip_colors_from args
  string = strings.join
  optionally_color GREY, string, false
end

#is_debug?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/beaker/logger.rb', line 65

def is_debug?
  LOG_LEVELS[@log_level] <= LOG_LEVELS[:debug]
end

#is_warn?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/beaker/logger.rb', line 69

def is_warn?
  LOG_LEVELS[@log_level] <= LOG_LEVELS[:warn]
end

#notify(*args) ⇒ Object



95
96
97
# File 'lib/beaker/logger.rb', line 95

def notify *args
  optionally_color BRIGHT_WHITE, args
end

#optionally_color(color_code, msg, add_newline = true) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/beaker/logger.rb', line 109

def optionally_color color_code, msg, add_newline = true
  print_statement = add_newline ? :puts : :print
  @destinations.each do |to|
    to.print color_code if @color
    to.send print_statement, msg
    to.print NORMAL if @color
  end
end

#pretty_backtrace(backtrace = caller(1)) ⇒ Object

utility method to get the current call stack and format it to a human-readable string (which some IDEs/editors will recognize as links to the line numbers in the trace)



121
122
123
124
# File 'lib/beaker/logger.rb', line 121

def pretty_backtrace backtrace = caller(1)
  trace = purge_harness_files_from( Array( backtrace ) )
  expand_symlinks( trace ).join "\n"
end

#remove_destination(dest) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/beaker/logger.rb', line 54

def remove_destination(dest)
  case dest
  when IO
    @destinations.delete(dest)
  when String
    @destinations.delete_if {|d| d.respond_to?(:path) and d.path == dest}
  else
    raise "Unsuitable log destination #{dest.inspect}"
  end
end

#strip_colors_from(lines) ⇒ Object



103
104
105
106
107
# File 'lib/beaker/logger.rb', line 103

def strip_colors_from lines
  Array(lines).map do |line|
    line.gsub /\e\[(\d+;)?\d+m/, ''
  end
end

#success(*args) ⇒ Object



91
92
93
# File 'lib/beaker/logger.rb', line 91

def success *args
  optionally_color GREEN, args
end

#warn(*args) ⇒ Object



85
86
87
88
89
# File 'lib/beaker/logger.rb', line 85

def warn *args
  return unless is_warn?
  strings = args.map {|msg| "Warning: #{msg}" }
  optionally_color YELLOW, strings
end