Class: Babushka::Logging

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

Constant Summary collapse

TickChar =
''
CrossChar =
''

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

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



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

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 ? TickChar : CrossChar}".colorize(result ? 'green' : 'red')
  elsif opts[:closing_status] == :dry_run
    '}'.colorize('grey') + ' ' + "#{result ? TickChar : '~'} #{message}".colorize(result ? 'green' : 'blue')
  elsif opts[:closing_status]
    '}'.colorize('grey') + ' ' + "#{result ? TickChar : CrossChar} #{message}".colorize(result ? 'green' : 'red')
  else
    "}".colorize('grey')
  end
end

.log_exception(exception) ⇒ Object



153
154
155
156
# File 'lib/babushka/helpers/log_helpers.rb', line 153

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

.log_table(headings, rows) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/babushka/helpers/log_helpers.rb', line 158

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