Sha256: 599fee57e93ec18d3bfb9f1195e136ba3661a8d197fa3f3ff9ffc492c5cf9ca1

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

class Tracer
  @@doOutput=true
  @@doOutputSelf=false
  @@output=""
  @@stackTraces={}
  @@outputThread=nil

  Where=Struct.new(:event,:file,:line,:id,:binding,:klass)

  TRACE_FUNC = proc{|event, file, line, id, binding, klass|
    th=Thread.current
    @@stackTraces[th]||=[]
    where=Where.new(event,file,line,id,binding,klass)
    case event
      when "call"
        @@stackTraces[th]<<where
      when "return"
        while @@stackTraces[th][-1] and @@stackTraces[th].pop.event=="line"
        end
      when "line"
        while @@stackTraces[th][-1] and @@stackTraces[th][-1].event=="line"
          @@stackTraces[th].pop
        end
        @@stackTraces[th]<<where
    end
  }



  def self.out(*x)
    if @@doOutput
      print ("\n"+x.map{|y|y.to_s}.join("\n")).gsub("\n","\n   :    ")
    else
      @@output+="\n#{x}".gsub("\n","\n   :    ")
    end
  end

  def self.doOutput=(flag)
    @@doOutput=flag
  end

  def self.output
    @@output
  end

  def self.outTrace(th,trace)
    out th.inspect
    th.keys.each{|key|
      out " #{key}=>#{th[key]}"
    }
    trace.each{|line|
      out "   #{line.file}:#{line.line}"
    }
    if th.stacktrace?
      th.stacktrace.each{|x|out "          "+x.to_s}
    end
  end

  def self.enable
    unless @@outputThread
      set_trace_func TRACE_FUNC
      @@outputThread=Thread.new {
        loop do
          @@stackTraces.each{|th,trace|
            if @@doOutputSelf or th!=Thread.current
              outTrace(th,trace) if th.alive?
            end
          }
          out "========="
          sleep 0.5
        end
      }
    end
  end
  def self.disable
    if @@outputThread
      set_trace_func nil
      @@outputThread.kill!
    end
  end

end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appswarm-0.0.1 lib/appswarm/tools/tracing.rb