pretty_trace 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01f4109d20baa996682e6f71ce0ea1b03d7d95ac
4
- data.tar.gz: ecb6f5346c533dc4915df2abe151c098423627da
3
+ metadata.gz: e47618bf9096072c8371be60df52dd3fe429a184
4
+ data.tar.gz: 8f3b516b840401db8c357a3d0ba953bee0340efa
5
5
  SHA512:
6
- metadata.gz: 0a4c2476428a4a7e4a29d6fcc183c0159fe5b51aed461a15a207394b6fac1cad2124a648be183e24fb4dd819272c7a8cd988e3ae63d945cc943a8d2cc10e88b4
7
- data.tar.gz: cb7c2888add66b808803af25b5b52c5ee37e51a87149df709757c5e90c56e204c302c64cea6a55ee18d1cb334e6802c417c04446f0a758b962954aaa0f08b979
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
- $ gem 'pretty_trace'
27
+ gem 'pretty_trace'
28
28
 
29
- # Install and enable
30
- $ gem 'pretty_trace', require: 'pretty_trace/enable'
29
+ # Or, install and enable
30
+ gem 'pretty_trace', require: 'pretty_trace/enable'
31
31
 
32
- # Install, enable and enable trimming
33
- $ gem 'pretty_trace', require: 'pretty_trace/enable-trim'
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
@@ -0,0 +1,11 @@
1
+ module PrettyTrace
2
+ module Colors
3
+ def colors
4
+ {
5
+ reset: "\e[0m", black: "\e[30m", red: "\e[31m",
6
+ green: "\e[32m", yellow: "\e[33m", blue: "\e[34m",
7
+ magenta:"\e[35m", cyan: "\e[36m", white: "\e[37m",
8
+ }
9
+ end
10
+ end
11
+ end
@@ -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 trace_point
8
- @trace_point ||= trace_point!
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 trace_point!
22
- TracePoint.new :raise do |tp|
23
- exception = tp.raised_exception
24
- backtrace = exception.backtrace
25
- pretty_trace = Formatter.pretty_trace backtrace, options
26
- exception.set_backtrace pretty_trace
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.trace_point.enable unless ENV['PRETTY_TRACE'] == 'off'
3
+ Handler.instance.enable unless ENV['PRETTY_TRACE'] == 'off'
4
4
  end
5
5
 
6
6
  def self.disable
7
- Handler.instance.trace_point.disable
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
- ENV['PRETTY_TRACE_TRIM'] = '1'
19
+ Handler.instance.options[:trim] = true
20
20
  end
21
21
 
22
22
  def self.no_trim
23
- ENV['PRETTY_TRACE_TRIM'] = nil
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
@@ -1,3 +1,3 @@
1
1
  module PrettyTrace
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/pretty_trace.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'pretty_trace/version'
2
+ require 'pretty_trace/colors'
3
+ require 'pretty_trace/backtrace_item'
2
4
  require 'pretty_trace/handler'
3
- require 'pretty_trace/formatter'
5
+ require 'pretty_trace/structured_backtrace'
4
6
  require 'pretty_trace/module_methods'
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.1.1
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-02 00:00:00.000000000 Z
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