Class: ActiveFacts::Tracer
Instance Method Summary collapse
- #available_keys ⇒ Object
- #disable(key) ⇒ Object
- #enable(key) ⇒ Object
- #enabled?(key) ⇒ Boolean
-
#initialize ⇒ Tracer
constructor
A new instance of Tracer.
- #setup_debugger ⇒ Object
- #setup_firstaid ⇒ Object
- #setup_flame ⇒ Object
- #setup_help ⇒ Object
- #toggle(key) ⇒ Object
- #trace(*args, &block) ⇒ Object
Constructor Details
#initialize ⇒ Tracer
Returns a new instance of Tracer.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/activefacts/tracer.rb', line 52 def initialize @indent = 0 # Current nesting level of enabled trace blocks @nested = false # Set when a block enables all enclosed tracing @available = {} # Hash of available trace keys, accumulated during the run @delayed = nil # A delayed message, emitted only if the enclosed block emits tracing @keys = {} if (e = ENV["TRACE"]) e.split(/[^_a-zA-Z0-9]/).each{|k| enable(k) } end end |
Instance Method Details
#available_keys ⇒ Object
74 75 76 |
# File 'lib/activefacts/tracer.rb', line 74 def available_keys @available.keys end |
#disable(key) ⇒ Object
92 93 94 |
# File 'lib/activefacts/tracer.rb', line 92 def disable key !key.empty? and @keys.delete(key.to_sym) end |
#enable(key) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/activefacts/tracer.rb', line 82 def enable key if !key.empty? && !@keys[s = key.to_sym] @keys[s] = true setup_help if s == :help setup_flame if s == :flame else true end end |
#enabled?(key) ⇒ Boolean
78 79 80 |
# File 'lib/activefacts/tracer.rb', line 78 def enabled? key !key.empty? && @keys[key.to_sym] end |
#setup_debugger ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/activefacts/tracer.rb', line 131 def setup_debugger begin require 'ruby-trace ' Debugger.start # (:post_mortem => true) # Some Ruby versions crash on post-mortem debugging rescue LoadError # Ok, no debugger, tough luck. end ( [ENV["DEBUG_PREFERENCE"]].compact + [ 'byebug', 'pry', 'debugger', 'ruby-trace ' ] ).each do |debugger| begin require debugger if debugger == 'byebug' Kernel.class_eval do alias_method :byebug, :debugger end end ::Debugger.start if (const_get(::Debugger) rescue nil) break rescue LoadError => e errors << e end end if trace :trap trap('SIGINT') do puts "Stopped at:\n\t"+caller*"\n\t" debugger true # Stopped on SIGINT end end end |
#setup_firstaid ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/activefacts/tracer.rb', line 171 def setup_firstaid if trace :firstaid puts "Preparing first aid kit" ::Exception.class_eval do alias_method :firstaid_initialize, :initialize def initialize *args, &b send(:firstaid_initialize, *args, &b) return if NoMethodError === self && =~ /^undefined method `to_ary' for \#<Gem::Specification/ return if LoadError == self puts "Stopped due to #{self.class}: #{} at "+caller*"\n\t" debugger true # Stopped in Exception constructor end end end end |
#setup_flame ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/activefacts/tracer.rb', line 114 def setup_flame require 'ruby-prof' require 'ruby-prof-flamegraph' profile_result = RubyProf.start at_exit { profile_result2 = RubyProf.stop printer = RubyProf::FlameGraphPrinter.new(profile_result2) data_file = "/tmp/flamedata_#{Process.pid}.txt" svg_file = "/tmp/flamedata_#{Process.pid}.svg" flamegraph = File.dirname(__FILE__)+"/flamegraph.pl" File.popen("tee #{data_file} | perl #{flamegraph} --countname=ms --width=4800 > #{svg_file}", "w") { |f| printer.print(f, {}) } STDERR.puts("Flame graph dumped to file:///#{svg_file}") } end |
#setup_help ⇒ Object
108 109 110 111 112 |
# File 'lib/activefacts/tracer.rb', line 108 def setup_help at_exit { $stderr.puts "---\nTracing keys available: #{@available.keys.map{|s| s.to_s}.sort*", "}" } end |
#toggle(key) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/activefacts/tracer.rb', line 96 def toggle key if !key.empty? if enabled?(key) disable(key) false else enable(key) true end end end |
#trace(*args, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/activefacts/tracer.rb', line 64 def trace(*args, &block) begin old_indent, old_nested, old_delayed, enabled = @indent, @nested, @delayed, show(*args) # This monstrosity reduces the steps when single-stepping: block ? yield : (args.size == 0 ? self : enabled) ensure @indent, @nested, @delayed = old_indent, old_nested, old_delayed end end |