pretty_trace 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -6
- data/lib/pretty_trace/backtrace_item.rb +34 -0
- data/lib/pretty_trace/colors.rb +11 -0
- data/lib/pretty_trace/handler.rb +36 -8
- data/lib/pretty_trace/module_methods.rb +4 -4
- data/lib/pretty_trace/structured_backtrace.rb +44 -0
- data/lib/pretty_trace/version.rb +1 -1
- data/lib/pretty_trace.rb +3 -1
- metadata +5 -3
- data/lib/pretty_trace/formatter.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e47618bf9096072c8371be60df52dd3fe429a184
|
4
|
+
data.tar.gz: 8f3b516b840401db8c357a3d0ba953bee0340efa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b68ad25758b5553c56d9378f33bdfb0fe80ab0fc10e3c90092145a9bc134276169da802b6855bb8de5ac93a834e352e7060bd53c90e1fb3c51e50a0cda1cbfa7
|
7
|
+
data.tar.gz: 7a9315f338a691b7e61d8e1e55288942571ad1c8f8dd086799984ed69239d3ef2f738864a1785508c4898030d211f966c391013451303e481628e71f992ed037
|
data/README.md
CHANGED
@@ -24,13 +24,13 @@ Or with bundler:
|
|
24
24
|
|
25
25
|
```
|
26
26
|
# Just install, do not activate
|
27
|
-
|
27
|
+
gem 'pretty_trace'
|
28
28
|
|
29
|
-
#
|
30
|
-
|
29
|
+
# Or, install and enable
|
30
|
+
gem 'pretty_trace', require: 'pretty_trace/enable'
|
31
31
|
|
32
|
-
#
|
33
|
-
|
32
|
+
# Or, install, enable and enable trimming
|
33
|
+
gem 'pretty_trace', require: 'pretty_trace/enable-trim'
|
34
34
|
```
|
35
35
|
|
36
36
|
Example
|
@@ -69,7 +69,7 @@ last line), require this script instead:
|
|
69
69
|
require 'pretty_trace/enable-trim'
|
70
70
|
```
|
71
71
|
|
72
|
-
If you prefer to have more control,you can use configure these settings
|
72
|
+
If you prefer to have more control, you can use configure these settings
|
73
73
|
manually:
|
74
74
|
|
75
75
|
```ruby
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module PrettyTrace
|
2
|
+
class BacktraceItem
|
3
|
+
include Colors
|
4
|
+
|
5
|
+
attr_reader :original_line, :path, :file, :line, :dir, :method, :full_dir
|
6
|
+
|
7
|
+
def initialize(original_line)
|
8
|
+
@original_line = original_line
|
9
|
+
@path, @file, @line, @dir, @full_dir = nil, nil, nil, nil, nil
|
10
|
+
@formatted = false
|
11
|
+
|
12
|
+
if @original_line =~ /(.+):(-?\d+):in `(.+)'/
|
13
|
+
@formatted = true
|
14
|
+
@path, @line, @method = $1, $2, $3
|
15
|
+
@full_dir = File.dirname(@path)
|
16
|
+
@dir = @full_dir.split('/').last
|
17
|
+
@dir = @dir == '.' ? '' : "#{dir}/"
|
18
|
+
@file = File.basename @path
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def formatted?
|
23
|
+
@formatted
|
24
|
+
end
|
25
|
+
|
26
|
+
def formatted_line
|
27
|
+
formatted? ? colored_line : original_line
|
28
|
+
end
|
29
|
+
|
30
|
+
def colored_line
|
31
|
+
"line %{green}#{line.to_s.ljust 4}%{reset} in %{cyan}#{dir}%{magenta}#{file}%{reset} > %{blue}#{method}%{reset}" % colors
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/pretty_trace/handler.rb
CHANGED
@@ -3,9 +3,26 @@ require 'singleton'
|
|
3
3
|
module PrettyTrace
|
4
4
|
class Handler
|
5
5
|
include Singleton
|
6
|
+
include Colors
|
6
7
|
|
7
|
-
def
|
8
|
-
@
|
8
|
+
def enable
|
9
|
+
@enabled = true
|
10
|
+
# :nocov: - this is actually covered through an external process
|
11
|
+
at_exit do
|
12
|
+
if @enabled and $! and !ignored.include? $!.class
|
13
|
+
show_errors $!
|
14
|
+
exit! 1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
# :nocov:
|
18
|
+
end
|
19
|
+
|
20
|
+
def disable
|
21
|
+
@enabled = false
|
22
|
+
end
|
23
|
+
|
24
|
+
def enabled?
|
25
|
+
@enabled
|
9
26
|
end
|
10
27
|
|
11
28
|
def options
|
@@ -18,13 +35,24 @@ module PrettyTrace
|
|
18
35
|
|
19
36
|
private
|
20
37
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
38
|
+
def ignored
|
39
|
+
# :nocov:
|
40
|
+
[ SystemExit ]
|
41
|
+
# :nocov:
|
42
|
+
end
|
43
|
+
|
44
|
+
def show_errors(exception)
|
45
|
+
# :nocov:
|
46
|
+
backtrace = StructuredBacktrace.new exception.backtrace, options
|
47
|
+
puts "\n#{backtrace}"
|
48
|
+
message = exception.message
|
49
|
+
if message.empty?
|
50
|
+
puts "\n%{blue}#{exception.class}%{reset}\n" % colors
|
51
|
+
else
|
52
|
+
puts "\n%{blue}#{exception.class}\n%{red}#{message}%{reset}\n" % colors
|
27
53
|
end
|
54
|
+
STDOUT.flush
|
55
|
+
# :nocov:
|
28
56
|
end
|
29
57
|
|
30
58
|
def default_options
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module PrettyTrace
|
2
2
|
def self.enable
|
3
|
-
Handler.instance.
|
3
|
+
Handler.instance.enable unless ENV['PRETTY_TRACE'] == 'off'
|
4
4
|
end
|
5
5
|
|
6
6
|
def self.disable
|
7
|
-
Handler.instance.
|
7
|
+
Handler.instance.disable
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.filter(filter)
|
@@ -16,10 +16,10 @@ module PrettyTrace
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def self.trim
|
19
|
-
|
19
|
+
Handler.instance.options[:trim] = true
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.no_trim
|
23
|
-
|
23
|
+
Handler.instance.options[:trim] = false
|
24
24
|
end
|
25
25
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module PrettyTrace
|
2
|
+
class StructuredBacktrace
|
3
|
+
attr_reader :options, :backtrace
|
4
|
+
|
5
|
+
def initialize(backtrace, options={})
|
6
|
+
@options = options
|
7
|
+
@backtrace = backtrace
|
8
|
+
end
|
9
|
+
|
10
|
+
def structure
|
11
|
+
filter = options[:filter] || []
|
12
|
+
filter = [filter] unless filter.is_a? Array
|
13
|
+
|
14
|
+
result = backtrace.dup
|
15
|
+
|
16
|
+
filter.each do |expression|
|
17
|
+
result.reject! { |trace| trace =~ expression }
|
18
|
+
end
|
19
|
+
|
20
|
+
result.map! { |line| BacktraceItem.new line }
|
21
|
+
first_line = result[0]
|
22
|
+
result.reverse!
|
23
|
+
result.uniq!(&:path) if should_trim? result
|
24
|
+
|
25
|
+
result.push first_line unless first_line.original_line == result[-1].original_line
|
26
|
+
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def formatted_backtrace
|
31
|
+
structure.map(&:formatted_line)
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
formatted_backtrace.join "\n"
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def should_trim?(backtrace)
|
41
|
+
options[:trim] and ENV['PRETTY_TRACE'] != 'full' and backtrace.size > 3
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/pretty_trace/version.rb
CHANGED
data/lib/pretty_trace.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretty_trace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: runfile
|
@@ -88,11 +88,13 @@ extra_rdoc_files: []
|
|
88
88
|
files:
|
89
89
|
- README.md
|
90
90
|
- lib/pretty_trace.rb
|
91
|
+
- lib/pretty_trace/backtrace_item.rb
|
92
|
+
- lib/pretty_trace/colors.rb
|
91
93
|
- lib/pretty_trace/enable-trim.rb
|
92
94
|
- lib/pretty_trace/enable.rb
|
93
|
-
- lib/pretty_trace/formatter.rb
|
94
95
|
- lib/pretty_trace/handler.rb
|
95
96
|
- lib/pretty_trace/module_methods.rb
|
97
|
+
- lib/pretty_trace/structured_backtrace.rb
|
96
98
|
- lib/pretty_trace/version.rb
|
97
99
|
homepage: https://github.com/DannyBen/pretty_trace
|
98
100
|
licenses:
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module PrettyTrace
|
2
|
-
class Formatter
|
3
|
-
def self.pretty_trace(backtrace, opts={})
|
4
|
-
filter = opts[:filter] || []
|
5
|
-
filter = [filter] unless filter.is_a? Array
|
6
|
-
|
7
|
-
filter.each do |expression|
|
8
|
-
backtrace.reject! { |trace| trace =~ expression }
|
9
|
-
end
|
10
|
-
|
11
|
-
backtrace = trim backtrace if should_trim? backtrace
|
12
|
-
|
13
|
-
backtrace.map! do |item|
|
14
|
-
if item =~ /(.+):(-?\d+):in `(.+)'/
|
15
|
-
file, line, method = $1, $2, $3
|
16
|
-
dir = File.dirname(file).split('/').last
|
17
|
-
dir = dir == '.' ? '' : "#{dir}/"
|
18
|
-
file = File.basename file
|
19
|
-
|
20
|
-
item = "line %{green}#{line.to_s.ljust 4}%{reset} in %{cyan}#{dir}%{magenta}#{file}%{reset} > %{blue}#{method}%{reset}" % colors
|
21
|
-
end
|
22
|
-
item
|
23
|
-
end
|
24
|
-
|
25
|
-
backtrace
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.colors
|
29
|
-
{
|
30
|
-
reset: "\e[0m",
|
31
|
-
black: "\e[30m",
|
32
|
-
red: "\e[31m",
|
33
|
-
green: "\e[32m",
|
34
|
-
yellow: "\e[33m",
|
35
|
-
blue: "\e[34m",
|
36
|
-
magenta:"\e[35m",
|
37
|
-
cyan: "\e[36m",
|
38
|
-
white: "\e[37m",
|
39
|
-
}
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.trim(backtrace)
|
43
|
-
[backtrace[0], '...... (trimmed)', backtrace[-1]]
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.should_trim?(backtrace)
|
47
|
-
ENV['PRETTY_TRACE_TRIM'] and ENV['PRETTY_TRACE'] != 'full' and
|
48
|
-
backtrace.size > 3
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|