Module: Babushka::LogHelpers
- Included in:
- BugReporter, Cmdline, Cmdline::Helpers, Cmdline::Parser, Dep, Dep, DepDefiner, DepDefiner, DepTemplate, Logging, PkgHelper, Prompt, Resource, RunHelpers, RunReporter, Shell, ShellHelpers, Source, Source, SourcePool, Task, Vars
- Defined in:
- lib/babushka/helpers/log_helpers.rb
Class Method Summary collapse
-
.debug(message, opts = {}, &block) ⇒ Object
Write
message
to the debug log. - .deprecated!(date, opts = {}) ⇒ Object
-
.log(message, opts = {}, &block) ⇒ Object
Write
message
to the log. -
.log_block(message, opts = {}, &block) ⇒ Object
Yield the block, writing a note to the log about it beforehand and afterwards.
-
.log_error(message, opts = {}, &block) ⇒ Object
Log
message
as an error. -
.log_ok(message, opts = {}, &block) ⇒ Object
Write
message
to the debug log, prefixed withTickChar
, returningtrue
. -
.log_stderr(message, opts = {}, &block) ⇒ Object
Log
message
to STDERR. -
.log_warn(message, opts = {}, &block) ⇒ Object
Log
message
as a warning. - .removed!(opts = {}) ⇒ Object
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 , opts = {}, &block log , 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 +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 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 |
# File 'lib/babushka/helpers/log_helpers.rb', line 105 def log , 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("#{} {".colorize('grey') + "\n", printable, opts[:as]) Logging.indent! if printable yield.tap {|result| Logging.undent! if printable log Logging.(, result, opts), opts } else = .to_s.rstrip.gsub "\n", "\n#{Logging.indentation}" = "#{Logging::TickChar.colorize('grey')} #{}" if opts[:as] == :ok = .colorize 'red' if opts[:as] == :error = .colorize 'yellow' if opts[:as] == :warning = .colorize 'bold' if opts[:as] == :stderr = .end_with "\n" unless opts[:newline] == false Logging.print_log(, 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 , opts = {}, &block log "#{}...", :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(, :as => :error)
16 17 18 |
# File 'lib/babushka/helpers/log_helpers.rb', line 16 def log_error , opts = {}, &block log , opts.merge(:as => :error), &block end |
.log_ok(message, opts = {}, &block) ⇒ Object
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.
50 51 52 53 |
# File 'lib/babushka/helpers/log_helpers.rb', line 50 def log_ok , opts = {}, &block log .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(, :as => :error)
10 11 12 |
# File 'lib/babushka/helpers/log_helpers.rb', line 10 def log_stderr , opts = {}, &block log , opts.merge(:as => :stderr), &block end |
.log_warn(message, opts = {}, &block) ⇒ Object
Log message
as a warning. This is a shortcut for
log(, :as => :warning)
22 23 24 |
# File 'lib/babushka/helpers/log_helpers.rb', line 22 def log_warn , opts = {}, &block log , opts.merge(:as => :warning), &block end |
.removed!(opts = {}) ⇒ Object
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? = ["#{warning}#{instead}", opts[:example]].compact.join("\n") raise NoMethodError.new() end |