class Motion::Lager
Constants
- COLORS
Public Class Methods
new(config = {})
click to toggle source
# File lib/project/lager.rb, line 26 def initialize(config = {}) @config = {} @config[:level] = config.fetch(:level, :debug).to_sym end
Public Instance Methods
debug(obj, color = :gray, bg_color = :default)
click to toggle source
# File lib/project/lager.rb, line 31 def debug(obj, color = :gray, bg_color = :default) log_with_level(:debug, obj, color, bg_color) end
error(obj, color = :red, bg_color = :default)
click to toggle source
# File lib/project/lager.rb, line 44 def error(obj, color = :red, bg_color = :default) log_with_level(:error, obj, color, bg_color) end
info(obj, color = :default, bg_color = :default)
click to toggle source
# File lib/project/lager.rb, line 35 def info(obj, color = :default, bg_color = :default) log_with_level(:info, obj, color, bg_color) end
Also aliased as: log
warn(obj, color = :yellow, bg_color = :default)
click to toggle source
# File lib/project/lager.rb, line 40 def warn(obj, color = :yellow, bg_color = :default) log_with_level(:warn, obj, color, bg_color) end
Private Instance Methods
colorize(str, fg_color, bg_color = :default)
click to toggle source
# File lib/project/lager.rb, line 73 def colorize(str, fg_color, bg_color = :default) return str unless defined?(MotionRepl) # only colorize when displaying in REPL "\e[#{COLORS[fg_color][:fg]};#{COLORS[bg_color][:bg]}m#{str}\e[0m" end
enabled?(level)
click to toggle source
# File lib/project/lager.rb, line 60 def enabled?(level) case @config[:level] when :debug return true when :info return level != :debug when :warn return level != :debug && level != :info when :error return level == :error end end
inspect_hash(hash)
click to toggle source
# File lib/project/lager.rb, line 88 def inspect_hash(hash) message = "{" hash.each do |k, v| value_lines = [] inspect_object(v).lines.each_with_index do |line, i| line = " #{line}" unless i == 0 value_lines << line end message += "\n #{inspect_object(k)} => #{value_lines.join}," end message += "\n}" end
inspect_nserror(err)
click to toggle source
# File lib/project/lager.rb, line 101 def inspect_nserror(err) return "NSError was actually #{err.inspect}" unless err message = "<#{err.class}" \ "\n Domain: #{err.domain}" \ "\n Code: #{err.code}" \ "\n Description: #{err.localizedDescription}" user_info_lines = [] inspect_object(err.userInfo).lines.each_with_index do |line, i| line = " #{line}" unless i == 0 user_info_lines << line end message += "\n UserInfo: #{user_info_lines.join}" \ "\n RecoveryOptions: #{err.localizedRecoveryOptions.inspect}" \ "\n RecoverySuggestion: #{err.localizedRecoverySuggestion.inspect}" \ "\n FailureReason: #{err.localizedFailureReason.inspect}" \ ">" end
inspect_object(obj)
click to toggle source
# File lib/project/lager.rb, line 78 def inspect_object(obj) case obj when Hash then inspect_hash(obj) when NSError then inspect_nserror(obj) when NSURL then obj.to_s else obj.inspect end end
log_with_level(level, obj, color = :white, bg_color = :default)
click to toggle source
# File lib/project/lager.rb, line 50 def log_with_level(level, obj, color = :white, bg_color = :default) return unless enabled?(level) if obj.is_a? String str = obj else str = inspect_object(obj) end puts str.to_s.split("\n").map {|line| colorize(line, color, bg_color) }.join("\n") end