Class: ActiveFacts::Tracer
Instance Method Summary collapse
- #disable(key) ⇒ Object
- #enable(key) ⇒ Object
- #enabled(key) ⇒ Object
-
#initialize ⇒ Tracer
constructor
A new instance of Tracer.
- #keys ⇒ Object
- #selected(args) ⇒ Object
- #show(*args) ⇒ Object
- #toggle(key) ⇒ Object
- #trace(*args, &block) ⇒ Object
Constructor Details
#initialize ⇒ Tracer
Returns a new instance of Tracer.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/activefacts/tracer.rb', line 14 def initialize @nested = false # Set when a block enables all enclosed tracing @available = {} # First time, initialise the tracing environment @indent = 0 @keys = {} if (e = ENV[@@trace_name ||= "TRACE"]) e.split(/[^_a-zA-Z0-9]/).each{|k| enable(k) } if @keys[:help] at_exit { @stderr.puts "---\nDebugging keys available: #{@available.keys.map{|s| s.to_s}.sort*", "}" } end if @keys[:debug] errors = [] success = false ( [ENV["DEBUG_PREFERENCE"]].compact + [ 'pry', 'debugger', 'ruby-debug' ] ).each do |debugger| begin require debugger puts "Loaded "+debugger success = true break rescue LoadError => e errors << e end end unless success puts "Can't load any debugger, failed on:\n#{errors.inspect}" end ::Debugger.start rescue nil end end end |
Instance Method Details
#disable(key) ⇒ Object
68 69 70 |
# File 'lib/activefacts/tracer.rb', line 68 def disable key !key.to_s.empty? && @keys.delete(key.to_sym) end |
#enable(key) ⇒ Object
64 65 66 |
# File 'lib/activefacts/tracer.rb', line 64 def enable key !key.to_s.empty? && @keys[key.to_sym] = true end |
#enabled(key) ⇒ Object
60 61 62 |
# File 'lib/activefacts/tracer.rb', line 60 def enabled key !key.empty? && @keys[key.to_sym] end |
#keys ⇒ Object
56 57 58 |
# File 'lib/activefacts/tracer.rb', line 56 def keys @available.keys end |
#selected(args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/activefacts/tracer.rb', line 76 def selected(args) # Figure out whether this trace is enabled (itself or by :all), if it nests, and if we should print the key: key = if Symbol === args[0] control = args.shift if (s = control.to_s) =~ /_\Z/ nested = true s.sub(/_\Z/, '').to_sym # Avoid creating new strings willy-nilly else control end else :all end @available[key] ||= key # Remember that this trace was requested, for help enabled = @nested || # This trace is enabled because it's in a nested block @keys[key] || # This trace is enabled in its own right @keys[:all] # This trace is enabled because all are @nested = nested [ (enabled ? 1 : 0), @keys[:all] ? " %-15s"%control : nil ] end |
#show(*args) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/activefacts/tracer.rb', line 102 def show(*args) enabled, key_to_show = selected(args) # Emit the message if enabled or a parent is: if args.size > 0 && enabled == 1 puts "\##{key_to_show} " + ' '*@indent + args. # A laudable aim, certainly, but in practise the Procs leak and slow things down: # map{|a| a.respond_to?(:call) ? a.call : a}. join(' ') end @indent += enabled enabled end |
#toggle(key) ⇒ Object
72 73 74 |
# File 'lib/activefacts/tracer.rb', line 72 def toggle key !key.to_s.empty? and enabled(key) ? (disable(key); false) : (enable(key); true) end |
#trace(*args, &block) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/activefacts/tracer.rb', line 118 def trace(*args, &block) begin old_indent, old_nested, enabled = @indent, @nested, show(*args) return (block || proc { enabled == 1 }).call ensure @indent, @nested = old_indent, old_nested end end |