Class: Beaker::Logger

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

Overview

The Beaker Logger class This class handles message reporting for Beaker, it reports based upon a provided log level to a given destination (be it a string or file)

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 =

The defined log levels. Each log level also reports messages at levels lower than itself

{
  :debug   => 5,
  :verbose => 3,
  :info    => 2,
  :notify  => 1,
  :warn    => 0,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dests) ⇒ Logger #initialize(dests, options) ⇒ Logger

Initialization of the Logger class

Overloads:

  • #initialize(dests) ⇒ Logger

    Initialize a Logger object that reports to the provided destinations, use default options

    Parameters:

    • Array (Array<String, IO>)

      of IO and strings (assumed to be file paths) to be reported to

  • #initialize(dests, options) ⇒ Logger

    Initialize a Logger object that reports to the provided destinations, use options from provided option hash

    Parameters:

    • Array (Array<String, IO>)

      of IO and strings (assumed to be file paths) to be reported to

    • options (Hash)

      Hash of options

    Options Hash (options):

    • :color (Boolean) — default: true

      Print color code before log messages

    • :quiet (Boolean) — default: false

      Do not log messages to STDOUT

    • :log_level (String) — default: "info"

      Log level (one of “debug” - highest level, “verbose”, “info”, “notify” and “warn” - lowest level (see LOG_LEVELS)) The log level indicates that messages at that log_level and lower will be reported.



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
# File 'lib/beaker/logger.rb', line 50

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  @color = options[:color]
  @sublog = nil
  case options[:log_level]
  when /debug/i, :debug
    @log_level = :debug
  when /verbose/i, :verbose
    @log_level = :verbose
  when /info/i, :info
    @log_level = :info
  when /notify/i, :notify
    @log_level = :notify
  when /warn/i, :warn
    @log_level = :warn
  else
    @log_level = :verbose
  end
  @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.



35
36
37
# File 'lib/beaker/logger.rb', line 35

def color
  @color
end

#destinationsObject

Returns the value of attribute destinations.



35
36
37
# File 'lib/beaker/logger.rb', line 35

def destinations
  @destinations
end

#log_levelObject

Returns the value of attribute log_level.



35
36
37
# File 'lib/beaker/logger.rb', line 35

def log_level
  @log_level
end

Instance Method Details

#add_destination(dest) ⇒ Object

Construct an array of open steams for printing log messages to

Parameters:

  • dest (Array<IO, String>)

    Array of strings (each used as a file path) and IO steams that messages will be printed to



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/beaker/logger.rb', line 78

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

#convert(string) ⇒ Object

Remove invalid UTF-8 codes from provided string(s)

Parameters:

  • string (String, Array<String>)

    The string(s) to remove invalid codes from



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/beaker/logger.rb', line 138

def convert string
  if string.kind_of?(Array)
    string.map do |s|
      convert s
    end
  else
    if string.respond_to?( :encode )
      # We're running in >= 1.9 and we'll need to convert
      # Remove invalid and undefined UTF-8 character encodings
      encoding = Encoding ? Encoding.default_external : "UTF-8"
      return string.encode(encoding, string.encoding, :invalid => :replace, :undef => :replace, :replace => '')
    else
      # We're running 1.8, do nothing
      string
    end
  end
end

#debug(*args) ⇒ Object

Report a debug message. Will not print unless we are at LOG_LEVELS ‘debug’ or higher.

Parameters:

  • args (Array<String>)

    Strings to be reported



170
171
172
173
# File 'lib/beaker/logger.rb', line 170

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

#error(*args) ⇒ Object

Report an error message. Will always be reported.

Parameters:

  • args (Array<String>)

    Strings to be reported



211
212
213
# File 'lib/beaker/logger.rb', line 211

def error *args
  optionally_color BRIGHT_RED, *args
end

#get_sublogObject

Return the contents of the sublog



258
259
260
261
# File 'lib/beaker/logger.rb', line 258

def get_sublog
  @sublog.rewind
  @sublog.read
end

#host_output(*args) ⇒ Object

Custom reporting for messages generated by host SUTs. Will not print unless we are at LOG_LEVELS ‘verbose’ or higher. Strips any color codes already in the provided messages, then adds logger color codes before reporting

Parameters:

  • args (Array<String>)

    Strings to be reported



160
161
162
163
164
165
# File 'lib/beaker/logger.rb', line 160

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

#info(*args) ⇒ Object

Report an info message. Will not print unless we are at LOG_LEVELS ‘info’ or higher.

