source_route 0.0.4 → 0.0.5

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: 53b09f104ea2720d70ee2312d8233aeefbc5ea6c
4
- data.tar.gz: 33f9ec189a6d23df94cd85bd27da378561ebda00
3
+ metadata.gz: ca12ebcf942682b151bf55b7e80c01b064fe5b04
4
+ data.tar.gz: f7959a511fb7d8eb531698dacd26df7e06c32ce3
5
5
  SHA512:
6
- metadata.gz: 64e04570f8a30d20631c86003bc3d311ac15f69a0e31d1b6e080220a553c1e8ad51699745fe4be6bf4f6c82f53266248ca78117e3f0f79da60effa506afdecaf
7
- data.tar.gz: f1ba59748e527c875f5f4ac2b9a46cb3287f845f43ee62b38db9ad3cbf8f57ba89c08921c25e21ae53349fadaacd168ba0ec7d255431ac61f327483edfa1bbed
6
+ metadata.gz: 144b8d6f493591605622a78ed4ff692da65eb44670eae99ec6d566d226f798997088f14e6671440138a228622ff0dae0c2845756caeb1392aeb1df4b717f3a30
7
+ data.tar.gz: af1a760698f4ad109943c11b67fba8ef8e14da88ba116f45bf4ba70dcadd512f057c1313daa302013e46072d1a123722a821e826dac553266f6e62b62bc28a79
data/README.md CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- #### In irb or pry terminal
25
+ #### In irb or pry
26
26
 
27
27
  SourceRoute.trace output_format: :console, event: :c_call do
28
28
  'abc'.upcase
@@ -30,7 +30,7 @@ Or install it yourself as:
30
30
 
31
31
  #### In rails console
32
32
 
33
- SourceRoute.trace defined_class: 'ActiveRecord', output_format: html do
33
+ SourceRoute.trace defined_class: :ActiveRecord, output_format: :html do
34
34
  User.new
35
35
  end
36
36
 
@@ -80,3 +80,5 @@ see more usage in examples.
80
80
  Add debug option to provider more verbose messages of what has happened
81
81
 
82
82
  Support SourceRoute.enable :wanted_method_or_class, at now only SourceRoute.enable :wanted_method works
83
+
84
+ When we record both call end return event, it's better to combine them togother into one, so we can get call order from call event and also get return value from return event
@@ -6,10 +6,12 @@ module SourceRoute
6
6
  module Html
7
7
 
8
8
  # results is instance of Wrapper
9
- def self.render(results)
9
+ def self.slim_render(results)
10
10
  template_path = File.expand_path "../html_template.slim", __FILE__
11
- html_output_str = Slim::Template.new(template_path).render(results)
12
- File.open("#{Time.now.strftime('%M%S-%H-%m-%d')}-source-route.html", 'w') do |f|
11
+ slim_template = Slim::Template.new(template_path)
12
+
13
+ html_output_str = slim_template.render(results)
14
+ File.open("#{Time.now.strftime('%S%M-%H-%m')}-source-route.html", 'w') do |f|
13
15
  f << html_output_str
14
16
  end
15
17
  end
@@ -10,21 +10,22 @@ html
10
10
  / workaround for following case:
11
11
  / pry(main)> ActiveRecord::Inheritance::ClassMethods.to_json => {} # Dont know why
12
12
  ruby:
13
- trace_data = @tp_attrs_results.map do |tp_result|
13
+ local_trace_data = @tp_attrs_results.map do |tp_result|
14
14
  if tp_result.has_key?(:defined_class)
15
- tp_result[:defined_class] = tp_result[:defined_class].nature_value
15
+ tp_result[:defined_class] = tp_result[:defined_class].to_s
16
16
  end
17
17
  tp_result
18
18
  end
19
19
 
20
20
  .data-collect
21
- #trace-data data-trace="#{trace_data.to_json}"
21
+ / dont use local_trace_data.to_json, because ActiveSupport override it and can introduce unexpected crash for some data
22
+ #trace-data data-trace="#{JSON.dump(local_trace_data)}"
22
23
 
23
24
  .container(ng-controller="MainCtrl")
24
25
  .top-header
25
26
  h4
