debug-tracer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +20 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +168 -0
- data/LICENSE +21 -0
- data/README.md +162 -0
- data/Rakefile +12 -0
- data/debug_tracer.gemspec +33 -0
- data/lib/debug_tracer/configuration.rb +45 -0
- data/lib/debug_tracer/logger.rb +58 -0
- data/lib/debug_tracer/tracer.rb +26 -0
- data/lib/debug_tracer/version.rb +5 -0
- data/lib/debug_tracer.rb +51 -0
- data/lib/generators/debug_tracer/install/install_generator.rb +23 -0
- data/lib/generators/debug_tracer/install/templates/README +23 -0
- data/lib/generators/debug_tracer/install/templates/debug-tracer.yml +8 -0
- data/lib/generators/debug_tracer/install/templates/debug_tracer.rb +17 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c9db6b5ae70d25fdfb513f6ccf66699737d1d43ad49fb29050a0a61abc1893cb
|
4
|
+
data.tar.gz: bebf733e698c4acd8b184d3ac80434cc86bc30a56698f14fd07631139fbaafc9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e6782768e809df0ecf41240414e6f92cd5645673704380291527f61b5186a16c40b53f5f3a04ebb14442a014f124aaa6034eed002b269aed2677ad568c0842a
|
7
|
+
data.tar.gz: 146b5c91598b0b178a70691bc66e009dfa05f4c0e0276ee00bff6ecb57930f174cf1c46759f31bb793969795561c2920cb1540410222225c3c25462c4f1e606b
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.1.0] - 2024-01-XX
|
11
|
+
|
12
|
+
### Added
|
13
|
+
- Initial release
|
14
|
+
- Basic debug tracing functionality with `dt` method
|
15
|
+
- Automatic binding capture using caller_locations
|
16
|
+
- YAML configuration support
|
17
|
+
- File and stdout output options
|
18
|
+
- Configurable display options for file path, line number, and method name
|
19
|
+
- Thread-safe implementation
|
20
|
+
- Rails generator for automatic setup (`rails generate debug_tracer:install`)
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
debug-tracer (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (8.0.3)
|
10
|
+
actionview (= 8.0.3)
|
11
|
+
activesupport (= 8.0.3)
|
12
|
+
nokogiri (>= 1.8.5)
|
13
|
+
rack (>= 2.2.4)
|
14
|
+
rack-session (>= 1.0.1)
|
15
|
+
rack-test (>= 0.6.3)
|
16
|
+
rails-dom-testing (~> 2.2)
|
17
|
+
rails-html-sanitizer (~> 1.6)
|
18
|
+
useragent (~> 0.16)
|
19
|
+
actionview (8.0.3)
|
20
|
+
activesupport (= 8.0.3)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubi (~> 1.11)
|
23
|
+
rails-dom-testing (~> 2.2)
|
24
|
+
rails-html-sanitizer (~> 1.6)
|
25
|
+
activesupport (8.0.3)
|
26
|
+
base64
|
27
|
+
benchmark (>= 0.3)
|
28
|
+
bigdecimal
|
29
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
30
|
+
connection_pool (>= 2.2.5)
|
31
|
+
drb
|
32
|
+
i18n (>= 1.6, < 2)
|
33
|
+
logger (>= 1.4.2)
|
34
|
+
minitest (>= 5.1)
|
35
|
+
securerandom (>= 0.3)
|
36
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
37
|
+
uri (>= 0.13.1)
|
38
|
+
ast (2.4.3)
|
39
|
+
base64 (0.3.0)
|
40
|
+
benchmark (0.4.1)
|
41
|
+
bigdecimal (3.3.1)
|
42
|
+
builder (3.3.0)
|
43
|
+
concurrent-ruby (1.3.5)
|
44
|
+
connection_pool (2.5.4)
|
45
|
+
crass (1.0.6)
|
46
|
+
date (3.4.1)
|
47
|
+
diff-lcs (1.6.2)
|
48
|
+
drb (2.2.3)
|
49
|
+
erb (5.1.1)
|
50
|
+
erubi (1.13.1)
|
51
|
+
generator_spec (0.9.5)
|
52
|
+
activesupport (>= 3.0.0)
|
53
|
+
railties (>= 3.0.0)
|
54
|
+
i18n (1.14.7)
|
55
|
+
concurrent-ruby (~> 1.0)
|
56
|
+
io-console (0.8.1)
|
57
|
+
irb (1.15.2)
|
58
|
+
pp (>= 0.6.0)
|
59
|
+
rdoc (>= 4.0.0)
|
60
|
+
reline (>= 0.4.2)
|
61
|
+
json (2.15.1)
|
62
|
+
language_server-protocol (3.17.0.5)
|
63
|
+
lint_roller (1.1.0)
|
64
|
+
logger (1.7.0)
|
65
|
+
loofah (2.24.1)
|
66
|
+
crass (~> 1.0.2)
|
67
|
+
nokogiri (>= 1.12.0)
|
68
|
+
minitest (5.26.0)
|
69
|
+
nokogiri (1.18.10-arm64-darwin)
|
70
|
+
racc (~> 1.4)
|
71
|
+
parallel (1.27.0)
|
72
|
+
parser (3.3.9.0)
|
73
|
+
ast (~> 2.4.1)
|
74
|
+
racc
|
75
|
+
pp (0.6.3)
|
76
|
+
prettyprint
|
77
|
+
prettyprint (0.2.0)
|
78
|
+
prism (1.5.2)
|
79
|
+
psych (5.2.6)
|
80
|
+
date
|
81
|
+
stringio
|
82
|
+
racc (1.8.1)
|
83
|
+
rack (3.2.3)
|
84
|
+
rack-session (2.1.1)
|
85
|
+
base64 (>= 0.1.0)
|
86
|
+
rack (>= 3.0.0)
|
87
|
+
rack-test (2.2.0)
|
88
|
+
rack (>= 1.3)
|
89
|
+
rackup (2.2.1)
|
90
|
+
rack (>= 3)
|
91
|
+
rails-dom-testing (2.3.0)
|
92
|
+
activesupport (>= 5.0.0)
|
93
|
+
minitest
|
94
|
+
nokogiri (>= 1.6)
|
95
|
+
rails-html-sanitizer (1.6.2)
|
96
|
+
loofah (~> 2.21)
|
97
|
+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
|
98
|
+
railties (8.0.3)
|
99
|
+
actionpack (= 8.0.3)
|
100
|
+
activesupport (= 8.0.3)
|
101
|
+
irb (~> 1.13)
|
102
|
+
rackup (>= 1.0.0)
|
103
|
+
rake (>= 12.2)
|
104
|
+
thor (~> 1.0, >= 1.2.2)
|
105
|
+
tsort (>= 0.2)
|
106
|
+
zeitwerk (~> 2.6)
|
107
|
+
rainbow (3.1.1)
|
108
|
+
rake (13.3.0)
|
109
|
+
rdoc (6.15.0)
|
110
|
+
erb
|
111
|
+
psych (>= 4.0.0)
|
112
|
+
tsort
|
113
|
+
regexp_parser (2.11.3)
|
114
|
+
reline (0.6.2)
|
115
|
+
io-console (~> 0.5)
|
116
|
+
rspec (3.13.1)
|
117
|
+
rspec-core (~> 3.13.0)
|
118
|
+
rspec-expectations (~> 3.13.0)
|
119
|
+
rspec-mocks (~> 3.13.0)
|
120
|
+
rspec-core (3.13.5)
|
121
|
+
rspec-support (~> 3.13.0)
|
122
|
+
rspec-expectations (3.13.5)
|
123
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
124
|
+
rspec-support (~> 3.13.0)
|
125
|
+
rspec-mocks (3.13.5)
|
126
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
127
|
+
rspec-support (~> 3.13.0)
|
128
|
+
rspec-support (3.13.6)
|
129
|
+
rubocop (1.81.1)
|
130
|
+
json (~> 2.3)
|
131
|
+
language_server-protocol (~> 3.17.0.2)
|
132
|
+
lint_roller (~> 1.1.0)
|
133
|
+
parallel (~> 1.10)
|
134
|
+
parser (>= 3.3.0.2)
|
135
|
+
rainbow (>= 2.2.2, < 4.0)
|
136
|
+
regexp_parser (>= 2.9.3, < 3.0)
|
137
|
+
rubocop-ast (>= 1.47.1, < 2.0)
|
138
|
+
ruby-progressbar (~> 1.7)
|
139
|
+
unicode-display_width (>= 2.4.0, < 4.0)
|
140
|
+
rubocop-ast (1.47.1)
|
141
|
+
parser (>= 3.3.7.2)
|
142
|
+
prism (~> 1.4)
|
143
|
+
ruby-progressbar (1.13.0)
|
144
|
+
securerandom (0.4.1)
|
145
|
+
stringio (3.1.7)
|
146
|
+
thor (1.4.0)
|
147
|
+
tsort (0.2.0)
|
148
|
+
tzinfo (2.0.6)
|
149
|
+
concurrent-ruby (~> 1.0)
|
150
|
+
unicode-display_width (3.2.0)
|
151
|
+
unicode-emoji (~> 4.1)
|
152
|
+
unicode-emoji (4.1.0)
|
153
|
+
uri (1.0.4)
|
154
|
+
useragent (0.16.11)
|
155
|
+
zeitwerk (2.7.3)
|
156
|
+
|
157
|
+
PLATFORMS
|
158
|
+
arm64-darwin-24
|
159
|
+
|
160
|
+
DEPENDENCIES
|
161
|
+
debug-tracer!
|
162
|
+
generator_spec (~> 0.9.4)
|
163
|
+
rake (~> 13.0)
|
164
|
+
rspec (~> 3.0)
|
165
|
+
rubocop (~> 1.21)
|
166
|
+
|
167
|
+
BUNDLED WITH
|
168
|
+
2.6.7
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 PenPeen
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
# DebugTracer
|
2
|
+
|
3
|
+
A Ruby gem for debug logging that outputs variable values with execution context to assist AI-powered bug detection.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- **Automatic Context Capture**: Automatically captures execution context using caller_locations
|
8
|
+
- **Flexible Configuration**: Customize output destination and display options via YAML configuration
|
9
|
+
- **Simple API**: Output variable values instantly with `dt(binding, :var)`
|
10
|
+
- **Thread-Safe**: Safe operation in multi-threaded environments
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add to your Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'debug-tracer'
|
18
|
+
```
|
19
|
+
|
20
|
+
Then execute:
|
21
|
+
|
22
|
+
```bash
|
23
|
+
$ bundle install
|
24
|
+
```
|
25
|
+
|
26
|
+
Or install directly:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
$ gem install debug-tracer
|
30
|
+
```
|
31
|
+
|
32
|
+
### Rails Setup
|
33
|
+
|
34
|
+
For Rails applications, run the install generator:
|
35
|
+
|
36
|
+
```bash
|
37
|
+
$ bundle exec rails generate debug_tracer:install
|
38
|
+
```
|
39
|
+
|
40
|
+
This will create:
|
41
|
+
- `config/initializers/debug_tracer.rb` - Initializer with default configuration
|
42
|
+
- `config/debug-tracer.yml` - YAML configuration file
|
43
|
+
|
44
|
+
## Basic Usage
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require 'debug-tracer'
|
48
|
+
|
49
|
+
class Order
|
50
|
+
include DebugTracer
|
51
|
+
|
52
|
+
def apply_coupon(price, coupon)
|
53
|
+
dt(binding, :price, :coupon)
|
54
|
+
# Output:
|
55
|
+
# /path/to/order.rb:7 in `apply_coupon`
|
56
|
+
# => price = 1000
|
57
|
+
# => coupon = #<Coupon id: 1, discount_rate: 20>
|
58
|
+
|
59
|
+
discount = price * (coupon.discount_rate / 100.0)
|
60
|
+
dt(binding, :discount)
|
61
|
+
# => discount = 200.0
|
62
|
+
|
63
|
+
final_price = price - discount
|
64
|
+
dt(binding, :final_price)
|
65
|
+
# => final_price = 800.0
|
66
|
+
|
67
|
+
final_price
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
## Configuration
|
73
|
+
|
74
|
+
### Programmatic Configuration
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
DebugTracer.configure do |config|
|
78
|
+
config.enabled = true
|
79
|
+
config.output_target = :stdout # or :file
|
80
|
+
config.output_file_path = 'log/debug_trace.log'
|
81
|
+
config.display_file_path = true
|
82
|
+
config.display_line_number = true
|
83
|
+
config.display_method_name = true
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
### YAML Configuration File
|
88
|
+
|
89
|
+
`config/debug-tracer.yml`:
|
90
|
+
|
91
|
+
```yaml
|
92
|
+
enabled: true
|
93
|
+
output:
|
94
|
+
target: stdout # stdout or file
|
95
|
+
file_path: log/debug_trace.log
|
96
|
+
display:
|
97
|
+
file_path: true
|
98
|
+
line_number: true
|
99
|
+
method_name: true
|
100
|
+
```
|
101
|
+
|
102
|
+
Load configuration file:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
DebugTracer.load_config_file('config/debug-tracer.yml')
|
106
|
+
```
|
107
|
+
|
108
|
+
### Rails Usage
|
109
|
+
|
110
|
+
After running `rails generate debug_tracer:install`, the initializer is automatically configured:
|
111
|
+
|
112
|
+
`config/initializers/debug_tracer.rb`:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
DebugTracer.configure do |config|
|
116
|
+
config.enabled = Rails.env.development?
|
117
|
+
config.output_target = :file
|
118
|
+
config.output_file_path = Rails.root.join('log', 'debug_trace.log')
|
119
|
+
config.display_file_path = true
|
120
|
+
config.display_line_number = true
|
121
|
+
config.display_method_name = true
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
## Configuration Options
|
126
|
+
|
127
|
+
| Option | Default | Description |
|
128
|
+
|--------|---------|-------------|
|
129
|
+
| `enabled` | `true` | Enable/disable debug tracing |
|
130
|
+
| `output_target` | `:stdout` | Output destination (`:stdout` or `:file`) |
|
131
|
+
| `output_file_path` | `'log/debug_trace.log'` | File path for file output |
|
132
|
+
| `display_file_path` | `true` | Display file path |
|
133
|
+
| `display_line_number` | `true` | Display line number |
|
134
|
+
| `display_method_name` | `true` | Display method name |
|
135
|
+
|
136
|
+
## AI-Powered Bug Detection
|
137
|
+
|
138
|
+
DebugTracer's output is optimized for AI-powered bug analysis:
|
139
|
+
|
140
|
+
1. **Execution Context**: Identify problem locations with file path, line number, and method name
|
141
|
+
2. **Variable State**: Track variable values at each point in time
|
142
|
+
3. **Structured Output**: Consistent format that's easy for AI to parse
|
143
|
+
|
144
|
+
## Troubleshooting
|
145
|
+
|
146
|
+
### Variables showing as (undefined)
|
147
|
+
|
148
|
+
- Check the spelling of variable names
|
149
|
+
- Ensure dt is called within the scope of local variables
|
150
|
+
|
151
|
+
### File output not working
|
152
|
+
|
153
|
+
- Check write permissions for the output directory
|
154
|
+
- Verify the file path is correctly configured
|
155
|
+
|
156
|
+
## Development
|
157
|
+
|
158
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/PenPeen/debug-tracer).
|
159
|
+
|
160
|
+
## License
|
161
|
+
|
162
|
+
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/debug_tracer/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "debug-tracer"
|
7
|
+
spec.version = DebugTracer::VERSION
|
8
|
+
spec.authors = ["PenPeen"]
|
9
|
+
spec.email = ["ikegami1022.h@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Debug logging utility for Ruby with execution context"
|
12
|
+
spec.description = "A Ruby gem that provides debug logging with automatic binding capture using TracePoint, designed to assist AI-powered bug detection"
|
13
|
+
spec.homepage = "https://github.com/PenPeen/debug-tracer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.7.0"
|
16
|
+
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/PenPeen/debug-tracer"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/PenPeen/debug-tracer/blob/main/CHANGELOG.md"
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
20
|
+
|
21
|
+
spec.files = Dir.chdir(__dir__) do
|
22
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
23
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module DebugTracer
|
6
|
+
class Configuration
|
7
|
+
attr_accessor :enabled, :output_target, :output_file_path,
|
8
|
+
:display_file_path, :display_line_number, :display_method_name
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@enabled = true
|
12
|
+
@output_target = :stdout
|
13
|
+
@output_file_path = 'log/debug_trace.log'
|
14
|
+
@display_file_path = true
|
15
|
+
@display_line_number = true
|
16
|
+
@display_method_name = true
|
17
|
+
end
|
18
|
+
|
19
|
+
def load_from_file(file_path)
|
20
|
+
return unless File.exist?(file_path)
|
21
|
+
|
22
|
+
config = YAML.safe_load_file(file_path, permitted_classes: [], permitted_symbols: [], aliases: true)
|
23
|
+
return unless config.is_a?(Hash)
|
24
|
+
|
25
|
+
@enabled = config['enabled'] if config.key?('enabled')
|
26
|
+
|
27
|
+
if config['output']
|
28
|
+
@output_target = config['output']['target']&.to_sym || @output_target
|
29
|
+
@output_file_path = config['output']['file_path'] || @output_file_path
|
30
|
+
end
|
31
|
+
|
32
|
+
if config['display']
|
33
|
+
@display_file_path = config['display']['file_path'] if config['display'].key?('file_path')
|
34
|
+
@display_line_number = config['display']['line_number'] if config['display'].key?('line_number')
|
35
|
+
@display_method_name = config['display']['method_name'] if config['display'].key?('method_name')
|
36
|
+
end
|
37
|
+
rescue Psych::SyntaxError
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def output_to_file?
|
42
|
+
@output_target == :file
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module DebugTracer
|
6
|
+
class Logger
|
7
|
+
def initialize(configuration)
|
8
|
+
@config = configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def log(context, variables)
|
12
|
+
return unless @config.enabled
|
13
|
+
|
14
|
+
output = format_output(context, variables)
|
15
|
+
write_output(output)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def format_output(context, variables)
|
21
|
+
lines = []
|
22
|
+
|
23
|
+
if context_header_needed?
|
24
|
+
header_parts = []
|
25
|
+
header_parts << context[:file_path] if @config.display_file_path && context[:file_path]
|
26
|
+
header_parts << ":#{context[:line_number]}" if @config.display_line_number && context[:line_number]
|
27
|
+
header_parts << " in `#{context[:method_name]}`" if @config.display_method_name && context[:method_name]
|
28
|
+
|
29
|
+
lines << "# #{header_parts.join('')}" unless header_parts.empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
variables.each do |var_name, value|
|
33
|
+
lines << "# => #{var_name} = #{value.inspect}"
|
34
|
+
end
|
35
|
+
|
36
|
+
lines.join("\n")
|
37
|
+
end
|
38
|
+
|
39
|
+
def context_header_needed?
|
40
|
+
@config.display_file_path || @config.display_line_number || @config.display_method_name
|
41
|
+
end
|
42
|
+
|
43
|
+
def write_output(output)
|
44
|
+
if @config.output_to_file?
|
45
|
+
write_to_file(output)
|
46
|
+
else
|
47
|
+
puts output
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def write_to_file(output)
|
52
|
+
FileUtils.mkdir_p(File.dirname(@config.output_file_path))
|
53
|
+
File.open(@config.output_file_path, 'a') do |file|
|
54
|
+
file.puts output
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DebugTracer
|
4
|
+
class Tracer
|
5
|
+
def self.capture_context(caller_binding, stack_level: 2)
|
6
|
+
location = caller_locations(stack_level, 1)&.first
|
7
|
+
return {} unless location
|
8
|
+
|
9
|
+
{
|
10
|
+
binding: caller_binding,
|
11
|
+
file_path: location.path,
|
12
|
+
line_number: location.lineno,
|
13
|
+
method_name: extract_method_name(location)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
private_class_method def self.extract_method_name(location)
|
18
|
+
label = location.label
|
19
|
+
return nil if label == '<main>'
|
20
|
+
return nil if label.start_with?('block ')
|
21
|
+
|
22
|
+
label = label.split('#').last if label.include?('#')
|
23
|
+
label.to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/debug_tracer.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "debug_tracer/version"
|
4
|
+
require_relative "debug_tracer/configuration"
|
5
|
+
require_relative "debug_tracer/tracer"
|
6
|
+
require_relative "debug_tracer/logger"
|
7
|
+
|
8
|
+
module DebugTracer
|
9
|
+
class << self
|
10
|
+
attr_writer :configuration
|
11
|
+
|
12
|
+
def configuration
|
13
|
+
@configuration ||= Configuration.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield(configuration)
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_config_file(file_path = 'config/debug-tracer.yml')
|
21
|
+
configuration.load_from_file(file_path)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def dt(caller_binding, *var_names)
|
26
|
+
return unless DebugTracer.configuration.enabled
|
27
|
+
|
28
|
+
context = DebugTracer::Tracer.capture_context(caller_binding)
|
29
|
+
variables = extract_variables(caller_binding, var_names)
|
30
|
+
|
31
|
+
logger = DebugTracer::Logger.new(DebugTracer.configuration)
|
32
|
+
logger.log(context, variables)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def extract_variables(binding_obj, var_names)
|
38
|
+
variables = {}
|
39
|
+
|
40
|
+
var_names.each do |var_name|
|
41
|
+
begin
|
42
|
+
value = binding_obj.local_variable_get(var_name)
|
43
|
+
variables[var_name] = value
|
44
|
+
rescue NameError
|
45
|
+
variables[var_name] = '(undefined)'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
variables
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/base'
|
4
|
+
|
5
|
+
module DebugTracer
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
def copy_initializer
|
11
|
+
template 'debug_tracer.rb', 'config/initializers/debug_tracer.rb'
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_config
|
15
|
+
template 'debug-tracer.yml', 'config/debug-tracer.yml'
|
16
|
+
end
|
17
|
+
|
18
|
+
def show_readme
|
19
|
+
readme 'README' if behavior == :invoke
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
===============================================================================
|
2
|
+
|
3
|
+
DebugTracer has been installed!
|
4
|
+
|
5
|
+
The following files have been created:
|
6
|
+
|
7
|
+
config/initializers/debug_tracer.rb
|
8
|
+
config/debug-tracer.yml
|
9
|
+
|
10
|
+
You can now use DebugTracer in your Rails application:
|
11
|
+
|
12
|
+
class YourController < ApplicationController
|
13
|
+
include DebugTracer
|
14
|
+
|
15
|
+
def your_action
|
16
|
+
user_id = params[:id]
|
17
|
+
dt(binding, :user_id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
For more information, visit: https://github.com/PenPeen/debug-tracer
|
22
|
+
|
23
|
+
===============================================================================
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
DebugTracer.configure do |config|
|
4
|
+
# Enable debug tracing in development environment only
|
5
|
+
config.enabled = Rails.env.development?
|
6
|
+
|
7
|
+
# Output destination: :stdout or :file
|
8
|
+
config.output_target = :file
|
9
|
+
|
10
|
+
# File path for file output
|
11
|
+
config.output_file_path = Rails.root.join('log', 'debug_trace.log')
|
12
|
+
|
13
|
+
# Display options
|
14
|
+
config.display_file_path = true
|
15
|
+
config.display_line_number = true
|
16
|
+
config.display_method_name = true
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: debug-tracer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- PenPeen
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: rspec
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '3.0'
|
19
|
+
type: :development
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '3.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '13.0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '13.0'
|
40
|
+
description: A Ruby gem that provides debug logging with automatic binding capture
|
41
|
+
using TracePoint, designed to assist AI-powered bug detection
|
42
|
+
email:
|
43
|
+
- ikegami1022.h@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- CHANGELOG.md
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- debug_tracer.gemspec
|
55
|
+
- lib/debug_tracer.rb
|
56
|
+
- lib/debug_tracer/configuration.rb
|
57
|
+
- lib/debug_tracer/logger.rb
|
58
|
+
- lib/debug_tracer/tracer.rb
|
59
|
+
- lib/debug_tracer/version.rb
|
60
|
+
- lib/generators/debug_tracer/install/install_generator.rb
|
61
|
+
- lib/generators/debug_tracer/install/templates/README
|
62
|
+
- lib/generators/debug_tracer/install/templates/debug-tracer.yml
|
63
|
+
- lib/generators/debug_tracer/install/templates/debug_tracer.rb
|
64
|
+
homepage: https://github.com/PenPeen/debug-tracer
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata:
|
68
|
+
source_code_uri: https://github.com/PenPeen/debug-tracer
|
69
|
+
changelog_uri: https://github.com/PenPeen/debug-tracer/blob/main/CHANGELOG.md
|
70
|
+
rubygems_mfa_required: 'true'
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.7.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.6.7
|
86
|
+
specification_version: 4
|
87
|
+
summary: Debug logging utility for Ruby with execution context
|
88
|
+
test_files: []
|