Sha256: e6b305d1c9a31d19201a546f7c16ce344c05c5005a04e123801a18a6c264749f

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

module Dlog
  def dlog(*args)
  end
end

if !ENV["NO_DLOG"]

module Dlog
  ROOT = if defined?(RAILS_ROOT)
    RAILS_ROOT
  else
    File.expand_path(Dir.getwd) 
  end
  
  HOME = ENV["HOME"] + "/"
  
  def self.release!
    @release = true
  end

  def self.release?
    @release
  end
      
  def self.debug!
    @release = false
  end

  def self.debug?
    !@release
  end

  def self.quiet!
    @quiet = true
  end

  def self.quiet?
    @quiet
  end

  def dlog(*args)
    return if Dlog.quiet?
    
    msg = ""
    was_string = true
    args.map do |s|
      msg += was_string ? " " : ", " unless msg.empty?
      msg += ((was_string = s.is_a?(String)) ? s : s.inspect)
    end
    STDERR.puts "#{Dlog.release? ? rlog_caller : dlog_caller} #{msg}"
  end

  def rlog_caller
    if caller[1] =~ /^(.*):(\d+)/
      file, line = $1, $2
      "[" + File.basename(file).sub(/\.[^\.]*$/, "") + "]"
    else
      "[log]"
    end
  end

  def dlog_caller
    if caller[1] =~ /^(.*):(\d+)/
      file, line = $1, $2
      file = File.expand_path(file)

      file.gsub!(ROOT, ".") or
      file.gsub!(HOME, "~/")
    
      "#{file}(#{line}):"
    else
      "<dlog>:"
    end
  end
end

end

class Object
  include Dlog
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
etest-0.4.2 lib/dlog_ext.rb
etest-0.4.1 lib/dlog_ext.rb
etest-0.4 lib/dlog_ext.rb
etest-0.3.1 lib/dlog_ext.rb