26
27
  | Event:
27
- =< @conditions.events
28
+ =< @condition.events
28
29
  .trace-flow
29
30
  .row
30
31
  .left-info.col-sm-4
@@ -33,10 +34,17 @@ html
33
34
  span(ng-bind="klass")
34
35
  .center-info.col-sm-6
35
36
  .well.well-lg(ng-repeat="trace in traces | filter:{defined_class: selectedKlass}")
36
- div(ng-click="showMoreDetail = !showMoreDetail")
37
+
38
+ .header(ng-click="showMoreDetail = !showMoreDetail")
37
39
  span(ng-bind="trace.defined_class")
38
- span #
40
+ span
41
+ | .
39
42
  span(ng-bind="trace.method_id")
43
+
44
+ span<>(ng-if="trace.return_value")
45
+ | =>
46
+ span<>(ng-if="trace.return_value" ng-bind="trace.return_value")
47
+
40
48
  .details(ng-if="showMoreDetail")
41
49
  .local-vars.well.well-sm(ng-if="trace.local_var" style="color: green")
42
50
  span Local Var
@@ -14,17 +14,22 @@ module SourceRoute
14
14
  c_return: [:defined_class, :method_id, :return_value],
15
15
  raise: [:raised_exception],
16
16
  b_call: [:binding, :defined_class, :method_id],
17
- b_return: [:binding, :defined_class, :method_id],
17
+ b_return: [:binding, :defined_class, :method_id, :return_value],
18
18
  thread_begin: [:defined_class, :method_id],
19
19
  thread_end: [:defined_class, :method_id]
20
20
  }
21
21
 
22
22
  def initialize(wrapper)
23
+ @logger = Logger.new(STDOUT)
23
24
  @wrapper = wrapper
24
25
 
25
- @output_config = @wrapper.conditions.result_config
26
+ @output_config = @wrapper.condition.result_config
26
27
 
27
- @tp_events = @wrapper.conditions.events
28
+ @tp_events = @wrapper.condition.events
29
+ if @tp_events.length > 1 and @output_config[:selected_attrs]
30
+ @logger.warn 'selected_attrs was ignored, cause watched event was more than one '
31
+ @output_config[:selected_attrs] = nil
32
+ end
28
33
  end
29
34
 
30
35
  def output_attributes(event)
@@ -56,6 +61,8 @@ module SourceRoute
56
61
  # I have to know when the application is end
57
62
  when :test, :silence
58
63
  # do nothing at now
64
+ when :stack_overflow
65
+ console_stack_overflow
59
66
  when Proc
60
67
  format.call(tp_ins)
61
68
  else
@@ -74,7 +81,7 @@ module SourceRoute
74
81
  end
75
82
 
76
83
  def collect_local_var_data
77
- if @wrapper.conditions.result_config[:include_local_var]
84
+ if @wrapper.condition.result_config[:include_local_var]
78
85
  local_var_hash = {}
79
86
 
80
87
  @tp.binding.eval('local_variables').each do |v|
@@ -87,7 +94,7 @@ module SourceRoute
87
94
  end
88
95
 
89
96
  def collect_instance_var_data
90
- if @wrapper.conditions.result_config[:include_instance_var]
97
+ if @wrapper.condition.result_config[:include_instance_var]
91
98
  instance_var_hash = {}
92
99
  @tp.self.instance_variables.each do |key|
93
100
  instance_var_hash[key] = @tp.self.instance_variable_get(key)
@@ -108,6 +115,9 @@ module SourceRoute
108
115
  ap ret
109
116
  end
110
117
 
118
+ def console_stack_overflow
119
+ ap "#{@collect_data[:defined_class].inspect}##{@collect_data[:method_id]}"
120
+ end
111
121
  end # END TpResult
112
122
 
113
123
  end
@@ -1,3 +1,3 @@
1
1
  module SourceRoute
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -3,84 +3,87 @@ module SourceRoute
3
3
  class Wrapper
4
4
  include Singleton
5
5
 
6
- attr_accessor :conditions, :tp, :tp_attrs_results
6
+ TRACE_POINT_METHODS = [:defined_class, :method_id, :path, :lineno]
7
+
8
+ attr_accessor :condition, :tp, :tp_attrs_results
7
9
  attr_accessor :output_include_local_variables, :output_include_instance_variables
