autolog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +98 -0
  2. data/Rakefile +11 -0
  3. data/lib/autolog.rb +90 -0
  4. data/lib/autolog/version.rb +3 -0
  5. metadata +51 -0
@@ -0,0 +1,98 @@
1
+ Autolog
2
+ =====
3
+
4
+ Automatically log tracing events in Ruby more easily.
5
+
6
+ To trace Ruby, you can just define `set_trace_func`, e.g.
7
+
8
+ set_trace_func proc { |event, file, line, id, binding, classname|
9
+ printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
10
+ }
11
+ t = Test.new
12
+ t.test
13
+
14
+ line prog.rb:11 false
15
+ c-call prog.rb:11 new Class
16
+ c-call prog.rb:11 initialize Object
17
+ c-return prog.rb:11 initialize Object
18
+ c-return prog.rb:11 new Class
19
+ line prog.rb:12 false
20
+ call prog.rb:2 test Test
21
+ line prog.rb:3 test Test
22
+ line prog.rb:4 test Test
23
+ return prog.rb:4 test Test
24
+
25
+ But, why not use fewer keystrokes to output debug information?
26
+
27
+ Autolog.trace
28
+
29
+ ### Installation
30
+
31
+ In your Gemfile, add:
32
+
33
+ gem 'autolog'
34
+
35
+ Then:
36
+
37
+ bundle install
38
+
39
+ ### Usage
40
+
41
+ Anywhere in your code after the gem is loaded, do one of these:
42
+
43
+ Autolog.c_calls
44
+ Autolog.c_returns
45
+ Autolog.c_calls_and_returns
46
+ Autolog.class_starts
47
+ Autolog.class_ends
48
+ Autolog.classes
49
+ Autolog.method_calls
50
+ Autolog.method_returns
51
+ Autolog.methods
52
+ Autolog.lines
53
+ Autolog.raises
54
+ Autolog.trace
55
+ Autolog.event :c_return
56
+ Autolog.events 'raise', 'c-call'
57
+ Autolog.events :raise, :c_call
58
+ Autolog.off
59
+
60
+ What they do:
61
+
62
+ * `Autolog.c_calls` - logs 'c-call'
63
+ * `Autolog.c_returns` - logs'c-return'
64
+ * `Autolog.c_calls_and_returns` - logs 'c-call' and 'c-return'
65
+ * `Autolog.class_starts` - logs 'class'
66
+ * `Autolog.class_ends` - logs 'end'
67
+ * `Autolog.classes` - logs 'class' and 'end'
68
+ * `Autolog.method_calls` - logs 'call'
69
+ * `Autolog.method_returns` - logs 'return'
70
+ * `Autolog.methods` - logs 'call' and 'return'
71
+ * `Autolog.lines` - logs 'line' (logs every Ruby line executed in this context, similar to a hook into Ruby's caller stack that logs/prints/puts all lines)
72
+ * `Autolog.raises` - logs 'raise'
73
+ * `Autolog.events` or `Autolog.event` - logs one or more provided events, converting each to string and substituting '_' with '-', of the supported events in [set_trace_func][set_trace_func]. Calling with no arguments or empty array will log all events.
74
+ * `Autolog.trace` - logs all events
75
+ * `Autolog.off` - turns off tracing (calls `set_trace_func nil`)
76
+
77
+ ### Changing the format, Using another logger, collecting stats, etc.
78
+
79
+ Keep the ease of Autolog and its minimal controls while doing nuts things with it:
80
+
81
+ Autolog.proc = lambda {|event, file, line, id, binding, classname| puts "#{event} #{file}.#{line} #{binding} #{classname} #{id}"}
82
+
83
+ ### Warning
84
+
85
+ Enabling some of these like lines or trace will significantly slow down execution and may generate a lot of output.
86
+
87
+ ### Contributing
88
+
89
+ It's as easy as [forking][fork], making your changes, and [submitting a pull request][pull].
90
+
91
+ ### License
92
+
93
+ Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
94
+
95
+ [fork]: https://help.github.com/articles/fork-a-repo
96
+ [pull]: https://help.github.com/articles/using-pull-requests
97
+ [set_trace_func]: http://apidock.com/ruby/Kernel/set_trace_func
98
+ [lic]: http://github.com/garysweaver/autolog/blob/master/LICENSE
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+
3
+ #http://nicksda.apotomo.de/2010/10/testing-your-rails-3-engine-sitting-in-a-gem/
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,90 @@
1
+ require 'autolog/version'
2
+
3
+ module Autolog
4
+ class << self
5
+ # called procedure instead of proc because set_trace_func proc was calling the proc attribute. Fun!
6
+ attr_accessor :procedure
7
+
8
+ # log all specified events
9
+ def events(*args)
10
+ args.flatten!
11
+ args.collect!{|e|e.to_s.gsub('_','-')}
12
+
13
+ # What's up with the Exception hiding?
14
+ # 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.
15
+ if args.size == 1
16
+ eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; Autolog.procedure.call(event, file, line, id, binding, classname) if event == #{args[0].inspect}; rescue SystemExit, Interrupt; raise; rescue Exception; end}"
17
+ elsif args.size > 1
18
+ eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; Autolog.procedure.call(event, file, line, id, binding, classname) if #{args.inspect}.include?(event); rescue SystemExit, Interrupt; raise; rescue Exception; end}"
19
+ else
20
+ 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
+ end
22
+ end
23
+ alias_method :event, :events
24
+
25
+ def trace
26
+ events
27
+ end
28
+
29
+ # log c-call events only
30
+ def c_calls
31
+ events 'c-call'
32
+ end
33
+
34
+ # log c-return events only
35
+ def c_returns
36
+ events 'c-return'
37
+ end
38
+
39
+ # log c-call and c-return events only
40
+ def c_calls_and_returns
41
+ events 'c-call', 'c-return'
42
+ end
43
+
44
+ # log class events only
45
+ def class_starts
46
+ events 'class'
47
+ end
48
+
49
+ # log end events only
50
+ def class_ends
51
+ events 'end'
52
+ end
53
+
54
+ # log class and end events only
55
+ def classes
56
+ events 'class', 'end'
57
+ end
58
+
59
+ # log call events only
60
+ def method_calls
61
+ events 'call'
62
+ end
63
+
64
+ # log return events only
65
+ def method_returns
66
+ events 'return'
67
+ end
68
+
69
+ # log call and return events only
70
+ def methods
71
+ events 'call', 'return'
72
+ end
73
+
74
+ # log raise events only
75
+ def raises
76
+ events 'raise'
77
+ end
78
+
79
+ # log line events only
80
+ def lines
81
+ events 'line'
82
+ end
83
+
84
+ def off
85
+ set_trace_func nil
86
+ end
87
+ end
88
+ end
89
+
90
+ Autolog.procedure = lambda {|event, file, line, id, binding, classname| puts "#{event} #{file}.#{line} #{binding} #{classname} #{id}"}
@@ -0,0 +1,3 @@
1
+ module Autolog
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autolog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gary S. Weaver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Automatically log events like executed lines, methods, class and module
15
+ definitions, C-language routines, and/or raises in Ruby.
16
+ email:
17
+ - garysweaver@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/autolog/version.rb
23
+ - lib/autolog.rb
24
+ - Rakefile
25
+ - README.md
26
+ homepage: https://github.com/garysweaver/autolog
27
+ licenses:
28
+ - MIT
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Automatically logs Ruby events.
51
+ test_files: []