pretty_backtrace 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3971218f377ecdef54fa8b356ccc3c02ac4335cb
4
- data.tar.gz: 04f0c5fa5a805cbbb23292f6068af5a3ca6a4b28
3
+ metadata.gz: 19a8c64399c2b093fa2ac3a14550b07def30fe4a
4
+ data.tar.gz: cfd978435567433f61bb149594b1b519104cdba2
5
5
  SHA512:
6
- metadata.gz: 66899e9934ad1a5e2cd125483146a6e33b7702e632f4aa7aeeb34e25ec91b16081f1bb9bfe68e738ca710e022f782ced1de6100deb89b0053c5930ce3f8b1148
7
- data.tar.gz: 315156aab1dc90a8c2438550c058df9b990c5bb79b6f60e556d393470651076ac35c0be4e53f800ba41a0f9b6f93e25b980385a38d8dec744c51d321295d10ac
6
+ metadata.gz: b67be67115eea63f4e58a5d5222c7ad4fdf0579a576485459ed0b3f6a20443c825c8bf944ccd1fee93bd2e370b29dd7bcc54011c75f277c617308bd040ab3a01
7
+ data.tar.gz: 37f4d2e3d9037dd8284554015a5a8b3d07d86d23179490bffa2052f73bfb2e08d4a8ef1caf915810cf2f2d66ce6ee1e2da66eeb702aacbcbb331398d1d6e4af1
data/README.md CHANGED
@@ -57,27 +57,59 @@ PrettyBacktrace::CONFIG can change behaviour. See source code files for details.
57
57
  You can use multi-line mode with the following configuration.
58
58
 
59
59
  ```ruby
60
- PrettyBacktrace::CONFIG[:multi_line] = true
60
+ PrettyBacktrace.multi_line = true
61
61
  ```
62
62
 
63
- Multi-line mode enable to show all variables (and pointing values) in each lines like that:
63
+ Multi-line mode enable to show file contents and all variables (and pointing values) in each lines like that:
64
64
 
65
65
  ```
66
66
  test.rb:11:in `recursive'
67
- n = 0
68
- str = "Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0...
67
+ [FILE]
68
+ 9| recursive n - 1
69
+ 10| else
70
+ -> 11| raise "bottom of recursive"
71
+ 12| end
72
+ 13|end
73
+
74
+ [LOCAL VARIABLES]
75
+ n = 0
76
+ str = "Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0!! Hi 0...
69
77
  : bottom of recursive (RuntimeError)
70
78
  from test.rb:9:in `recursive'
71
- n = 1
72
- str = "Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1...
79
+ [FILE]
80
+ 7| str = "Hi #{n}!! " * 128
81
+ 8| if n > 0
82
+ -> 9| recursive n - 1
83
+ 10| else
84
+ 11| raise "bottom of recursive"
85
+
86
+ [LOCAL VARIABLES]
87
+ n = 1
88
+ str = "Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1!! Hi 1...
73
89
 
74
90
  from test.rb:9:in `recursive'
75
- n = 2
76
- str = "Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2...
91
+ [FILE]
92
+ 7| str = "Hi #{n}!! " * 128
93
+ 8| if n > 0
94
+ -> 9| recursive n - 1
95
+ 10| else
96
+ 11| raise "bottom of recursive"
97
+
98
+ [LOCAL VARIABLES]
99
+ n = 2
100
+ str = "Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2!! Hi 2...
77
101
 
78
102
  from test.rb:9:in `recursive'
79
- n = 3
80
- str = "Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3...
103
+ [FILE]
104
+ 7| str = "Hi #{n}!! " * 128
105
+ 8| if n > 0
106
+ -> 9| recursive n - 1
107
+ 10| else
108
+ 11| raise "bottom of recursive"
109
+
110
+ [LOCAL VARIABLES]
111
+ n = 3
112
+ str = "Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3!! Hi 3...
81
113
 
82
114
  from test.rb:15:in `<main>'