8
10
 
9
- def initialize
10
- reset
11
- end
11
+ Condition = Struct.new(:events, :negative, :positive, :result_config)
12
12
 
13
- # output_format can be console, html
14
- def reset
15
- @tp.disable if @tp
16
- @conditions = OpenStruct.new(events: [:call], negative: {}, positive: {},
17
- result_config: { output_format: 'none',
18
- selected_attrs: nil,
19
- include_local_var: false,
20
- include_instance_var: false
21
- })
22
- @tp_attrs_results = []
23
- self
24
- end
13
+ class Condition
25
14
 
26
- def events(v)
27
- @conditions.events = Array(v).map(&:to_sym) unless v.nil?
28
- end
29
- alias :event :events
15
+ TRACE_POINT_METHODS.each do |m|
16
+ define_method m do |v|
17
+ positive[m] = v.to_s
18
+ end
30
19
 
31
- def set_result_config(value)
32
- unless value.is_a? Hash
33
- conditions.result_config = value
20
+ define_method "#{m}_not" do |v|
21
+ negative[m] = v.to_s
22
+ end
34
23
  end
35
- end
36
24
 
37
- def output_format(data = nil, &block)
38
- conditions.result_config[:output_format] = if block_given?
39
- block
40
- else
41
- data
42
- end
43
- end
44
-
45
- def selected_attrs(*attr)
46
- conditions.result_config[:selected_attrs] = Array(attr)
47
- end
48
-
49
- def output_include_local_variables
50
- conditions.result_config[:include_local_var] = true
51
- end
25
+ def event(*v)
26
+ # why need self? without self, the events will not really changed, why?. seems a bug in ruby
27
+ self.events = v.map(&:to_sym) unless v == []
28
+ end
52
29
 
53
- def output_include_instance_variables
54
- conditions.result_config[:include_instance_var] = true
55
- end
30
+ def output_format(data = nil, &block)
31
+ result_config[:output_format] = if block_given?
32
+ block
33
+ else
34
+ data
35
+ end
36
+ end
56
37
 
57
- TRACE_POINT_METHODS = [:defined_class, :method_id, :path, :lineno]
38
+ def selected_attrs(*attr)
39
+ result_config[:selected_attrs] = attr
40
+ end
58
41
 
59
- TRACE_POINT_METHODS.each do |m|
60
- define_method m do |v|
61
- @conditions.positive[m] = v.to_s
42
+ def output_include_local_variables
43
+ result_config[:include_local_var] = true
62
44
  end
63
45
 
64
- define_method "#{m}_not" do |v|
65
- @conditions.negative[m] = v.to_s
46
+ def output_include_instance_variables
47
+ result_config[:include_instance_var] = true
66
48
  end
49
+
50
+ end
51
+
52
+ def initialize
53
+ reset
67
54
  end
68
55
 
56
+ # output_format can be console, html
57
+ def reset
58
+ @tp.disable if @tp
59
+ @condition = Condition.new([:call], {}, {},
60
+ { output_format: 'none',
61
+ selected_attrs: nil,
62
+ include_local_var: false,
63
+ include_instance_var: false
64
+ })
65
+ @tp_attrs_results = []
66
+ self
67
+ end
68
+
69
+
69
70
  def trace
70
- # dont wanna init it in tp block, cause tp block could run thousands of time in one cycle trace
71
+ # dont wanna init it in tp block, cause tp block could run thousands of times in one cycle trace
71
72
  tp_result = TpResult.new(self)
72
73
 
73
- track = TracePoint.new *conditions.events do |tp|
74
- negative_break = conditions.negative.any? do |method_key, value|
75
- tp.send(method_key).nature_value =~ Regexp.new(value)
74
+ track = TracePoint.new *condition.events do |tp|
75
+ # todo: it's better to change the break check to condition methods to make more flexible
76
+ negative_break = condition.negative.any? do |method_key, value|
77
+ tp.send(method_key).to_s =~ Regexp.new(value)
76
78
  end
77
79
  next if negative_break
