module Scrolls::Log

Public Instance Methods

log(action, attrs = {}) { || ... } click to toggle source
# File lib/scrolls.rb, line 3
def log(action, attrs = {})
  unless block_given?
    str = "#{action} #{unparse(attrs)}"
    mtx.synchronize { $stdout.puts str }
  else
    start = Time.now
    log(action, attrs.merge(at: :start))
    res = yield
    log(action, attrs.merge(at: :finish,
      elapsed: "#{((Time.now - start) * 1000).to_i}ms"))
    res
  end
end

Private Instance Methods

mtx() click to toggle source
# File lib/scrolls.rb, line 19
def mtx
  @mtx ||= Mutex.new
end
unparse(attrs) click to toggle source
# File lib/scrolls.rb, line 23
def unparse(attrs)
  attrs.map { |k, v| unparse_pair(k, v) }.join(" ")
end
unparse_pair(k, v) click to toggle source
# File lib/scrolls.rb, line 27
def unparse_pair(k, v)
  v = v.call if v.is_a?(Proc)
  # only quote strings if they include whitespace
  if v.is_a?(String) && v =~ /\s/
    %{#{k}="#{v}"}
  else
    "#{k}=#{v}"
  end
end