class Patch::Log

Logging

Public Class Methods

new(out, options = {}) click to toggle source

@param [IO] out @param [Hash] options @option options [Array<Symbol>] :show

# File lib/patch/log.rb, line 9
def initialize(out, options = {})
  @out = out
  @start = Time.now
  populate_level(options)
end

Public Instance Methods

error(exception)
Alias for: exception
exception(exception) click to toggle source

Output an exception @param [String] exception @return [String]

# File lib/patch/log.rb, line 38
def exception(exception)
  if @exception
    message = format(exception.message, :type => :exception)
    @out.puts(message)
  end
  exception
end
Also aliased as: error
info(message)
Alias for: puts
path() click to toggle source
# File lib/patch/log.rb, line 15
def path
  @out.path
end
puts(message) click to toggle source

Output an info message @param [String] message @return [String]

# File lib/patch/log.rb, line 28
def puts(message)
  message = format(message, :type => :info)
  @out.puts(message) if @info
  message
end
Also aliased as: info
time() click to toggle source

The current time since startup @return [Time]

# File lib/patch/log.rb, line 21
def time
  Time.now - @start
end

Private Instance Methods

caller_method(depth=1) click to toggle source

Get the caller method where a message originated @param [Fixnum] depth @return [String]

# File lib/patch/log.rb, line 80
def caller_method(depth=1)
  method = caller(depth+1).first
  parse_caller(method)
end
format(message, options = {}) click to toggle source

Format a message for output @param [String] message @param [Hash] options @option options [Symbol] type @return [String]

# File lib/patch/log.rb, line 68
def format(message, options = {})
  {
    :timestamp => time.seconds.round(2),
    :caller => caller_method,
    :message => message,
    :type => options[:type]
  }.to_json
end
parse_caller(at) click to toggle source

Parse the caller name @param [String] at @return [String]

# File lib/patch/log.rb, line 88
def parse_caller(at)
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
    file   = Regexp.last_match[1]
    file.scan(/.+\/(\w+)\.rb/)[0][0]
  end
end
populate_level(options = {}) click to toggle source

Populate the level setting @param [Hash] options @return [Debug]

# File lib/patch/log.rb, line 52
def populate_level(options = {})
  if !options[:show].nil?
    show = [options[:show]].flatten.compact
    @exception = !(show & [:exception, :error]).empty?
    @info = !(show & [:info, :message]).empty?
  end
  @exception = true if @exception.nil?
  @info = true if @info.nil?
  self
end