78
- positive_break = conditions.positive.any? do |method_key, value|
79
- tp.send(method_key).nature_value !~ Regexp.new(value)
80
+
81
+ positive_break = condition.positive.any? do |method_key, value|
82
+ tp.send(method_key).to_s !~ Regexp.new(value)
80
83
  end
81
84
  next if positive_break
82
85
 
83
- unless conditions[:result_config][:output_format].is_a? Proc
86
+ unless condition[:result_config][:output_format].is_a? Proc
84
87
  ret_data = tp_result.build(tp)
85
88
  tp_attrs_results.push(ret_data)
86
89
  end
data/lib/source_route.rb CHANGED
@@ -7,7 +7,7 @@ require 'awesome_print'
7
7
  require "source_route/version"
8
8
  require "source_route/wrapper"
9
9
  require "source_route/tp_result"
10
- require "source_route/nature_value"
10
+ # require "source_route/customize_to_json"
11
11
 
12
12
  module SourceRoute
13
13
  extend self
@@ -27,9 +27,9 @@ module SourceRoute
27
27
  def enable(match = nil, &block)
28
28
  wrapper.reset
29
29
 
30
- wrapper.method_id(match) if match # TODO in future future: should add as wrapper.method_id_or(match)
30
+ wrapper.condition.method_id(match) if match # TODO in future future: should add as wrapper.method_id_or(match)
31
31
 
32
- wrapper.instance_eval(&block) if block_given?
32
+ wrapper.condition.instance_eval(&block) if block_given?
33
33
 
34
34
  wrapper.trace
35
35
  end
@@ -39,7 +39,7 @@ module SourceRoute
39
39
  opt[:output_format] ||= :silence
40
40
  wrapper.reset
41
41
  opt.each do |k, v|
42
- wrapper.send(k, v)
42
+ wrapper.condition.send(k, v)
43
43
  end
44
44
  wrapper.trace
45
45
  yield
@@ -48,7 +48,8 @@ module SourceRoute
48
48
  end
49
49
 
50
50
  def build_html_output
51
- SourceRoute::Formats::Html.render(wrapper)
51
+ SourceRoute.disable
52
+ SourceRoute::Formats::Html.slim_render(wrapper)
52
53
  end
53
54
 
54
55
  # Not implement yet
data/test/wrapper_test.rb CHANGED
@@ -86,6 +86,7 @@ module SourceRoute
86
86
  SourceRoute.trace event: :c_call do
87
87
  'abc'.upcase
88
88
  end
89
+
89
90
  assert_equal 2, @wrapper.tp_attrs_results.size
90
91
  end
91
92
 
@@ -99,7 +100,7 @@ module SourceRoute
99
100
 
100
101
  def test_trace_two_events
101
102
  SourceRoute.enable 'nonsense' do
102
- events [:call, :return]
103
+ event :call, :return
103
104
  end
104
105
  SampleApp.new.nonsense
105
106
  assert_equal 2, @wrapper.tp_attrs_results.size
@@ -122,7 +123,7 @@ module SourceRoute
122
123
 
123
124
  def test_track_local_var_when_event_is_return
124
125
  SourceRoute.enable 'nonsense_with_params' do
125
- events :return
126
+ event :return
126
127
  output_include_local_variables
127
128
  end
128
129
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: source_route
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - raykin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-07 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -139,7 +139,6 @@ files:
139
139
  - lib/source_route.rb
140
140
  - lib/source_route/formats/html.rb
141
141
  - lib/source_route/formats/html_template.slim
142
- - lib/source_route/nature_value.rb
143
142
  - lib/source_route/tp_result.rb
144
143
  - lib/source_route/version.rb
145
144
  - lib/source_route/wrapper.rb
@@ -1,29 +0,0 @@
1
- class NilClass
2
- def nature_value
3
- nil
4
- end
5
- end
6
-
7
- class String
8
- def nature_value
9
- self
10
- end
11
- end
12
-
13
- class Class
14
- def nature_value
15
- self.name
16
- end
17
- end
18
-
19
- class Module
20
- def nature_value
21
- self.name
22
- end
23
- end
24
-
25
- class Symbol
26
- def nature_value
27
- to_s
28
- end
29
- end