caller_log 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 170210b9d7d1c0fe0f6caf0b358cfbccec98e54c
4
+ data.tar.gz: fd1c8f249534e9d62ff28e55d1ebd1014832ded1
5
+ SHA512:
6
+ metadata.gz: 81309e4dc08389dc2f29bd8d45d43c1375e96aa739c4694db5adeeb84fb94bd9d7b0dba99b4a98bdd5e229babdad073f863f75c1df244026aba7bf06d460302d
7
+ data.tar.gz: 5ad9e14f9dcf5a4324372c54ed072a7425081506f7d007fb1574f722abc119f78df32cd45d9143ad7c6fbd3a9ac283efdcb705b12258cd61424c025a212893b3
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
11
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in caller_log.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 ken
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # CallerLog
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/caller_log`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'caller_log'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install caller_log
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ken/caller_log.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "caller_log"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'caller_log/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "caller_log"
8
+ spec.version = CallerLog::VERSION
9
+ spec.authors = ["ken"]
10
+ spec.email = ["block24block@gmail.com"]
11
+
12
+ spec.summary = %q{Log every call to a object}
13
+ spec.homepage = 'https://github.com/turnon/caller_log'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+
27
+ spec.add_dependency "dcr"
28
+ spec.add_dependency "binding_of_callers"
29
+ spec.add_dependency "nokogiri", "~> 1.7.1"
30
+ end
@@ -0,0 +1,80 @@
1
+ require 'set'
2
+ require 'nokogiri'
3
+ require 'binding_of_callers/pry'
4
+ require 'cgi'
5
+
6
+ module CallerLog
7
+
8
+ class Log
9
+
10
+ attr_reader :file, :html, :lock
11
+
12
+ def initialize file
13
+ file = File.new file, 'a+' unless File === file
14
+ @file = file
15
+ @html = Html.new
16
+ @lock = Mutex.new
17
+ end
18
+
19
+ def puts record
20
+ lock.synchronize do
21
+ html.add record
22
+ File.open file, 'w+' do |f|
23
+ f.puts html.to_s
24
+ end
25
+ end
26
+ end
27
+
28
+ class Html
29
+
30
+ Template = File.expand_path('../template.html', __FILE__)
31
+
32
+ attr_reader :base, :threads
33
+
34
+ def initialize
35
+ @base = File.open(Template) { |f| Nokogiri::HTML(f) }
36
+ @threads = Set.new
37
+ end
38
+
39
+ def add record
40
+ new_thread_fragment
41
+ new_call_fragment record
42
+ new_stack_fragment record
43
+ end
44
+
45
+ def new_thread_fragment
46
+ return if threads.include?(id = Thread.current.object_id)
47
+ thread = Nokogiri::HTML::DocumentFragment.parse <<-EOHTML
48
+ <li id='thread-#{id}'>
49
+ <input type="checkbox" name=threads value='#{id}' checked='checked'>#{id}</input>
50
+ </li>
51
+ EOHTML
52
+ base.at_css('.threads') << thread
53
+ threads << id
54
+ end
55
+
56
+ def new_call_fragment record
57
+ time = record.time.strftime "%F %T"
58
+ call = Nokogiri::HTML::DocumentFragment.parse <<-EOHTML
59
+ <li id='call-#{record.object_id}' class='thread-#{Thread.current.object_id} call'>
60
+ <span class='time'>#{time}</span><span class='class_and_method'>#{record.module}##{record.method_id}</span>
61
+ </li>
62
+ EOHTML
63
+ base.at_css('.calls') << call
64
+ end
65
+
66
+ def new_stack_fragment record
67
+ callers = record.callers.map do |c|
68
+ class_and_method = CGI::escapeHTML "#{c.klass}#{c.call_symbol}#{c.frame_env}"
69
+ "<p><span class='class_and_method'>#{class_and_method}</span><span class='location'>#{c.file}:#{c.line}</span></p>"
70
+ end.join
71
+ stack = "<li class='call-#{record.object_id}'>#{callers}</li>"
72
+ base.at_css('.stacks') << stack
73
+ end
74
+
75
+ def to_s
76
+ base.to_s
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,110 @@
1
+ <!DOCTYPE HTML>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'/>
5
+ <style>
6
+ ul {
7
+ padding-left: 0;
8
+ }
9
+
10
+ li {
11
+ list-style: none;
12
+ margin-bottom: .8rem;
13
+ }
14
+
15
+ ul.threads li {
16
+ float: left;
17
+ margin-top: -.5rem;
18
+ }
19
+
20
+ ul.calls {
21
+ position: fixed;
22
+ margin-top: 2rem;
23
+ top: 1rem;
24
+ bottom: 0;
25
+ width: 18rem;
26
+ overflow: auto;
27
+ }
28
+
29
+ ul.stacks {
30
+ position: fixed;
31
+ left: 20rem;
32
+ top: 1rem;
33
+ bottom: 0;
34
+ margin-top: 2rem;
35
+ overflow: auto;
36
+ width: calc(100% - 20rem);
37
+ }
38
+
39
+ p {
40
+ margin:0 0 0 0;
41
+ }
42
+
43
+ span.time {
44
+ display: block;
45
+ font-size: smaller;
46
+ }
47
+
48
+ .stacks span.class_and_method {
49
+ color: gray;
50
+ margin-right: .5rem;
51
+ }
52
+
53
+ .hidden {
54
+ display: none;
55
+ }
56
+
57
+ ul.stacks li {
58
+ display: none;
59
+ }
60
+
61
+ ul.stacks li.show {
62
+ display: block;
63
+ }
64
+ </style>
65
+ </head>
66
+ <body>
67
+ <ul class='threads'>
68
+ </ul>
69
+ <ul class='calls'>
70
+ </ul>
71
+ <ul class='stacks'>
72
+ </ul>
73
+ <script>
74
+ (function(){
75
+
76
+ function calls_in_thread(id){
77
+ return document.querySelectorAll('.thread-' + id);
78
+ }
79
+
80
+ function show_call(thread_id){
81
+ calls_in_thread(thread_id).forEach(function(call){
82
+ call.classList.remove('hidden');
83
+ });
84
+ return true;
85
+ }
86
+
87
+ function hide_call(thread_id){
88
+ calls_in_thread(thread_id).forEach(function(call){
89
+ call.classList.add('hidden');
90
+ });
91
+ }
92
+
93
+ function toggle_stack(call_li){
94
+ var curr_shown = document.querySelector('.show')
95
+ curr_shown && curr_shown.classList.remove('show');
96
+ document.querySelector('.' + call_li.id).classList.add('show');
97
+ }
98
+
99
+ var body = document.getElementsByTagName('body')[0];
100
+
101
+ body.addEventListener('click', function(e){
102
+ var target = e.target;
103
+ target.type == 'checkbox' && target.checked && show_call(target.value) || hide_call(target.value);
104
+ e.target.parentElement.classList.contains('call') && toggle_stack(e.target.parentElement);
105
+ }, false);
106
+
107
+ })();
108
+ </script>
109
+ </body>
110
+ </html>
@@ -0,0 +1,3 @@
1
+ module CallerLog
2
+ VERSION = "0.1.0"
3
+ end
data/lib/caller_log.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'caller_log/version'
2
+ require 'caller_log/log'
3
+ require 'binding_of_callers'
4
+ require 'dcr'
5
+
6
+ module CallerLog
7
+ class << self
8
+
9
+ def instance *args
10
+ raise ArgumentError,
11
+ "wrong number of arguments (given #{args.count}, expected more than 1)" if args.count < 2
12
+ log_file = args.pop
13
+ modules = Array args
14
+
15
+ log = Log.new log_file
16
+
17
+ modules.each do |mod|
18
+ mod.instance_methods(false).each do |method_id|
19
+ Dcr.instance mod, method_id do |method, *para, &blk|
20
+ record = OpenStruct.new time: Time.now, module: mod, method_id: method_id, callers: binding.of_callers[2..-1]
21
+ log.puts record
22
+ method.call *para, &blk
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caller_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ken
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: binding_of_callers
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nokogiri
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.7.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.7.1
97
+ description:
98
+ email:
99
+ - block24block@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - caller_log.gemspec
113
+ - lib/caller_log.rb
114
+ - lib/caller_log/log.rb
115
+ - lib/caller_log/template.html
116
+ - lib/caller_log/version.rb
117
+ homepage: https://github.com/turnon/caller_log
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.8
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Log every call to a object
141
+ test_files: []