Class: UpdateRepo::Logger
- Inherits:
-
Object
- Object
- UpdateRepo::Logger
- Includes:
- Helpers
- Defined in:
- lib/update_repo/logger.rb
Overview
Class : Logger. This class encapsulates printing to screen and logging to file if requried.
Instance Method Summary collapse
-
#close ⇒ void
close the logfile, if it exists.
-
#generate_filename ⇒ string
generate a filename for the log, with or without a timestamp.
-
#initialize(enabled, timestamp, verbose, quiet) ⇒ void
constructor
Constructor for the Logger class.
-
#output(*string) ⇒ void
this function will simply pass the given string to ‘print’, and also log to file if that is specified.
-
#repo_text? ⇒ boolean
returns non nil if we have been called originally by one of the Repo update output functions.
-
#repostat(status) ⇒ void
function repostat - outputs the passed char at the passed color, only if we are not in quiet nor verbose mode.
Methods included from Helpers
Constructor Details
#initialize(enabled, timestamp, verbose, quiet) ⇒ void
Constructor for the Logger class.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/update_repo/logger.rb', line 18 def initialize(enabled, , verbose, quiet) @settings = { enabled: enabled, timestamp: , verbose: verbose, quiet: quiet } # don't prepare a logfile unless it's been requested. return unless @settings[:enabled] # 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 |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
close the logfile, if it exists
93 94 95 |
# File 'lib/update_repo/logger.rb', line 93 def close @logfile.close if @logfile end |
#generate_filename ⇒ string
generate a filename for the log, with or without a timestamp
33 34 35 36 37 38 39 |
# File 'lib/update_repo/logger.rb', line 33 def generate_filename if @settings[:timestamp] 'updaterepo-' + Time.new.strftime('%y%m%d-%H%M%S') + '.log' else 'updaterepo.log' end end |
#output(*string) ⇒ void
This method returns an undefined value.
this function will simply pass the given string to ‘print’, and also log to file if that is specified.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/update_repo/logger.rb', line 45 def output(*string) # nothing to screen if we want to be --quiet unless @settings[:quiet] # log header and footer to screen regardless print(*string) if @settings[:verbose] || !repo_text? end # log to file if that has been enabled return unless @settings[:enabled] @logfile.write(string.join('').gsub(/\e\[(\d+)(;\d+)*m/, '')) end |
#repo_text? ⇒ boolean
returns non nil if we have been called originally by one of the Repo update output functions.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/update_repo/logger.rb', line 78 def repo_text? # get calling function - need to skip first 2, also remove 'block in ' # prefix if exists calling_fn = caller_locations[2].label.gsub(/block in /, '') # array with the functions we want to skip repo_output = %w(do_update handle_output handle_err skip_repo update) # return the name in string if DOES match. calling_fn if repo_output.include?(calling_fn) end |
#repostat(status) ⇒ void
This method returns an undefined value.
function repostat - outputs the passed char at the passed color, only if we are not in quiet nor verbose mode.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/update_repo/logger.rb', line 60 def repostat(status) # only print if not quiet and not verbose! return if @settings[:quiet] || @settings[:verbose] if status[:failed] print 'x'.red elsif status[:updated] print '^'.green elsif status[:unchanged] print '.' elsif status[:skipped] print 's'.yellow end end |