Sha256: 256eb0fca4bb7aead3e99434e7c8511cc82e10d7a2bd97c9d5e4c556c93443ba

Contents?: true

Size: 855 Bytes

Versions: 3

Compression:

Stored size: 855 Bytes

Contents

module BinData
  # reference to the current tracer
  @tracer ||= nil

  class Tracer #:nodoc:
    def initialize(io)
      @trace_io = io
    end

    def trace(msg)
      @trace_io.puts(msg)
    end

    def trace_obj(obj_name, val)
      if val.length > 30
        val = val.slice(0 .. 30) + "..."
      end

      trace "#{obj_name} => #{val}"
    end
  end

  # Turn on trace information when reading a BinData object.
  # If +block+ is given then the tracing only occurs for that block.
  # This is useful for debugging a BinData declaration.
  def trace_reading(io = STDERR, &block)
    @tracer = Tracer.new(io)
    if block_given?
      begin
        block.call
      ensure
        @tracer = nil
      end
    end
  end

  def trace_message(&block) #:nodoc:
    yield @tracer if @tracer
  end

  module_function :trace_reading, :trace_message
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bindata-1.2.2 lib/bindata/trace.rb
bindata-1.2.1 lib/bindata/trace.rb
bindata-1.2.0 lib/bindata/trace.rb