module Doable::Helpers::Logging

Public Instance Methods

colorize(text, color, options = {}) click to toggle source

Applies a color (with optional addition settings) to some text @return [String] @param text [String] The String to be colorized @param color [Symbol] The color to use. Must be one of [ :gray, :red, :green, :yellow, :blue, :magenta, :cyan, :white ]

# File lib/doable/helpers/logging.rb, line 11
def colorize(text, color, options = {})
  background = options[:background] || options[:bg] || false
  style = options[:style].to_sym if options[:style]
  offsets = [ :gray, :red, :green, :yellow, :blue, :magenta, :cyan, :white ]
  styles = [ :normal, :bold, :dark, :italic, :underline, :xx, :xx, :underline, :xx, :strikethrough ]
  start = background ? 40 : 30
  color_code = start + (offsets.index(color) || 8)
  style_code = styles.index(style) || 0
  "\e[#{style_code};#{color_code}m#{text}\e[0m"
end
log(text, level = :info) click to toggle source

All logging or writing to STDOUT should happen here (to be threadsafe)

# File lib/doable/helpers/logging.rb, line 23
def log(text, level = :info)
  level = level.to_sym
    
  color, options = case level
  when :info
    [:white, {}]
  when :warn
    [:yellow, {}]
  when :error
    [:red, {:bg => true}]
  when :success
    [:green, {}]
  else
    [:gray, {}]
  end
    
  LOGGING_MUTEX.synchronize { 
    puts "[#{Time.now.strftime('%Y/%m/%d %H:%M:%S')}] #{colorize(text, color, options)}" 
  }
end