Module: Peekaboo

Defined in:
lib/peekaboo.rb,
lib/peekaboo/version.rb,
lib/peekaboo/configuration.rb

Overview

The workhorse of this “Unobtrusive Tracing System”.

Defined Under Namespace

Modules: SingletonMethods, Version Classes: Configuration

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Returns the current configuration.

Returns:



7
8
9
# File 'lib/peekaboo.rb', line 7

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Convenience method added to assist in configuring the system ( see Configuration for details on all options ).

Examples:

Configuring the system inside your project

Peekaboo.configure do |config|
  config.trace_with MyCustomerLogger.new
  config.autoinclude_with SomeBaseClass, AnotherSoloClass
end

Yields:



19
20
21
# File 'lib/peekaboo.rb', line 19

def configure
  yield configuration
end

.included(klass) ⇒ Object

Callback used to hook tracing system into any class.

Parameters:

  • klass (Class)

    including class



26
27
28
29
30
31
32
33
34
# File 'lib/peekaboo.rb', line 26

def included klass
  klass.const_set :PEEKABOO_METHOD_LIST, []
  klass.instance_variable_set :@_hooked_by_peekaboo, true
  klass.extend SingletonMethods
  
  def klass.method_added name
    Peekaboo.wrap_method self, name if peek_list.include? name
  end
end

.setup_autoinclusion(klass) ⇒ Object

Modifies a class, and its child classes, to dynamically include module at runtime. This method is used by Peekaboo::Configuration#autoinclude_with.

Parameters:

  • klass (Class)

    class to modify



40
41
42
43
44
45
46
47
48
49
# File 'lib/peekaboo.rb', line 40

def setup_autoinclusion klass
  def klass.method_missing(method_name, *args, &block)
    if method_name.to_s =~ /^enable_tracing_on$/
      instance_eval { include Peekaboo }
      enable_tracing_on *args
    else
      super
    end
  end
end

.wrap_method(klass, name) ⇒ Object

Note:

Should I add execution time to tracing? Configurable?

Takes a class object and method name, aliases the original method, and redefines the method with injected tracing.

Parameters:

  • klass (Class)

    method owner

  • name (Symbol)

    method to trace



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/peekaboo.rb', line 58

def wrap_method klass, name
  return if @_adding_a_method
  @_adding_a_method = true
  
  original_method = "original_#{name}"
  method_wrapping = %{
    alias_method :#{original_method}, :#{name}
    def #{name} *args, &block
      trace = "\#{caller(1)[0]}\n\t( Invoking: #{klass}\##{name} with \#{args.inspect} "
      begin
        result = #{original_method} *args, &block
        trace << "==> Returning: \#{result.inspect} )"
        result
      rescue Exception => exe
        trace << "!!! Raising: \#{exe.message.inspect} )"
        raise exe
      ensure
        Peekaboo.configuration.tracer.info trace
      end
    end
  }
  klass.class_eval method_wrapping
  
  @_adding_a_method = false
end