Class: UpdateRepo::Logger

Inherits:
Object
  • Object
show all
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

Methods included from Helpers

#trunc_dir

Constructor Details

#initialize(enabled, timestamp, verbose, quiet) ⇒ void

Constructor for the Logger class.

Examples:

log = Logger.new(true, false)

Parameters:

  • enabled (boolean)

    True if we log to file

  • timestamp (boolean)

    True if we timestamp the filename

  • verbose (boolean)

    True if verbose flag is set

  • quiet (boolean)

    True if quiet flag is set



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/update_repo/logger.rb', line 18

def initialize(enabled, timestamp, verbose, quiet)
  @settings = { enabled: enabled, timestamp: timestamp, verbose: verbose,
                quiet: quiet }
  @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 @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

#closevoid

This method returns an undefined value.

close the logfile, if it exists

Parameters:

  • (none)


90
91
92
# File 'lib/update_repo/logger.rb', line 90

def close
  @logfile.close if @logfile
end

#generate_filenamestring

generate a filename for the log, with or without a timestamp

Parameters:

  • (none)

Returns:

  • (string)

    Filename for the logfile.



37
38
39
40
41
42
43
44
# File 'lib/update_repo/logger.rb', line 37

def generate_filename
  if @settings[:timestamp]
    name = 'updaterepo-' + Time.new.strftime('%y%m%d-%H%M%S') + '.log'
  else
    name = 'updaterepo.log'
  end
  File.expand_path(File.join('~/', name))
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.

Parameters:

  • string (array)

    Array of strings for print formatting



50
51
52
53
54
55
56
57
58
59
# File 'lib/update_repo/logger.rb', line 50

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.

Parameters:

  • (none)

Returns:

  • (boolean)

    True if we have been called during repo update



77
78
79
80
81
82
83
84
85
# File 'lib/update_repo/logger.rb', line 77

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 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 a coloured char depending on the status hash, but not if we are in quiet or verbose mode.

Parameters:

  • status (hash)

    pointer to GitControl.status hash



65
66
67
68
69
70
71
# File 'lib/update_repo/logger.rb', line 65

def repostat(status)
  # only print if not quiet and not verbose!
  return if @settings[:quiet] || @settings[:verbose]
  @legend.each do |key, value|
    print value[:char].send(value[:color].to_sym) if status[key]
  end
end