Parameters:

  • args (Array<String>)

    Strings to be reported



188
189
190
191
# File 'lib/beaker/logger.rb', line 188

def info *args
  return unless is_info?
  optionally_color BLUE, *args
end

#is_debug?Boolean

Are we at LOG_LEVELS debug?

Returns:

  • (Boolean)

    true if ‘debug’ or higher, false if not ‘debug’ LOG_LEVELS or lower



108
109
110
# File 'lib/beaker/logger.rb', line 108

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

#is_info?Boolean

Are we at LOG_LEVELS info?

Returns:

  • (Boolean)

    true if ‘info’ or higher, false if not ‘info’ LOG_LEVELS or lower



126
127
128
# File 'lib/beaker/logger.rb', line 126

def is_info?
  LOG_LEVELS[@log_level] >= LOG_LEVELS[:info]
end

#is_notify?Boolean

Are we at LOG_LEVELS notify?

Returns:

  • (Boolean)

    true if ‘notify’ or higher, false if not ‘notify’ LOG_LEVELS or lower



132
133
134
# File 'lib/beaker/logger.rb', line 132

def is_notify?
  LOG_LEVELS[@log_level] >= LOG_LEVELS[:notify]
end

#is_verbose?Boolean

Are we at LOG_LEVELS verbose?

Returns:

  • (Boolean)

    true if ‘verbose’ or higher, false if not ‘verbose’ LOG_LEVELS or lower



114
115
116
# File 'lib/beaker/logger.rb', line 114

def is_verbose?
  LOG_LEVELS[@log_level] >= LOG_LEVELS[:verbose]
end

#is_warn?Boolean

Are we at LOG_LEVELS warn?

Returns:

  • (Boolean)

    true if ‘warn’ or higher, false if not ‘warn’ LOG_LEVELS or lower



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

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

#notify(*args) ⇒ Object

Report a notify message. Will not print unless we are at LOG_LEVELS ‘notify’ or higher.

Parameters:

  • args (Array<String>)

    Strings to be reported



203
204
205
206
# File 'lib/beaker/logger.rb', line 203

def notify *args
  return unless is_notify?
  optionally_color BRIGHT_WHITE, *args
end

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

Print the provided message to the set destination streams, using color codes if appropriate

Parameters:

  • color_code (String)

    The color code to pre-pend to the message

  • msg (String)

    The message to be reported

  • add_newline (Boolean) (defaults to: true)

    (true) Add newlines between the color codes and the message



228
229
230
231
232
233
234
235
# File 'lib/beaker/logger.rb', line 228

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, convert( msg )
    to.print NORMAL if @color
  end
end

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

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). Beaker associated files will be purged from backtrace unless log level is ‘debug’ or higher

Parameters:

  • backtrace (String) (defaults to: caller(1))

    (caller(1)) The backtrace to format

Returns:

  • (String)

    The formatted backtrace



243
244
245
246
# File 'lib/beaker/logger.rb', line 243

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

#remove_destination(dest) ⇒ Object

Remove a steam from the destinations array based upon it’s name or file path

Parameters:

  • dest (String, IO)

    String representing a file path or IO stream



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/beaker/logger.rb', line 93

def remove_destination(dest)
  case dest
  when IO
    @destinations.delete(dest)
  when StringIO
    @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

#start_sublogObject

Create a new StringIO log to track the current output



249
250
251
252
253
254
255
# File 'lib/beaker/logger.rb', line 249

def start_sublog
  if @sublog
    remove_destination(@sublog)
  end
  @sublog = StringIO.new
  add_destination(@sublog)
end

#strip_colors_from(lines) ⇒ Array<String>

Strip any color codes from provided string(s)

Parameters:

  • lines (String)

    A single or array of lines to removed color codes from

Returns:

  • (Array<String>)

    An array of strings that do not have color codes



218
219
220
221
222
# File 'lib/beaker/logger.rb', line 218

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

#success(*args) ⇒ Object

Report a success message. Will always be reported.

Parameters:

  • args (Array<String>)

    Strings to be reported



196
197
198
# File 'lib/beaker/logger.rb', line 196

def success *args
  optionally_color GREEN, *args
end

#warn(*args) ⇒ Object

Report a warning message. Will not print unless we are at LOG_LEVELS ‘warn’ or higher. Will pre-pend the message with “Warning: ”.

Parameters:

  • args (Array<String>)

    Strings to be reported



179
180
181
182
183
# File 'lib/beaker/logger.rb', line 179

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