autolog 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,9 +22,11 @@ To trace Ruby, you can just define `set_trace_func`, e.g.
22
22
  line prog.rb:4 test Test
23
23
  return prog.rb:4 test Test
24
24
 
25
- But, why not use fewer keystrokes to output debug information?
25
+ But, why not use fewer keystrokes to output debug information? And what about just logging certain sets of events? How about:
26
26
 
27
- Autolog.trace
27
+ Autolog.events :raise, :c_call
28
+
29
+ Also, check out the Ruby stdlib's [Tracer][tracer], which does similar if you are just looking for a simple trace or need the other options or added speed that it may provide. Autolog is really just meant to be a shortcut for Kernel's set_trace_func, which is a lot more flexible in some ways, since it lets you hook into/log other types of events specifically.
28
30
 
29
31
  ### Installation
30
32
 
@@ -38,7 +40,27 @@ Then:
38
40
 
39
41
  ### Usage
40
42
 
41
- Anywhere in your code after the gem is loaded, do one of these:
43
+ In the main object/IRB, or in any object, call autolog with parameters, e.g.:
44
+
45
+ autolog :methods
46
+ autolog :c_calls
47
+ autolog :c_returns
48
+ autolog :c_calls_and_returns
49
+ autolog :class_starts
50
+ autolog :class_ends
51
+ autolog :classes
52
+ autolog :method_calls
53
+ autolog :method_returns
54
+ autolog :methods
55
+ autolog :lines
56
+ autolog :raises
57
+ autolog :trace
58
+ autolog :event :c_return
59
+ autolog :events 'raise', 'c-call'
60
+ autolog :events :raise, :c_call
61
+ autolog :off
62
+
63
+ Or call it on Autolog if that is easier:
42
64
 
43
65
  Autolog.c_calls
44
66
  Autolog.c_returns
@@ -57,10 +79,47 @@ Anywhere in your code after the gem is loaded, do one of these:
57
79
  Autolog.events :raise, :c_call
58
80
  Autolog.off
59
81
 
60
- What they do:
82
+ ### Blocks
83
+
84
+ autolog do
85
+ puts "this is something"
86
+ puts "this is something 2"
87
+ end
88
+
89
+ or
90
+
91
+ autolog :methods do
92
+ # ...
93
+ end
94
+
95
+ or
96
+
97
+ autolog :lines do
98
+ # ...
99
+ end
100
+
101
+ or
102
+
103
+ autolog :events, :line, :c_call do
104
+ # ...
105
+ end
106
+
107
+ or
108
+
109
+ Autolog.methods do
110
+ # ...
111
+ end
112
+
113
+ or
114
+
115
+ Autolog.events :line, :c_call do
116
+ # ...
117
+ end
118
+
119
+ ### What you can trace
61
120
 
62
121
  * `Autolog.c_calls` - logs 'c-call'
63
- * `Autolog.c_returns` - logs'c-return'
122
+ * `Autolog.c_returns` - logs 'c-return'
64
123
  * `Autolog.c_calls_and_returns` - logs 'c-call' and 'c-return'
65
124
  * `Autolog.class_starts` - logs 'class'
66
125
  * `Autolog.class_ends` - logs 'end'
@@ -94,5 +153,6 @@ Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
94
153
 
95
154
  [fork]: https://help.github.com/articles/fork-a-repo
96
155
  [pull]: https://help.github.com/articles/using-pull-requests
156
+ [tracer]: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tracer/rdoc/index.html
97
157
  [set_trace_func]: http://apidock.com/ruby/Kernel/set_trace_func
98
158
  [lic]: http://github.com/garysweaver/autolog/blob/master/LICENSE
@@ -1,4 +1,5 @@
1
1
  require 'autolog/version'
2
+ require 'autolog/methods'
2
3
 
3
4
  module Autolog
4
5
  class << self
@@ -9,6 +10,7 @@ module Autolog
9
10
  def events(*args)
10
11
  args.flatten!
11
12
  args.collect!{|e|e.to_s.gsub('_','-')}
13
+ puts "events method received #{args.inspect}"
12
14
 
13
15
  # What's up with the Exception hiding?
14
16
  # Ruby bug 7180: can use up 100% cpu in 1.9.3p194 if let anything be raised. We'll silently rescue and ignore issues. Otherwise, it produces a deluge of output.
@@ -19,72 +21,135 @@ module Autolog
19
21
  else
20
22
  set_trace_func proc {|event, file, line, id, binding, classname| begin; Autolog.procedure.call(event, file, line, id, binding, classname); rescue SystemExit, Interrupt; raise; rescue Exception; end}
21
23
  end
24
+
25
+ if block_given?
26
+ yield
27
+ off
28
+ end
22
29
  end
23
30
  alias_method :event, :events
24
31
 
25
- def trace
26
- events
32
+ def trace(*args)
33
+ if block_given?
34
+ events args, &Proc.new
35
+ else
36
+ events args
37
+ end
27
38
  end
28
39
 
29
40
  # log c-call events only
