power_trace 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e0d694b8c7925be5e04c1f5f3ea4a5da6b35c877b21889b4087932431cbafb5b
4
+ data.tar.gz: 36e4678c7647402010216e5312f42f4675eaafcd8d46f32c8564b0ccc97cd161
5
+ SHA512:
6
+ metadata.gz: 66bde570863c5e24f4c8a853fa68e40f810f5a5278e2296a52ca8b4fa6cc1e0ad47394453e7814b8b61355576075a3a9dbd8534bb63a7404d7e15a093bed102b
7
+ data.tar.gz: 5a24fd2fd709ad654b12adf15e1a321425316839e400d33d75a336b78565ee3c6520daad0cf881f32c13d5ec0d5e09df492631f8c554b0d66843a4b68d9fc8dd
@@ -0,0 +1,28 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ build:
10
+ name: Build + Publish
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@master
15
+ - name: Set up Ruby 2.6
16
+ uses: actions/setup-ruby@v1
17
+ with:
18
+ version: 2.6.x
19
+ - name: Publish to RubyGems
20
+ run: |
21
+ mkdir -p $HOME/.gem
22
+ touch $HOME/.gem/credentials
23
+ chmod 0600 $HOME/.gem/credentials
24
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
25
+ gem build *.gemspec
26
+ gem push *.gem
27
+ env:
28
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,39 @@
1
+ name: Ruby
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ name: Test on ruby ${{ matrix.ruby_version }}
8
+ runs-on: ${{ matrix.os }}
9
+ strategy:
10
+ matrix:
11
+ ruby_version: ['2.6']
12
+ os: [ubuntu-latest]
13
+ steps:
14
+ - uses: actions/checkout@v1
15
+
16
+ - name: Set up Ruby ${{ matrix.ruby_version }}
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby_version }}
20
+
21
+ - name: Setup
22
+ run: |
23
+ gem install bundler
24
+ bundle install --jobs 4 --retry 3
25
+
26
+ - name: Run test
27
+ run: bundle exec rake
28
+
29
+ - name: Publish Test Coverage
30
+ uses: paambaati/codeclimate-action@v2.6.0
31
+ env:
32
+ CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
33
+ if: ${{ env.CC_TEST_REPORTER_ID != '' }}
34
+ with:
35
+ # the coverage result should already be generated by the previous step
36
+ # so we don't need to provide and command in the step
37
+ # this is just a placeholder to avoid it run the default `yarn coverage` command
38
+ coverageCommand: ruby -v
39
+ debug: true
@@ -0,0 +1,60 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ .DS_Store
14
+
15
+ # Used by dotenv library to load environment variables.
16
+ # .env
17
+
18
+ # Ignore Byebug command history file.
19
+ .byebug_history
20
+
21
+ .rspec_status
22
+
23
+ ## Specific to RubyMotion:
24
+ .dat*
25
+ .repl_history
26
+ build/
27
+ *.bridgesupport
28
+ build-iPhoneOS/
29
+ build-iPhoneSimulator/
30
+
31
+ ## Specific to RubyMotion (use of CocoaPods):
32
+ #
33
+ # We recommend against adding the Pods directory to your .gitignore. However
34
+ # you should judge for yourself, the pros and cons are mentioned at:
35
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
36
+ #
37
+ # vendor/Pods/
38
+
39
+ ## Documentation cache and generated files:
40
+ /.yardoc/
41
+ /_yardoc/
42
+ /doc/
43
+ /rdoc/
44
+
45
+ ## Environment normalization:
46
+ /.bundle/
47
+ /vendor/bundle
48
+ /lib/bundler/man/
49
+
50
+ # for a library or gem, you might want to ignore these files since the code is
51
+ # intended to run in multiple environments; otherwise, check them in:
52
+ # Gemfile.lock
53
+ # .ruby-version
54
+ # .ruby-gemset
55
+
56
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
57
+ .rvmrc
58
+
59
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
60
+ # .rubocop-https?--*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at stan001212@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in power_trace.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ power_trace (0.1.0)
5
+ activesupport
6
+ pry
7
+ pry-stack_explorer
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.0.3.2)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ zeitwerk (~> 2.2, >= 2.2.2)
18
+ binding_of_caller (0.8.0)
19
+ debug_inspector (>= 0.0.1)
20
+ coderay (1.1.3)
21
+ concurrent-ruby (1.1.6)
22
+ debug_inspector (0.0.3)
23
+ diff-lcs (1.3)
24
+ i18n (1.8.3)
25
+ concurrent-ruby (~> 1.0)
26
+ method_source (1.0.0)
27
+ minitest (5.14.1)
28
+ pry (0.13.1)
29
+ coderay (~> 1.1)
30
+ method_source (~> 1.0)
31
+ pry-stack_explorer (0.5.1)
32
+ binding_of_caller (~> 0.7)
33
+ pry (~> 0.13)
34
+ rake (12.3.3)
35
+ rspec (3.9.0)
36
+ rspec-core (~> 3.9.0)
37
+ rspec-expectations (~> 3.9.0)
38
+ rspec-mocks (~> 3.9.0)
39
+ rspec-core (3.9.1)
40
+ rspec-support (~> 3.9.1)
41
+ rspec-expectations (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-mocks (3.9.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.9.0)
47
+ rspec-support (3.9.2)
48
+ thread_safe (0.3.6)
49
+ tzinfo (1.2.7)
50
+ thread_safe (~> 0.1)
51
+ zeitwerk (2.3.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ power_trace!
58
+ rake (~> 12.0)
59
+ rspec (~> 3.0)
60
+
61
+ BUNDLED WITH
62
+ 2.1.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Stan Lo
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 st0012
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.
@@ -0,0 +1,122 @@
1
+ # power_trace
2
+
3
+ ![Ruby](https://github.com/st0012/power_trace/workflows/Ruby/badge.svg)
4
+
5
+ Backtrace (Stack traces) are essential information for debugging our applications. However, they only tell us what the program did, but don't tell us what it had (the arguments, local variables...etc.). So it's very often that we'd need to visit each call site, rerun the program, and try to print out the variables. To me, It's like the Google map's navigation only tells us the name of the roads, but not showing us the map along with them.
6
+
7
+ So I hope to solve this problem by adding some additional runtime info to the backtrace, and save us the work to manually look them up.
8
+
9
+ **Please Don't Use It On Production**
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'power_trace', group: [:test, :development]
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```
22
+ $ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```
28
+ $ gem install power_trace
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ### Call `power_trace` directly
34
+
35
+ If you call `power_trace` directly, it'll return a `PowerTrace::Stack` instance that contains all the processed traces. You can then use it in 3 different ways:
36
+
37
+ - Print it directly
38
+ - Access each trace (`Entry` instance)
39
+ - Convert it into backtraces (an array of strings)
40
+
41
+ #### Print It Directly
42
+
43
+ You can use `puts(power_trace)` to print the beautiful output to stdout:
44
+
45
+ ![print power_trace directly](https://github.com/st0012/power_trace/blob/master/images/print_directly.png)
46
+
47
+ It should look just like the normal `puts(caller)`, just colorized and with more helpful info.
48
+
49
+ #### Access Individual Entries
50
+
51
+ Except for the call site, each entry also contains rich information about the runtime context. You can build your own debugging tool with that information easily.
52
+
53
+ There are 2 types of entries:
54
+
55
+ - `MethodEntry`- a method call's trace
56
+ - `BlockEntry` - a block evaluation's trace
57
+
58
+ They both have these attributes:
59
+
60
+ - `filepath`
61
+ - `line_number`
62
+ - `receiver` - the receiver object
63
+ - `frame` - the call frame (`Binding`) object
64
+ - `locals` - local variables in that frame
65
+ - `arguments`
66
+ - the method call's arguments
67
+ - will always be empty for a `BlockEntry`
68
+
69
+ ![use individual entries](https://github.com/st0012/power_trace/blob/master/images/entries.png)
70
+
71
+ #### Convert It Into Backtraces
72
+
73
+ You can do it by calling `power_trace.to_backtrace`. The major usage is to replace an exception object's backtrace like
74
+
75
+ ```ruby
76
+ a_exception.set_backtrace(power_trace.to_backtrace)
77
+ ```
78
+
79
+ I don't recommend using it like this for other purposes, though. Because by default, all entries will be color-encoded strings. Also, the embedded arguments/locals aren't easily parseable. For other uses, you should either print it directly or process the traces without calling `to_backtrace`.
80
+
81
+ #### Options
82
+
83
+ - `colorize` - to decide whether to colorize each entry in their string format. Default is `true`.
84
+ - `line_limit` - `power_trace` truncates every argument/local variable's value to avoid creating too much noise. Default is `100`
85
+
86
+ ### Get `power_trace` From An Exception (Experimental)
87
+
88
+ You can also access an exception object's enhanced backtrace with the `power_trace` method:
89
+
90
+ ```ruby
91
+ begin
92
+ perform_a_call
93
+ rescue => e
94
+ e.power_trace # <= like this
95
+ end
96
+ ```
97
+
98
+ And use it as you get it from the call site.
99
+
100
+ ## Inspirations & Helpful Tools
101
+
102
+ - [pretty_backtrace](https://github.com/ko1/pretty_backtrace)
103
+ - [pry-stack_explorer](https://github.com/pry/pry-stack_explorer)
104
+ - [tapping_device](https://github.com/st0012/tapping_device)
105
+
106
+ ## Development
107
+
108
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
109
+
110
+ 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/).
111
+
112
+ ## Contributing
113
+
114
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/[USERNAME]/power_trace](https://github.com/%5BUSERNAME%5D/power_trace). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/%5BUSERNAME%5D/power_trace/blob/master/CODE_OF_CONDUCT.md).
115
+
116
+ ## License
117
+
118
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
119
+
120
+ ## Code of Conduct
121
+
122
+ Everyone interacting in the PowerTrace project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/%5BUSERNAME%5D/power_trace/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "power_trace"
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__)
@@ -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
Binary file
Binary file
@@ -0,0 +1,12 @@
1
+ require "power_trace/version"
2
+ require "power_trace/stack"
3
+
4
+ module PowerTrace
5
+ def power_trace(options = {})
6
+ PowerTrace::Stack.new(options)
7
+ end
8
+ end
9
+
10
+ include PowerTrace
11
+
12
+ require "power_trace/exception_patch"
@@ -0,0 +1,7 @@
1
+ module PowerTrace
2
+ class BlockEntry < Entry
3
+ def method_parameters
4
+ []
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module PowerTrace
2
+ class MethodEntry < Entry
3
+ def method
4
+ @method ||= Object.instance_method(:method).bind(@receiver).call(name)
5
+ end
6
+
7
+ def method_parameters
8
+ method.parameters.map { |parameter| parameter[1] }
9
+ end
10
+
11
+ def defined_class
12
+ method.owner
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,130 @@
1
+ require "power_trace/helpers/colorize_helper"
2
+ require "active_support/core_ext/string"
3
+
4
+ module PowerTrace
5
+ class Entry
6
+ include ColorizeHelper
7
+ UNDEFINED = "[undefined]"
8
+
9
+ INDENT = "\s" * 4
10
+
11
+ attr_reader :frame, :filepath, :line_number, :receiver, :locals, :arguments
12
+
13
+ def initialize(frame)
14
+ @frame = frame
15
+ @filepath = frame.eval("__FILE__")
16
+ @line_number = frame.eval("__LINE__")
17
+ @receiver = frame.receiver
18
+ @locals, @arguments = colloct_locals_and_arguments
19
+ end
20
+
21
+ def name(options = {})
22
+ frame.frame_description
23
+ end
24
+
25
+ def location(options = {})
26
+ "#{filepath}:#{line_number}"
27
+ end
28
+
29
+ def arguments_string(options = {})
30
+ hash_to_string(arguments, false, options[:line_limit])
31
+ end
32
+
33
+ def locals_string(options = {})
34
+ hash_to_string(locals, false, options[:line_limit])
35
+ end
36
+
37
+ def call_trace(options = {})
38
+ "#{location(options)}:in `#{name(options)}'"
39
+ end
40
+
41
+ ATTRIBUTE_COLORS = {
42
+ name: COLORS[:blue],
43
+ location: COLORS[:green],
44
+ arguments_string: COLORS[:orange],
45
+ locals_string: COLORS[:megenta]
46
+ }
47
+
48
+ ATTRIBUTE_COLORS.each do |attribute, color|
49
+ alias_method "original_#{attribute}".to_sym, attribute
50
+
51
+ # regenerate attributes with `colorize: true` support
52
+ define_method attribute do |options = {}|
53
+ call_result = send("original_#{attribute}", options)
54
+
55
+ if options[:colorize]
56
+ "#{color}#{call_result}#{COLORS[:reset]}"
57
+ else
58
+ call_result
59
+ end
60
+ end
61
+ end
62
+
63
+ def to_s(options = {})
64
+ assemble_string(options)
65
+ end
66
+
67
+ private
68
+
69
+ def assemble_string(options)
70
+ strings = [call_trace(options)]
71
+
72
+ if arguments.present?
73
+ strings << <<~STR.chomp
74
+ (Arguments)
75
+ #{arguments_string(options)}
76
+ STR
77
+ end
78
+
79
+ if locals.present?
80
+ strings << <<~STR.chomp
81
+ (Locals)
82
+ #{locals_string(options)}
83
+ STR
84
+ end
85
+
86
+ strings.join("\n")
87
+ end
88
+
89
+ def hash_to_string(hash, inspect, truncation)
90
+ elements_string = hash.map do |key, value|
91
+ value_string = value_to_string(value, truncation)
92
+ "#{key.to_s}: #{value_string}"
93
+ end.join("\n#{INDENT}")
94
+ "#{INDENT}#{elements_string}"
95
+ end
96
+
97
+ def value_to_string(value, truncation)
98
+ case value
99
+ when Array
100
+ value.to_s.truncate(truncation, omission: "...]")
101
+ when Hash
102
+ value.to_s.truncate(truncation, omission: "...}")
103
+ when nil
104
+ "nil"
105
+ else
106
+ value.to_s.truncate(truncation)
107
+ end
108
+ end
109
+
110
+ def colloct_locals_and_arguments
111
+ locals = {}
112
+ arguments = {}
113
+
114
+ frame.local_variables.each do |name|
115
+ value = frame.local_variable_get(name)
116
+
117
+ if method_parameters.include?(name)
118
+ arguments[name] = value
119
+ else
120
+ locals[name] = value
121
+ end
122
+ end
123
+
124
+ [locals, arguments]
125
+ end
126
+ end
127
+ end
128
+
129
+ require "power_trace/entries/method_entry"
130
+ require "power_trace/entries/block_entry"
@@ -0,0 +1,14 @@
1
+ class Exception
2
+ attr_accessor :power_trace
3
+ end
4
+
5
+ TracePoint.trace(:raise) do |tp|
6
+ begin
7
+ e = tp.raised_exception
8
+ e.power_trace = power_trace(exception: true)
9
+ rescue => e
10
+ puts e
11
+ puts e.backtrace
12
+ fail "power_trace BUG"
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module PowerTrace
2
+ module ColorizeHelper
3
+ COLOR_CODES = {
4
+ green: 10,
5
+ yellow: 11,
6
+ blue: 12,
7
+ megenta: 13,
8
+ cyan: 14,
9
+ orange: 214
10
+ }
11
+
12
+ RESET_MARK = "\u001b[0m"
13
+
14
+ COLOR_CODES.each do |color, code|
15
+ define_method "#{color}_color" do |str|
16
+ color_mark = "\u001b[38;5;#{code}m"
17
+ "#{color_mark}#{str}#{RESET_MARK}"
18
+ end
19
+ end
20
+
21
+ COLORS = COLOR_CODES.each_with_object({}) do |(name, code), hash|
22
+ hash[name] = "\u001b[38;5;#{code}m"
23
+ end.merge(
24
+ reset: "\u001b[0m",
25
+ nocolor: ""
26
+ )
27
+ end
28
+ end
@@ -0,0 +1,68 @@
1
+ require "power_trace/entry"
2
+ require "pry-stack_explorer"
3
+
4
+ module PowerTrace
5
+ class Stack
6
+ include Enumerable
7
+
8
+ attr_reader :entries
9
+
10
+ OUTPUT_OPTIONS_DEFAULT = {
11
+ colorize: true,
12
+ line_limit: 100
13
+ }
14
+
15
+ def initialize(options = {})
16
+ @options = options
17
+ @exception = options.fetch(:exception, false)
18
+ @entries = extract_entries.compact
19
+ end
20
+
21
+ def each(&block)
22
+ @entries.each(&block)
23
+ end
24
+
25
+ def to_backtrace(output_options = {})
26
+ output_options = extract_output_options(output_options)
27
+ @entries.map { |e| e.to_s(output_options) }
28
+ end
29
+
30
+ def to_s(output_options = {})
31
+ to_backtrace(output_options).join("\n")
32
+ end
33
+
34
+ def empty?
35
+ @entries.empty?
36
+ end
37
+
38
+ private
39
+
40
+ def extract_output_options(options)
41
+ OUTPUT_OPTIONS_DEFAULT.each_with_object({}) do |(option_name, default), output_options|
42
+ output_options[option_name] = options.fetch(option_name, default)
43
+ options.delete(option_name)
44
+ end
45
+ end
46
+
47
+ def frame_manager
48
+ PryStackExplorer.frame_manager(Pry.new)
49
+ end
50
+
51
+ def extract_entries
52
+ frames = frame_manager.bindings
53
+ # when using pry console, the power_trace_index will be `nil` and breaks EVERYTHING
54
+ # so we should fallback it to 0
55
+ power_trace_index = (frames.index { |b| b.frame_description&.to_sym == :power_trace } || 0) + 1
56
+ power_trace_index += 1 if @exception
57
+
58
+ frames[power_trace_index..].map do |b|
59
+ case b.frame_type
60
+ when :method
61
+ MethodEntry.new(b)
62
+ when :block
63
+ BlockEntry.new(b)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module PowerTrace
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/power_trace/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "power_trace"
5
+ spec.version = PowerTrace::VERSION
6
+ spec.authors = ["st0012"]
7
+ spec.email = ["stan001212@gmail.com"]
8
+
9
+ spec.summary = %q{power_trace provides you a more powerful backtrace.}
10
+ spec.description = %q{Backtrace (Stack traces) are essential information for debugging our applications. However, they only tell us what the program did, but don't tell us what it had (the arguments, local variables...etc.). So it's very often that we'd need to visit each call site, rerun the program, and try to print out the variables. To me, It's like the Google map's navigation only tells us the name of the roads, but not showing us the map along with them.
11
+
12
+ So I hope to solve this problem by adding some additional runtime info to the backtrace, and save us the work to manually look them up.}
13
+
14
+ spec.homepage = "https://github.com/st0012/power_trace"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "activesupport"
32
+ spec.add_dependency "pry"
33
+ spec.add_dependency "pry-stack_explorer"
34
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: power_trace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - st0012
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-stack_explorer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |-
56
+ Backtrace (Stack traces) are essential information for debugging our applications. However, they only tell us what the program did, but don't tell us what it had (the arguments, local variables...etc.). So it's very often that we'd need to visit each call site, rerun the program, and try to print out the variables. To me, It's like the Google map's navigation only tells us the name of the roads, but not showing us the map along with them.
57
+
58
+ So I hope to solve this problem by adding some additional runtime info to the backtrace, and save us the work to manually look them up.
59
+ email:
60
+ - stan001212@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".github/workflows/gempush.yml"
66
+ - ".github/workflows/ruby.yml"
67
+ - ".gitignore"
68
+ - ".rspec"
69
+ - ".travis.yml"
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - bin/console
78
+ - bin/setup
79
+ - images/entries.png
80
+ - images/normal_backtrace.png
81
+ - images/power_trace_backtrace.png
82
+ - images/print_directly.png
83
+ - lib/power_trace.rb
84
+ - lib/power_trace/entries/block_entry.rb
85
+ - lib/power_trace/entries/method_entry.rb
86
+ - lib/power_trace/entry.rb
87
+ - lib/power_trace/exception_patch.rb
88
+ - lib/power_trace/helpers/colorize_helper.rb
89
+ - lib/power_trace/stack.rb
90
+ - lib/power_trace/version.rb
91
+ - power_trace.gemspec
92
+ homepage: https://github.com/st0012/power_trace
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ homepage_uri: https://github.com/st0012/power_trace
97
+ source_code_uri: https://github.com/st0012/power_trace
98
+ changelog_uri: https://github.com/st0012/power_trace/CHANGELOG.md
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 2.6.0
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: power_trace provides you a more powerful backtrace.
118
+ test_files: []