Class: Sunshine::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/sunshine/output.rb

Overview

The Output class handles all of the logging to the shell during the Sunshine runtime.

Constant Summary collapse

COLORS =
{
  Logger::UNKNOWN => :red,
  Logger::FATAL   => :red,
  Logger::ERROR   => :red,
  Logger::WARN    => :yellow,
  Logger::INFO    => :default,
  Logger::DEBUG   => :cyan,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Output

Returns a new instance of Output.



20
21
22
23
24
25
# File 'lib/sunshine/output.rb', line 20

def initialize(options={})
  @logger = Logger.new options[:output] || $stdout
  @logger.formatter = lambda{|sev, time, progname, msg| msg}
  @level = @logger.level = options[:level] || Logger::DEBUG
  @indent = 0
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



18
19
20
# File 'lib/sunshine/output.rb', line 18

def indent
  @indent
end

#levelObject

Returns the value of attribute level.



18
19
20
# File 'lib/sunshine/output.rb', line 18

def level
  @level
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/sunshine/output.rb', line 18

def logger
  @logger
end

Instance Method Details

#debug(title, message, options = {}, &block) ⇒ Object

Log a message of log level debug.



127
128
129
# File 'lib/sunshine/output.rb', line 127

def debug(title, message, options={}, &block)
  self.log(title, message, options.merge(:level => Logger::DEBUG), &block)
end

#error(title, message, options = {}, &block) ⇒ Object

Log a message of log level error.



109
110
111
# File 'lib/sunshine/output.rb', line 109

def error(title, message, options={}, &block)
  self.log(title, message, options.merge(:level => Logger::ERROR), &block)
end

#fatal(title, message, options = {}, &block) ⇒ Object

Log a message of log level fatal.



103
104
105
# File 'lib/sunshine/output.rb', line 103

def fatal(title, message, options={}, &block)
  self.log(title, message, options.merge(:level => Logger::FATAL), &block)
end

#info(title, message, options = {}, &block) ⇒ Object

Log a message of log level info.



121
122
123
# File 'lib/sunshine/output.rb', line 121

def info(title, message, options={}, &block)
  self.log(title, message, options.merge(:level => Logger::INFO), &block)
end

#log(title, message, options = {}, &block) ⇒ Object

Generic log message which handles log indentation (for clarity). Log indentation if achieved done by passing a block:

output.log("MAIN", "Main thing is happening") do
  ...
  output.log("SUB1", "Sub process thing") do
    ...
    output.log("SUB2", "Innermost process thing")
  end
end

output.log("MAIN", "Start something else")

------
> [MAIN] Main thing is happening
>   [SUB1] Sub process thing
>     [SUB2] Innermost process thing
>
> [MAIN] Start something else

Log level is set to the instance’s default unless specified in the options argument with :level => Logger::LEVEL. The default log level is Logger::INFO

Best practice for using log levels is to call the level methods which all work similarly to the log method: unknown, fatal, error, warn, info, debug



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sunshine/output.rb', line 76

def log(title, message, options={}, &block)
  unless Sunshine.trace?
    return block.call if block_given?
    return
  end

  options = {:indent => @indent}.merge(options)
  self.print(title, message, options)
  if block_given?
    @indent = @indent + 1
    begin
      block.call
    ensure
      @indent = @indent - 1 unless @indent <= 0
      @logger << "\n"
    end
  end
end

Prints messages according to the standard output format. Options supported:

:level:    level of the log message
:indent:  indentation of the message


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sunshine/output.rb', line 32

def print(title, message, options={})
  severity = options[:level] || Logger::DEBUG
  color = COLORS[severity]

  options[:indent] = 0 if options[:indent] < 0
  indent = " " * (options[:indent].to_i * 2)

  print_string = message.split("\n")
  print_string.map!{|m| "#{indent}[#{title}] #{m.chomp}"}
  print_string = "#{print_string.join("\n")} \n"
  print_string = print_string.foreground(color)
  print_string = print_string.bright if indent.empty?

  @logger.add(severity, print_string)
end

#unknown(title, message, options = {}, &block) ⇒ Object

Log a message of log level unknown.



97
98
99
# File 'lib/sunshine/output.rb', line 97

def unknown(title, message, options={}, &block)
  self.log(title, message, options.merge(:level => Logger::UNKNOWN), &block)
end

#warn(title, message, options = {}, &block) ⇒ Object

Log a message of log level warn.



115
116
117
# File 'lib/sunshine/output.rb', line 115

def warn(title, message, options={}, &block)
  self.log(title, message, options.merge(:level => Logger::WARN), &block)
end