class UpdateRepo::Logger
Class : Logger
. This class encapsulates printing to screen and logging to file if requried.
Public Class Methods
Constructor for the Logger
class. @param cmd [instance] - pointer to the CmdConfig
class @return [void] @example
log = Logger.new(@cmd)
# File lib/update_repo/logger.rb, line 17 def initialize(cmd) @cmd = cmd @legend = { failed: { char: 'x', color: 'red' }, updated: { char: '^', color: 'green' }, unchanged: { char: '.', color: 'white' }, skipped: { char: 's', color: 'yellow' } } # don't prepare a logfile unless it's been requested. return unless @cmd[:log] # generate a filename depending on 'timestamp' setting. filename = generate_filename # open the logfile and set sync mode. @logfile = File.open(filename, 'w') @logfile.sync = true end
Public Instance Methods
close the logfile, if it exists @param [none] @return [void]
# File lib/update_repo/logger.rb, line 103 def close @logfile&.close # if @logfile end
generate a filename for the log, with or without a timestamp @param [none] @return [string] Filename for the logfile.
# File lib/update_repo/logger.rb, line 36 def generate_filename # add a timestamp if requested name = if @cmd[:timestamp] "updaterepo-#{Time.new.strftime('%y%m%d-%H%M%S')}.log" else 'updaterepo.log' end # log to local directory instead of home directory if requested if @cmd[:log_local] File.expand_path(File.join('./', name)) else File.expand_path(File.join('~/', name)) end end
Return a string containing the logfile name and full path. @return [string]
# File lib/update_repo/logger.rb, line 82 def logfile @logfile.path end
this function will simply pass the given string to 'print', and also log to file if that is specified. @param string [array] Array of strings for print formatting @return [void]
# File lib/update_repo/logger.rb, line 55 def output(*string) # nothing to screen if we want to be --quiet if !@cmd[:quiet] && (@cmd[:verbose] || !repo_text?) # log header and footer to screen regardless print(*string) end # log to file if that has been enabled return unless @cmd[:log] @logfile.write(string.join.gsub(/\e\[(\d+)(;\d+)*m/, '')) end
returns non nil if we have been called originally by one of the Repo update output functions. @param [none] @return [boolean] True if we have been called during repo update
# File lib/update_repo/logger.rb, line 90 def repo_text? # get calling function - need to skip first 2, also remove 'block in ' # prefix if exists calling_fn = caller_locations(3..3).first.label.gsub(/block in /, '') # array with the functions we want to skip repo_output = %w[do_update print_line handle_output skip_repo update] # return TRUE if DOES match, FALSE otherwise. repo_output.include?(calling_fn) ? true : false end
function repostat - outputs a coloured char depending on the status hash, but not if we are in quiet or verbose mode. @param status [hash] pointer to GitControl.status
hash @return [void]
# File lib/update_repo/logger.rb, line 71 def repostat(status) # only print if not quiet and not verbose! return if @cmd[:quiet] || @cmd[:verbose] @legend.each do |key, value| print value[:char].send(value[:color].to_sym) if status[key] end end