Class: XCRes::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/xcres/logger.rb

Overview

A Logger utility help class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Initialize a new logger



33
34
35
36
37
38
# File 'lib/xcres/logger.rb', line 33

def initialize
  self.silent = false
  self.verbose = false
  self.colored = true
  self.indentation = ''
end

Instance Attribute Details

#coloredBool Also known as: colored?



24
25
26
# File 'lib/xcres/logger.rb', line 24

def colored
  @colored
end

#indentationString



29
30
31
# File 'lib/xcres/logger.rb', line 29

def indentation
  @indentation
end

#silentBool Also known as: silent?



18
19
20
# File 'lib/xcres/logger.rb', line 18

def silent
  @silent
end

#verboseBool Also known as: verbose?



12
13
14
# File 'lib/xcres/logger.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#fail(message_or_exception, *format_args) ⇒ Object

Print a log message to indicate failure of an operation in red color



135
136
137
138
139
140
141
142
# File 'lib/xcres/logger.rb', line 135

def fail message_or_exception, *format_args
  message, exception = coerce_to_message(message_or_exception)
  inform_colored '✗' + ' ' + message, :red, *format_args

  if verbose? && exception != nil
    log "Backtrace:\n"+exception.backtrace.join("\n"), :red
  end
end

#inform(message, *format_args) ⇒ Object

Prints a formatted message



49
50
51
# File 'lib/xcres/logger.rb', line 49

def inform message, *format_args
  puts indentation + message % format_args unless silent?
end

#inform_colored(message, color, *format_args) ⇒ Object

Prints a formatted message in a given color, and prints its arguments with bold font weight



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xcres/logger.rb', line 66

def inform_colored message, color, *format_args
  if colored?
    parts = message
       .gsub(/%[\ +#]?\d*\.?\d*[a-z]/, "\0"+'\0'+"\0")
       .split("\0")
       .reject(&:empty?)
    message = parts.map do |part|
      if part[0] == '%' && part[1] != '%'
        (part % [format_args.shift]).bold.gsub('%', '%%')
      else
        part
      end
    end.map(&color).join('')
  end
  inform message, *format_args
end

#log(message, *format_args) ⇒ Object

Print a log message of log level verbose



92
93
94
# File 'lib/xcres/logger.rb', line 92

def log message, *format_args
  inform_colored 'Ⓥ' + ' ' + message, :magenta, *format_args if verbose?
end

#success(message, *format_args) ⇒ Object

Print a log message to indicate success of an operation in green color



105
106
107
# File 'lib/xcres/logger.rb', line 105

def success message, *format_args
  inform_colored '✓' + ' ' + message, :green, *format_args
end

#warn(message_or_exception, *format_args) ⇒ Object

Print a warning log message in yellow color



118
119
120
121
# File 'lib/xcres/logger.rb', line 118

def warn message_or_exception, *format_args
  message, _ = coerce_to_message(message_or_exception)
  inform_colored '⚠' + ' ' + message, :yellow, *format_args
end