Sha256: a5016c8b86ac7c707baa45d77981bd980c51569a6579a9346baef1e27307fe10

Contents?: true

Size: 1.88 KB

Versions: 10

Compression:

Stored size: 1.88 KB

Contents

require 'digest/md5'
module Log
  FP_MAX_STRING = 150
  FP_MAX_ARRAY = 20
  FP_MAX_HASH = 10
  def self.fingerprint(obj)
    return obj.fingerprint if obj.respond_to?(:fingerprint)

    case obj
    when nil
      "nil"
    when TrueClass
      "true"
    when FalseClass
      "false"
    when Symbol
      ":" << obj.to_s
    when String
      if obj.length > FP_MAX_STRING
        digest = Digest::MD5.hexdigest(obj)
        middle = "<...#{obj.length} - #{digest[0..4]}...>"
        s = (FP_MAX_STRING - middle.length) / 2
        "'" << obj.slice(0,s-1) << middle << obj.slice(-s, obj.length )<< "'"
      else 
        "'" << obj << "'"
      end
    when ConcurrentStream
      name = obj.inspect + " " + obj.object_id.to_s
      name += " #{obj.filename}" if obj.filename
      name
    when IO
      (obj.respond_to?(:filename) and obj.filename ) ? "<IO:" + (obj.filename || obj.inspect + rand(100000)) + ">" : obj.inspect + " " + obj.object_id.to_s
    when File
      "<File:" + obj.path + ">"
    when Array
      if (length = obj.length) > FP_MAX_ARRAY
        "[#{length}--" <<  (obj.values_at(0,1, length / 2, -2, -1).collect{|e| fingerprint(e)} * ",") << "]"
      else
        "[" << (obj.collect{|e| fingerprint(e) } * ", ") << "]"
      end
    when Hash
      if obj.length > FP_MAX_HASH
        "H:{"<< fingerprint(obj.keys) << ";" << fingerprint(obj.values) << "}"
      else
        new = "{"
        obj.each do |k,v|
          new << fingerprint(k) << '=>' << fingerprint(v) << ' '
        end
        if new.length > 1
           new[-1] =  "}"
        else
          new << '}'
        end
        new
      end
    when Float
      if obj.abs > 10
        "%.1f" % obj
      elsif obj.abs > 1
        "%.3f" % obj
      else
        "%.6f" % obj
      end
    when Thread
      if obj["name"]
        obj["name"]
      else
        obj.inspect
      end
    else
      obj.to_s
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
scout-essentials-1.6.5 lib/scout/log/fingerprint.rb
scout-essentials-1.6.4 lib/scout/log/fingerprint.rb
scout-essentials-1.6.3 lib/scout/log/fingerprint.rb
scout-essentials-1.6.2 lib/scout/log/fingerprint.rb
scout-essentials-1.6.1 lib/scout/log/fingerprint.rb
scout-essentials-1.6.0 lib/scout/log/fingerprint.rb
scout-essentials-1.3.1 lib/scout/log/fingerprint.rb
scout-essentials-1.3.0 lib/scout/log/fingerprint.rb
scout-essentials-1.2.0 lib/scout/log/fingerprint.rb
scout-essentials-1.1.1 lib/scout/log/fingerprint.rb