83
115
  ```
@@ -5,6 +5,10 @@ module PrettyBacktrace
5
5
  CONFIG = {
6
6
  truncate_length: 20,
7
7
  disabled_exception_classes: {},
8
+ effective_lines: 0, # 0 is infinite
9
+ file_contents: true,
10
+ file_contents_lines: 2,
11
+
8
12
  multi_line: false,
9
13
  multi_line_truncate_length: 60,
10
14
  multi_line_indent: 10,
@@ -20,8 +24,13 @@ module PrettyBacktrace
20
24
 
21
25
  RubyVM::DebugInspector.open{|dc|
22
26
  locs = dc.backtrace_locations
27
+
28
+ effective_lines = CONFIG[:effective_lines]
29
+ effective_lines = locs.size - 2 if effective_lines == 0
30
+
23
31
  pretty_backtrace = locs.map.with_index{|loc, i|
24
32
  next if i < 2
33
+ next loc.to_s unless (effective_lines -= 1) >= 0
25
34
 
26
35
  iseq = dc.frame_iseq(i)
27
36
 
@@ -36,7 +45,7 @@ module PrettyBacktrace
36
45
  lvs_val = {}
37
46
  end
38
47
 
39
- modify_trace_line loc, lvs_val
48
+ modify_trace_line loc, loc.absolute_path, loc.lineno, lvs_val
40
49
  }.compact
41
50
  e.set_backtrace pretty_backtrace
42
51
  }
@@ -47,26 +56,6 @@ module PrettyBacktrace
47
56
  end
48
57
  }
49
58
 
50
- def self.enable
51
- if block_given?
52
- EXCEPTION_MODIFIER_TRACE.enable{
53
- yield
54
- }
55
- else
56
- EXCEPTION_MODIFIER_TRACE.enable
57
- end
58
- end
59
-
60
- def self.disable
61
- if block_given?
62
- EXCEPTION_MODIFIER_TRACE.disable{
63
- yield
64
- }
65
- else
66
- EXCEPTION_MODIFIER_TRACE.disable
67
- end
68
- end
69
-
70
59
  def self.iseq_local_variables iseq
71
60
  _,_,_,_,arg_info,name,path,a_path,_,type,lvs, * = iseq.to_a
72
61
  lvs
@@ -76,16 +65,40 @@ module PrettyBacktrace
76
65
  # local_variables_values is a Hash object containing pairs of
77
66
  # a local variable name and value of local variable.
78
67
  #
79
- def self.modify_trace_line backtrace_location, local_variables_values
68
+ def self.modify_trace_line backtrace_location, absolute_path, lineno, local_variables_values
80
69
  trace_line = backtrace_location.to_s
81
70
 
82
71
  unless local_variables_values.empty?
83
72
  if CONFIG[:multi_line]
84
- additional = local_variables_values.map{|lv, v|
85
- v = v[0..CONFIG[:multi_line_truncate_length]] + '...' if v.length > CONFIG[:multi_line_truncate_length]
86
- ' ' * CONFIG[:multi_line_indent] + "#{lv} = #{v.to_s}"
87
- }.join("\n") + "\n"
88
- trace_line = "#{trace_line}\n#{additional}"
73
+ indent = ' ' * CONFIG[:multi_line_indent]
74
+ additional = ''
75
+
76
+ # file scope
77
+ if CONFIG[:file_contents] && File.exists?(absolute_path)
78
+ fclines = CONFIG[:file_contents_lines]
79
+ start_line = lineno - 1 - 1 * fclines
80
+ start_line = 0 if start_line < 0
81
+
82
+ additional << indent + "[FILE]\n"
83
+ additional << open(absolute_path){|f| f.readlines[start_line, 1 + 2 * fclines]}.map.with_index{|line, i|
84
+ ln = start_line + i + 1
85
+ line = line.chomp
86
+ '%s%4d|%s' % [ln == lineno ? indent[0..-3] + "->" : indent, ln, line]
87
+ }.join("\n")
88
+ additional << "\n"
89
+ additional << "\n"
90
+ end
91
+
92
+ # local variables
93
+ unless local_variables_values.empty?
94
+ additional << indent + "[LOCAL VARIABLES]\n"
95
+ additional << local_variables_values.map{|lv, v|
96
+ v = v[0..CONFIG[:multi_line_truncate_length]] + '...' if v.length > CONFIG[:multi_line_truncate_length]
97
+ indent + " #{lv} = #{v.to_s}"
98
+ }.join("\n") + "\n"
99
+ end
100
+
101
+ trace_line = "#{trace_line}\n#{additional}" unless additional.empty?
89
102
  else
90
103
  additional = local_variables_values.map{|lv, v|
91
104
  v = v[0..CONFIG[:truncate_length]] + '...' if v.length > CONFIG[:truncate_length]
@@ -97,4 +110,38 @@ module PrettyBacktrace
97
110
 
98
111
  trace_line
99
112
  end
113
+
114
+ # configuration
115
+
116
+ def self.enable
117
+ if block_given?
118
+ EXCEPTION_MODIFIER_TRACE.enable{
119
+ yield
120
+ }
121
+ else
122
+ EXCEPTION_MODIFIER_TRACE.enable
123
+ end
124
+ end
125
+
126
+ def self.disable
127
+ if block_given?
128
+ EXCEPTION_MODIFIER_TRACE.disable{
129
+ yield
130
+ }
131
+ else
132
+ EXCEPTION_MODIFIER_TRACE.disable
133
+ end
134
+ end
135
+
136
+ def self.multi_line=(setting)
137
+ CONFIG[:multi_line] = setting
138
+ end
139
+
140
+ def self.file_contents=(setting)
141
+ CONFIG[:file_contents] = setting
142
+ end
143
+
144
+ def self.effective_lines=(lines)
145
+ CONFIG[:effective_lines] = lines
146
+ end
100
147
  end
@@ -1,3 +1,3 @@
1
1
  module PrettyBacktrace
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $: << './lib'
2
2
  require 'pretty_backtrace/enable'
3
3
 
4
- # PrettyBacktrace::CONFIG[:multi_line] = true
4
+ PrettyBacktrace.multi_line = true
5
5
 
6
6
  def recursive n
7
7
  str = "Hi #{n}!! " * 128
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_backtrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koichi Sasada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-02 00:00:00.000000000 Z
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debug_inspector