Class: Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/tools/tracing.rb

Defined Under Namespace

Classes: Where

Constant Summary collapse

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
}
@@doOutput =
true
@@doOutputSelf =
false
@@output =
""
@@stackTraces =
{}
@@outputThread =
nil

Class Method Summary collapse

Class Method Details

.disableObject



75
76
77
78
79
80
# File 'lib/appswarm/tools/tracing.rb', line 75

def self.disable
  if @@outputThread
    set_trace_func nil
    @@outputThread.kill!
  end
end

.doOutput=(flag) ⇒ Object



38
39
40
# File 'lib/appswarm/tools/tracing.rb', line 38

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

.enableObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/appswarm/tools/tracing.rb', line 59

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

.out(*x) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/appswarm/tools/tracing.rb', line 30

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

.outputObject



42
43
44
# File 'lib/appswarm/tools/tracing.rb', line 42

def self.output
  @@output
end

.outTrace(th, trace) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/appswarm/tools/tracing.rb', line 46

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