peekaboo 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/README.md +100 -0
- data/lib/peekaboo/configuration.rb +21 -0
- data/lib/peekaboo/version.rb +9 -0
- data/lib/peekaboo.rb +68 -0
- metadata +118 -0
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Peekaboo: Unobtrusive method tracing for Ruby classes
|
2
|
+
|
3
|
+
Do you find yourself constantly adding log statements to a lot of the methods in your project?
|
4
|
+
Does it lead to a lot of duplication and make your code feel less "elegant"?
|
5
|
+
Peekaboo offers an alternative approach to tracing method calls, their provided arguments, and their return values.
|
6
|
+
Simply specify which methods you want to trace inside a class and let peekaboo take care of the rest.
|
7
|
+
|
8
|
+
## Installation and usage
|
9
|
+
|
10
|
+
Gem:
|
11
|
+
|
12
|
+
$ gem install peekaboo
|
13
|
+
|
14
|
+
### How it works:
|
15
|
+
|
16
|
+
Peekaboo uses method wrapping and an internal tracer to capture the data you want to know about your methods.
|
17
|
+
Its tracer adheres to the API established by the Logger class ( i.e. debug, info, warn, etc... ).
|
18
|
+
For now, the only trace level supported is "info", but there are plans to support all trace levels in the future.
|
19
|
+
Also, this first cut only provides tracing for _instance_ methods.
|
20
|
+
|
21
|
+
When creating a new class, include Peekaboo and then call `enable_tracing_on`, passing it a list of method names to trace.
|
22
|
+
|
23
|
+
class Example
|
24
|
+
include Peekaboo
|
25
|
+
enable_tracing_on :foo, :bar
|
26
|
+
|
27
|
+
def foo
|
28
|
+
...
|
29
|
+
end
|
30
|
+
|
31
|
+
def bar
|
32
|
+
...
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Sometimes you may want to trace methods in a class that has already been created.
|
37
|
+
In that case, simply reopen the class definition and follow the same steps listed above.
|
38
|
+
|
39
|
+
# Example class already exists with instance methods #baz and #bif defined
|
40
|
+
|
41
|
+
class Example
|
42
|
+
include Peekaboo
|
43
|
+
enable_tracing_on :baz, :bif
|
44
|
+
end
|
45
|
+
|
46
|
+
Now, with tracing enabled, Peekaboo will report when/where those methods are called along with their input and output values.
|
47
|
+
|
48
|
+
# @obj is an instance of Example
|
49
|
+
# Peekaboo tracer receives the following message when #baz is called below:
|
50
|
+
# "File:Line ( Example#baz called with [:one, 2, "three"] ==> Returning: 'whatever gets returned' )"
|
51
|
+
|
52
|
+
@obj.baz :one, 2, "three"
|
53
|
+
|
54
|
+
## Configuration
|
55
|
+
|
56
|
+
The default tracer for Peekaboo is an instance of `Logger` streaming to `STDOUT`.
|
57
|
+
If this doesn't suit your needs, it is a trivial task to set the tracer to another object using the Peekaboo configuration.
|
58
|
+
|
59
|
+
Peekaboo.configure do |config|
|
60
|
+
# file-based logging
|
61
|
+
config.trace_with Logger.new("some_file")
|
62
|
+
|
63
|
+
# inside Rails
|
64
|
+
config.trace_with Rails.logger
|
65
|
+
|
66
|
+
# any object that responds to debug, info, warn, error, fatal, unknown
|
67
|
+
config.trace_with @custom_logger_object
|
68
|
+
end
|
69
|
+
|
70
|
+
## Issues
|
71
|
+
|
72
|
+
Please report any bugs or issues to the [Issue Tracking System](http://github.com/sgarcia/peekaboo/issues/).
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
If you have any feature requests or ideas feel free to contact me directly.
|
77
|
+
If you're looking to contribute please read the contribution guidelines before submitting patches or pull requests.
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
Copyright (c) 2009 Sonny Ruben Garcia
|
82
|
+
|
83
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
84
|
+
a copy of this software and associated documentation files (the
|
85
|
+
"Software"), to deal in the Software without restriction, including
|
86
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
87
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
88
|
+
permit persons to whom the Software is furnished to do so, subject to
|
89
|
+
the following conditions:
|
90
|
+
|
91
|
+
The above copyright notice and this permission notice shall be
|
92
|
+
included in all copies or substantial portions of the Software.
|
93
|
+
|
94
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
95
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
96
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
97
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
98
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
99
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
100
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Peekaboo
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
TRACE_LEVELS = [ :debug, :info, :warn, :error, :fatal, :unknown ]
|
7
|
+
|
8
|
+
def tracer
|
9
|
+
@tracer ||= Logger.new STDOUT
|
10
|
+
end
|
11
|
+
|
12
|
+
def trace_with tracer
|
13
|
+
if TRACE_LEVELS.all? { |level| tracer.respond_to? level }
|
14
|
+
@tracer = tracer
|
15
|
+
else
|
16
|
+
raise 'Tracer must respond to debug(), info(), warn(), error(), fatal(), and unknown()'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/peekaboo.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'peekaboo/configuration'
|
2
|
+
|
3
|
+
module Peekaboo
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def configuration
|
7
|
+
@configuration ||= Configuration.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def configure
|
11
|
+
yield configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def included klass
|
15
|
+
klass.const_set :PEEKABOO_METHOD_LIST, []
|
16
|
+
klass.extend SingletonMethods
|
17
|
+
|
18
|
+
def klass.method_added name
|
19
|
+
Peekaboo.wrap_method self, name if peek_list.include? name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# @note Should I add execution time to logs?
|
24
|
+
def wrap_method klass, name
|
25
|
+
return if @_adding_a_method
|
26
|
+
@_adding_a_method = true
|
27
|
+
|
28
|
+
original_method = "original_#{name}"
|
29
|
+
method_wrapping = %{
|
30
|
+
alias_method :#{original_method}, :#{name}
|
31
|
+
def #{name} *args, &block
|
32
|
+
trace = "\#{caller(1)[0]}\n\t( Invoking: #{klass}\##{name} with \#{args.inspect} "
|
33
|
+
begin
|
34
|
+
result = #{original_method} *args, &block
|
35
|
+
trace << "==> Returning: \#{result.inspect} )"
|
36
|
+
result
|
37
|
+
rescue Exception => exe
|
38
|
+
trace << "!!! Raising: \#{exe.message.inspect} )"
|
39
|
+
raise exe
|
40
|
+
ensure
|
41
|
+
Peekaboo.configuration.tracer.info trace
|
42
|
+
end
|
43
|
+
end
|
44
|
+
}
|
45
|
+
klass.class_eval method_wrapping
|
46
|
+
|
47
|
+
@_adding_a_method = false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module SingletonMethods
|
52
|
+
def peek_list
|
53
|
+
self::PEEKABOO_METHOD_LIST
|
54
|
+
end
|
55
|
+
|
56
|
+
def peeks_at *method_names
|
57
|
+
method_names.each do |method_name|
|
58
|
+
unless peek_list.include? method_name
|
59
|
+
peek_list << method_name
|
60
|
+
Peekaboo.wrap_method(self, method_name) if self.instance_methods(false).include? method_name.to_s
|
61
|
+
else
|
62
|
+
raise "Already tracing `#{method_name}'"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: peekaboo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sonny Ruben Garcia
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-26 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 1.3.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rcov
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - "="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 41
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 9
|
49
|
+
- 9
|
50
|
+
version: 0.9.9
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: yard
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - "="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 5
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
- 6
|
65
|
+
- 1
|
66
|
+
version: 0.6.1
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
description: Allows you to log method call information ( input arguments, class/method name, and return value ) without being overly intrusive.
|
70
|
+
email: sonny.ruben@gmail.com
|
71
|
+
executables: []
|
72
|
+
|
73
|
+
extensions: []
|
74
|
+
|
75
|
+
extra_rdoc_files: []
|
76
|
+
|
77
|
+
files:
|
78
|
+
- README.md
|
79
|
+
- CHANGELOG.md
|
80
|
+
- lib/peekaboo.rb
|
81
|
+
- lib/peekaboo/configuration.rb
|
82
|
+
- lib/peekaboo/version.rb
|
83
|
+
has_rdoc: true
|
84
|
+
homepage: http://github.com/sgarcia/peekaboo
|
85
|
+
licenses: []
|
86
|
+
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
requirements: []
|
111
|
+
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.3.7
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Beautiful "mixin" magic for tracing Ruby method calls.
|
117
|
+
test_files: []
|
118
|
+
|