Module: Babushka::LogHelpers

Class Method Summary collapse

Class Method Details

.debug(message, opts = {}, &block) ⇒ Object

Write message to the debug log.

The message will be written to the log in the normal style, but will only appear on STDOUT if debug logging is enabled.



59
60
61
# File 'lib/babushka/helpers/log_helpers.rb', line 59

def debug message, opts = {}, &block
  log message, opts.merge(:debug => !opts[:log]), &block
end

.deprecated!(date, opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/babushka/helpers/log_helpers.rb', line 63

def deprecated! date, opts = {}
  callpoint = "#{caller[opts[:skip] || 1].sub(/\:in `.*$/, '')}: " unless opts[:callpoint] == false
  opts[:method_name] ||= "##{caller[0].scan(/`(\w+)'$/).flatten.first}"
  warning = "#{callpoint}#{opts[:method_name]} has been deprecated and will be removed on #{date}."
  instead = " Use #{opts[:instead]} instead#{opts[:example] ? ", e.g.:" : '.'}" unless opts[:instead].nil?
  log_warn "#{warning}#{instead}"
  log opts[:example].strip unless opts[:example].nil?
  log ''
end

.log(message, opts = {}, &block) ⇒ Object

Write message to the log.

By default, the log is written to STDOUT, and to ~/.babushka/logs/<dep_name>. The log in ~/.babushka/logs is always a full debugging log, but STDOUT only includes debug logging if --debug was supplied on the command line.

By default, the message is ended with a newline. You can pass :newline => false to prevent the newline character being added.

To specify the message type, you can use :as. There are four custom types supported:

:ok      The message is printed in grey with +TICK_CHAR+ prepended, as
         used by +log_ok+.
:warning The message is printed in yellow, as used by +log_warn+.
:error   The message is printed in red, as used by +log_error+.
:stderr  The message (representing STDERR output) is printed in bold,
         as used by +Shell+ for debug logging.

If a block is given, the block is yielded with the indentation level incremented. Opening and closing braces are printed to the log to represent the nesting. (This is the logging style used to show the nesting during dep runs - so please consider other logging styles before using this one, so as not to visually confuse dep runs with other operations.)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/babushka/helpers/log_helpers.rb', line 105

def log message, opts = {}, &block
  printable = !opts[:debug] || Base.task.opt(:debug)
  if Base.task.opt(:profile)
    delta = Time.now - Base.start_time
    Logging.print_log("%.4f ".colorize('grey') % delta, printable, opts[:as])
  end
  Logging.print_log(Logging.indentation, printable, opts[:as]) unless opts[:indentation] == false
  if block_given?
    Logging.print_log("#{message} {".colorize('grey') + "\n", printable, opts[:as])
    Logging.indent! if printable
    yield.tap {|result|
      Logging.undent! if printable
      log Logging.closing_log_message(message, result, opts), opts
    }
  else
    message = message.to_s.to_utf8.rstrip
    message = message.gsub "\n", "\n#{Logging.indentation}"
    message = "#{Logging::TICK_CHAR.colorize('grey')} #{message}" if opts[:as] == :ok
    message = message.colorize 'red' if opts[:as] == :error
    message = message.colorize 'yellow' if opts[:as] == :warning
    message = message.colorize 'bold' if opts[:as] == :stderr
    message = message.end_with "\n" unless opts[:newline] == false
    Logging.print_log(message, printable, opts[:as])
    STDOUT.flush
    nil
  end
end

.log_block(message, opts = {}, &block) ⇒ Object

Yield the block, writing a note to the log about it beforehand and afterwards.

As an example, suppose we called #log_block as follows:

log_block('Sleeping for a bit') { sleep 10 }

While the block yields, the log would show

Sleeping for a bit... (without a newline)

Once the block returns, the log would be completed to show

Sleeping for a bit... done.


37
38
39
40
41
42
# File 'lib/babushka/helpers/log_helpers.rb', line 37

def log_block message, opts = {}, &block
  log "#{message}...", :newline => false
  block.call.tap {|result|
    log result ? " #{opts[:success] || 'done'}." : " #{opts[:failure] || 'failed'}", :as => (result ? nil : :error), :indentation => false
  }
end

.log_error(message, opts = {}, &block) ⇒ Object

Log message as an error. This is a shortcut for

log(message, :as => :error)


16
17
18
# File 'lib/babushka/helpers/log_helpers.rb', line 16

def log_error message, opts = {}, &block
  log message, opts.merge(:as => :error), &block
end

.log_ok(message, opts = {}, &block) ⇒ Object

Write message to the debug log, prefixed with TICK_CHAR, returning true.

This is used to report events that have succeeded, or items that are already working. For example, when the package manager reports that a package is already installed, that's an 'OK' that babushka can move on to the next job, and so log_ok is used to report that fact.



50
51
52
53
# File 'lib/babushka/helpers/log_helpers.rb', line 50

def log_ok message, opts = {}, &block
  log message.end_with('.'), opts.merge(:as => :ok), &block
  true
end

.log_stderr(message, opts = {}, &block) ⇒ Object

Log message to STDERR. This is a shortcut for

log(message, :as => :error)


10
11
12
# File 'lib/babushka/helpers/log_helpers.rb', line 10

def log_stderr message, opts = {}, &block
  log message, opts.merge(:as => :stderr), &block
end

.log_warn(message, opts = {}, &block) ⇒ Object

Log message as a warning. This is a shortcut for

log(message, :as => :warning)


22
23
24
# File 'lib/babushka/helpers/log_helpers.rb', line 22

def log_warn message, opts = {}, &block
  log message, opts.merge(:as => :warning), &block
end

.removed!(opts = {}) ⇒ Object

Raises:

  • (NoMethodError)


73
74
75
76
77
78
79
# File 'lib/babushka/helpers/log_helpers.rb', line 73

def removed! opts = {}
  opts[:method_name] ||= "##{caller[0].scan(/`(\w+)'$/).flatten.first}"
  warning = "#{opts[:method_name]} has been removed after being deprecated."
  instead = " Use #{opts[:instead]} instead#{opts[:example] ? ", e.g.:" : '.'}" unless opts[:instead].nil?
  message = ["#{warning}#{instead}", opts[:example]].compact.join("\n")
  raise NoMethodError.new(message)
end