require "thread"

class Log4u
  @mutex = {}
  def self.mutex(path)
    @mutex[path] ||= Mutex.new
  end

  def initialize(path)
    @mutex = Log4u.mutex(path)

    if path[0,1] == "|" then
      @log = IO.popen(path[1..-1],"w")
    else
      @log = File.open(path, "w+")
    end
  end

  def time_string
    time_now = Time.now
    time_now.strftime("%I:%M%p")
  end

  def error msg
    @mutex { @log.puts "ERROR #{time_string}: #{msg}" }
  end

  def info msg
    @mutex { @log.puts "INFO #{time_string}: #{msg}" }
  end

  def debug msg
    @mutex { @log.puts "DEBUG #{time_string}: #{msg}" }
  end

  def close_log
    @mutex { @log.close }
  end
end