Class: Babushka::Logging

Inherits:
Object show all
Extended by:
LogHelpers
Defined in:
lib/babushka/helpers/log_helpers.rb

Constant Summary

TICK_CHAR =
''
CROSS_CHAR =
''

Class Method Summary (collapse)

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Class Method Details

+ (Object) closing_log_message(message, result = true, opts = {})



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/babushka/helpers/log_helpers.rb', line 153

def self.closing_log_message message, result = true, opts = {}
  message = opts[:closing_status] if opts[:closing_status].is_a?(String)

  if opts[:closing_status] == :status_only
    '}'.colorize('grey') + ' ' + "#{result ? TICK_CHAR : CROSS_CHAR}".colorize(result ? 'green' : 'red')
  elsif opts[:closing_status] == :dry_run
    '}'.colorize('grey') + ' ' + "#{result ? TICK_CHAR : '~'} #{message}".colorize(result ? 'green' : 'blue')
  elsif opts[:closing_status]
    '}'.colorize('grey') + ' ' + "#{result ? TICK_CHAR : CROSS_CHAR} #{message}".colorize(result ? 'green' : 'red')
  else
    "}".colorize('grey')
  end
end

+ (Object) const_missing(const_name)



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/babushka/helpers/log_helpers.rb', line 140

def self.const_missing const_name
  if %w[
    TickChar
    CrossChar
  ].include?(const_name.to_s)
    const_case = const_name.to_s.scan(/[A-Z][a-z]+/).map(&:upcase).join('_')
    deprecated! "2013-12-15", :method_name => "Babushka::Logging::#{const_name}", :instead => "Babushka::Logging::#{const_case}"
    Babushka::Logging.const_get(const_case)
  else
    super
  end
end

+ (Object) log_exception(exception)



167
168
169
170
# File 'lib/babushka/helpers/log_helpers.rb', line 167

def self.log_exception exception
  log_error "#{exception.backtrace.first}: #{exception.message}"
  debug exception.backtrace * "\n"
end

+ (Object) log_table(headings, rows)



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/babushka/helpers/log_helpers.rb', line 172

def self.log_table headings, rows
  all_rows = rows.map {|row|
    row.map(&:to_s)
  }.unshift(
    headings
  ).transpose.map {|col|
    max_length = col.map(&:length).max
    col.map {|cell| cell.ljust(max_length) }
  }.transpose

  [
    all_rows.first.join(' | '),
    all_rows.first.map {|i| '-' * i.length }.join('-+-')
  ].concat(
    all_rows[1..-1].map {|row| row.join(' | ') }
  ).each {|row|
    log row
  }
end