Class to display/update/remove status update messages during processing
with the option of sending these messages to the Logger
for
logging. This class also has class methods to print
and
puts
strings which cannot be updated/removed but which will be
logged, by default.
Unlike the Logger
methods, the Status
methods
generate output on $stdout
or $stderr
.
Display the string str and log it, by default. The string can
later be replaced on the display with the update
method. The
string should not have any special characters in it (tabs, newlines
etc.). To disable logging, supply :log => false
parameter for opts.
# File buryspam.rb, line 4159 def initialize(str = "", opts = {}) params = { :log => true }.merge(opts) # Don't try something like: # @str, @log = str, opts[:log] || true # because if you do,then @log will never be set false. @str, @log = str, params[:log] # We need to go up one extra step in the stack frame, # because 'initialize' is called by 'new'. Status.print(@str, :log => @log, :invoker => invoker(2)) end
Display the message string str on the appropriate stream and log it, if requested. The opts hash values are as follows:
:log
: If false
then don't log message. Default:
true
.
:flush
: If true
and msg doesn't end with
newline, then flush the stream. Default: true
.
:severity
: The severity level at which to log msg. if
:severity
is :info
or :debug
then
display msg on $stdout
otherwise, send msg
to $stderr
. Default: :info
.
:invoker
: The calling method name to use when logging the
message. Default: the name of the method that called print
.
# File buryspam.rb, line 4197 def print(msg, opts = {}) params = { :log => true, :flush => true, :severity => :info, :invoker => invoker }.merge(opts) if params[:log] # Don't log backspace, leading/trailing whitespace characters. log_msg = msg.delete("").strip # Don't log empty/blank strings. unless log_msg.strip.empty? Logger.send(params[:severity], params[:invoker]) { log_msg } end end stream = [:debug, :info].include?(params[:severity]) ? $stdout : $stderr stream.print(msg) # Don't flush if already newline terminated if msg[-1] != \n\ && params[:flush] stream.flush end end
Wrapper for the print
method, but append a newline to
msg if one already isn't there. For the opts options, see
the print
method.
# File buryspam.rb, line 4227 def puts(msg = "", opts = {}) if msg[-1] != \n\ msg += "\n" end Status.print(msg, { :invoker => invoker }.merge(opts)) end
Remove the string (created/displayed by new
) from the display.
# File buryspam.rb, line 4181 def finish update("") end
Replace the string displayed earlier with the given string str.
# File buryspam.rb, line 4171 def update(str) return if str == @str Status.print("" * @str.length, :flush => false, :log => false) diff = @str.length - str.length blank = diff > 0 ? " " * diff + "" * diff : "" Status.print(str + blank, :log => @log, :invoker => invoker) @str = str end