30
- def c_calls
31
- events 'c-call'
41
+ def c_calls(*args)
42
+ if block_given?
43
+ events ['c-call', args].flatten, &Proc.new
44
+ else
45
+ events ['c-call', args].flatten
46
+ end
32
47
  end
33
48
 
34
49
  # log c-return events only
35
- def c_returns
36
- events 'c-return'
50
+ def c_returns(*args)
51
+ if block_given?
52
+ events ['c-return', args].flatten, &Proc.new
53
+ else
54
+ events ['c-return', args].flatten
55
+ end
37
56
  end
38
57
 
39
58
  # log c-call and c-return events only
40
- def c_calls_and_returns
41
- events 'c-call', 'c-return'
59
+ def c_calls_and_returns(*args)
60
+ if block_given?
61
+ events ['c-call', 'c-return', args].flatten, &Proc.new
62
+ else
63
+ events ['c-call', 'c-return', args].flatten
64
+ end
42
65
  end
43
66
 
44
67
  # log class events only
45
- def class_starts
46
- events 'class'
68
+ def class_starts(*args)
69
+ if block_given?
70
+ events ['class', args].flatten, &Proc.new
71
+ else
72
+ events ['class', args].flatten
73
+ end
47
74
  end
48
75
 
49
76
  # log end events only
50
- def class_ends
51
- events 'end'
77
+ def class_ends(*args)
78
+ if block_given?
79
+ events ['end', args].flatten, &Proc.new
80
+ else
81
+ events ['end', args].flatten
82
+ end
52
83
  end
53
84
 
54
85
  # log class and end events only
55
- def classes
56
- events 'class', 'end'
86
+ def classes(*args)
87
+ if block_given?
88
+ events ['class', 'end', args].flatten, &Proc.new
89
+ else
90
+ events ['class', 'end', args].flatten
91
+ end
57
92
  end
58
93
 
59
94
  # log call events only
60
- def method_calls
61
- events 'call'
95
+ def method_calls(*args)
96
+ if block_given?
97
+ events ['call', args].flatten, &Proc.new
98
+ else
99
+ events ['call', args].flatten
100
+ end
62
101
  end
63
102
 
64
103
  # log return events only
65
- def method_returns
66
- events 'return'
104
+ def method_returns(*args)
105
+ if block_given?
106
+ events ['return', args].flatten, &Proc.new
107
+ else
108
+ events ['return', args].flatten
109
+ end
67
110
  end
68
111
 
69
112
  # log call and return events only
70
- def methods
71
- events 'call', 'return'
113
+ def methods(*args)
114
+ if block_given?
115
+ events ['call', 'return'], &Proc.new
116
+ else
117
+ events ['call', 'return', args].flatten
118
+ end
72
119
  end
73
120
 
74
121
  # log raise events only
75
- def raises
76
- events 'raise'
122
+ def raises(*args)
123
+ if block_given?
124
+ events ['raise', args].flatten, &Proc.new
125
+ else
126
+ events ['raise', args].flatten
127
+ end
77
128
  end
78
129
 
79
130
  # log line events only
80
- def lines
81
- events 'line'
131
+ def lines(*args)
132
+ if block_given?
133
+ events ['line', args].flatten, &Proc.new
134
+ else
135
+ events ['line', args].flatten
136
+ end
82
137
  end
83
138
 
84
- def off
139
+ # turn logging off
140
+ def off(*args)
141
+ # accepts *args to make implementation of autolog in methods easier, but ignores them
85
142
  set_trace_func nil
86
143
  end
87
144
  end
88
145
  end
89
146
 
90
- Autolog.procedure = lambda {|event, file, line, id, binding, classname| puts "#{event} #{file}.#{line} #{binding} #{classname} #{id}"}
147
+ Autolog.procedure = lambda {|event, file, line, id, binding, classname| begin; puts "#{event} #{file}.#{line} #{binding} #{classname} #{id}"; rescue SystemExit, Interrupt; raise; rescue Exception; end}
148
+
149
+ class Object
150
+ # make autolog a method in any object
151
+ extend Autolog::Methods
152
+ end
153
+
154
+ # make autolog a method in main
155
+ TOPLEVEL_BINDING.eval('include Autolog::Methods')
@@ -0,0 +1,25 @@
1
+ module Autolog
2
+ module Methods
3
+ def autolog(*args)
4
+ args.flatten!
5
+ puts "autolog method got #{args.inspect}"
6
+ if args.size > 0
7
+ if Autolog.respond_to?(args[0])
8
+ if block_given?
9
+ Autolog.send(args.delete_at(0), args, &Proc.new)
10
+ else
11
+ Autolog.send(args.delete_at(0), args)
12
+ end
13
+ elsif block_given?
14
+ Autolog.events args, &Proc.new
15
+ else
16
+ Autolog.events args
17
+ end
18
+ elsif block_given?
19
+ Autolog.events &Proc.new
20
+ else
21
+ Autolog.events
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Autolog
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autolog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -19,6 +19,7 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
+ - lib/autolog/methods.rb
22
23
  - lib/autolog/version.rb
23
24
  - lib/autolog.rb
24
25
  - Rakefile