lib/activefacts/support.rb in activefacts-0.8.16 vs lib/activefacts/support.rb in activefacts-0.8.18
- old
+ new
@@ -4,25 +4,27 @@
# Set the DEBUG environment variable to enable it. Search the code to find the DEBUG keywords, or use "all".
#
# Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
#
#module ActiveFacts
- $debug_indent = nil
+ $debug_indent = 0
$debug_nested = false # Set when a block enables all enclosed debugging
$debug_keys = nil
$debug_available = {}
def debug_initialize
# First time, initialise the tracing environment
$debug_indent = 0
- $debug_keys = {}
- if (e = ENV["DEBUG"])
- e.split(/[^_a-zA-Z0-9]/).each{|k| debug_enable(k) }
- if $debug_keys[:help]
- at_exit {
- $stderr.puts "---\nDebugging keys available: #{$debug_available.keys.map{|s| s.to_s}.sort*", "}"
- }
+ unless $debug_keys
+ $debug_keys = {}
+ if (e = ENV["DEBUG"])
+ e.split(/[^_a-zA-Z0-9]/).each{|k| debug_enable(k) }
+ if $debug_keys[:help]
+ at_exit {
+ $stderr.puts "---\nDebugging keys available: #{$debug_available.keys.map{|s| s.to_s}.sort*", "}"
+ }
+ end
end
end
end
def debug_keys
@@ -70,11 +72,13 @@
$debug_keys[:all] ? " %-15s"%control : nil
]
end
def debug_show(*args)
- debug_initialize unless $debug_indent
+ unless $debug_keys
+ debug_initialize
+ end
enabled, key_to_show = debug_selected(args)
# Emit the message if enabled or a parent is:
if args.size > 0 && enabled == 1
@@ -150,11 +154,11 @@
self
end
end
# Load the ruby debugger before everything else, if requested
-if debug :debug
+if debug :debug or debug :firstaid
begin
require 'ruby-debug'
Debugger.start # (:post_mortem => true) # Some Ruby versions crash on post-mortem debugging
rescue LoadError
# Ok, no debugger, tough luck.
@@ -163,8 +167,22 @@
if debug :trap
trap('SIGINT') do
puts "Stopped at:\n\t"+caller*"\n\t"
debugger
true
+ end
+ end
+
+ if debug :firstaid
+ puts "Preparing first aid kit"
+ class ::Exception
+ alias_method :firstaid_initialize, :initialize
+
+ def initialize *args, &b
+ send(:firstaid_initialize, *args, &b)
+ puts "Stopped due to #{self.class}: #{message} at "+caller*"\n\t"
+ debugger
+ true # Exception thrown
+ end
end
end
end