Module: Babushka::LogHelpers

Included in:
BugReporter, Cmdline, Cmdline::Helpers, Cmdline::Parser, Dep, Dep, DepDefiner, DepDefiner, Logging, MetaDep, PkgHelper, Prompt, Resource, Resource, RunHelpers, RunReporter, Shell, ShellHelpers, Source, Source, SourcePool, Task, Vars
Defined in:
lib/babushka/helpers/log_helpers.rb

Instance Method Summary (collapse)

Instance Method Details

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

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.



55
56
57
# File 'lib/babushka/helpers/log_helpers.rb', line 55

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

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

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 three custom types supported:

:ok      The message is printed in grey with +TickChar+ 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 blue,
         as used within the +Shell+ class 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.)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/babushka/helpers/log_helpers.rb', line 83

def log message, opts = {}, &block
  # now = Time.now
  # print "#{now.to_i}.#{now.usec}: ".ljust(20) unless opts[:debug]
  printable = !opts[:debug] || Base.task.opt(:debug)
  Logging.print_log Logging.indentation, printable unless opts[:indentation] == false
  if block_given?
    Logging.print_log "#{message} {\n".colorize('grey'), printable
    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.rstrip.gsub "\n", "\n#{Logging.indentation}"
    message = "#{Logging::TickChar.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
    $stdout.flush
    nil
  end
end

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

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.


33
34
35
36
37
38
# File 'lib/babushka/helpers/log_helpers.rb', line 33

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

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

Log message as an error. This is a shortcut for

log(message, :as => :error)


7
8
9
# File 'lib/babushka/helpers/log_helpers.rb', line 7

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

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

Write message to the debug log, prefixed with TickChar, 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.



46
47
48
49
# File 'lib/babushka/helpers/log_helpers.rb', line 46

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

- (Object) log_verbose(message, opts = {}, &block)



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

def log_verbose message, opts = {}, &block
  log_error "#{caller.first}: #log_verbose has been deprecated. Instead, just use #log." # deprecated
  log message, opts, &block
end

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

Log message as a warning. This is a shortcut for

log(message, :as => :warning)


13
14
15
# File 'lib/babushka/helpers/log_helpers.rb', line 13

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