trace-util-adv 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.
Files changed (44) hide show
  1. data/CHANGELOG +7 -0
  2. data/VERSION +1 -1
  3. data/lib/README +87 -0
  4. data/lib/TODO.txt +12 -0
  5. data/lib/action_handler.rb +99 -0
  6. data/lib/appenders/appender.rb +97 -0
  7. data/lib/appenders/appender_registration.rb +16 -0
  8. data/lib/appenders/base_appender.rb +82 -0
  9. data/lib/appenders/file_appender.rb +72 -0
  10. data/lib/appenders/html_appender.rb +94 -0
  11. data/lib/appenders/logger_appender.rb +84 -0
  12. data/lib/appenders/stream_appender.rb +37 -0
  13. data/lib/appenders/template_log_appender.rb +28 -0
  14. data/lib/appenders/xml_appender.rb +75 -0
  15. data/lib/core_extensions.rb +101 -0
  16. data/lib/filters/base_filters.rb +178 -0
  17. data/lib/filters/composite_filters.rb +71 -0
  18. data/lib/filters/filter_factory.rb +17 -0
  19. data/lib/filters/message_filters.rb +35 -0
  20. data/lib/filters/tracing_filter.rb +88 -0
  21. data/lib/output_templates.rb +5 -0
  22. data/lib/rule_match.rb +38 -0
  23. data/lib/sample_filters.rb +95 -0
  24. data/lib/templates/base_template.rb +21 -0
  25. data/lib/templates/html_template.rb +48 -0
  26. data/lib/templates/string_template.rb +30 -0
  27. data/lib/templates/trace_output_handler.rb +47 -0
  28. data/lib/templates/xml_template.rb +36 -0
  29. data/lib/test_action_handler.rb +34 -0
  30. data/lib/test_appender.rb +29 -0
  31. data/lib/test_file_appender.rb +32 -0
  32. data/lib/test_filters.rb +112 -0
  33. data/lib/test_filters_chain.rb +100 -0
  34. data/lib/test_filters_create.rb +28 -0
  35. data/lib/test_html_appender.rb +66 -0
  36. data/lib/test_special_filters.rb +78 -0
  37. data/lib/test_xml_appender.rb +66 -0
  38. data/lib/test_xml_gen.rb +46 -0
  39. data/lib/trace_appenders.rb +9 -0
  40. data/lib/trace_calls.rb +86 -0
  41. data/lib/trace_ext.rb +57 -0
  42. data/lib/trace_filters.rb +4 -0
  43. data/trace-util-adv.gemspec +98 -0
  44. metadata +44 -2
@@ -0,0 +1,71 @@
1
+ module Tracing
2
+ class CompositeClassFilter < BaseFilter
3
+ def initialize(options)
4
+ super(options)
5
+ @rules = options || {}
6
+ end
7
+
8
+ # filter on class names and then methods within those classes
9
+ def allow?(msg, context)
10
+ # class name of context
11
+ class_name = context[:class_name]
12
+ rules[:classes].each do |clazz|
13
+ names=clazz[:names]
14
+ next if !names.matches_any?(name)
15
+ # if name matches rule
16
+ method_filter = MethodFilter.new(clazz)
17
+ return false if !method_filter.allow?(msg, context)
18
+ end
19
+ return true
20
+ end
21
+ end
22
+
23
+ class CompositeModuleFilter < BaseFilter
24
+ def initialize(options)
25
+ super(options)
26
+ @rules = options || {}
27
+ end
28
+
29
+ # filter on module names and then on class names and methods within those modules
30
+ def allow?(msg, context)
31
+ modules_name = context[:full_module_name]
32
+ rules[:modules].each do |_module|
33
+ # get names of modules to match
34
+ names = _module[:names]
35
+ # test if current module name matches any of the module names for this rule
36
+ next if !names.matches_any?(modules_name)
37
+ # if name matches rule
38
+ if _module[:classes]
39
+ composite_class_filter = CompositeClassFilter.new(_module)
40
+ return false if !composite_class_filter.allow?(msg, context)
41
+ end
42
+ if _module[:class_rules]
43
+ class_filter = ClassFilter.new(_module)
44
+ return false if !class_filter.allow?(msg, context)
45
+ end
46
+ if _module[:method_rules]
47
+ method_filter = MethodFilter.new(_module)
48
+ return false if !method_filter.allow?(msg, context)
49
+ end
50
+ end
51
+ # log it!
52
+ true
53
+ end
54
+ end
55
+
56
+ class CompositeInstanceVarFilter < BaseFilter
57
+ def initialize(options)
58
+ super(options)
59
+ @rules = options || {}
60
+ end
61
+
62
+ def allow?(msg, context)
63
+ rules[:vars].each do |var|
64
+ var_filter = InstanceVarFilter.new(var)
65
+ return false if !var_filter.allow?(msg, context)
66
+ end
67
+ true
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,17 @@
1
+ # convenience methods to create and operate on filters, fx merge filters into composites, extract filters from composites etc
2
+
3
+ module Tracing::Filter
4
+
5
+ def self.create_filter(name_hash)
6
+ # puts "TRY create_filter: " + name_hash.inspect
7
+ [:module_filter, :class_filter, :method_filter].each do |symbol|
8
+ # puts "symbol:" + symbol.to_s
9
+ res = name_hash.try_create_filter(symbol)
10
+ # puts "Filter created:" + res.inspect
11
+ return res if res
12
+ end
13
+ # puts "no filter could be created :("
14
+ nil
15
+ end
16
+
17
+ end
@@ -0,0 +1,35 @@
1
+ # various alternative ways to provide include/exclude filters
2
+
3
+ # interface class
4
+ module Tracing
5
+ class MsgFilter
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+
10
+ def allow?(msg, context)
11
+ true
12
+ end
13
+ end
14
+
15
+ # example of specific filter on instance variable
16
+ class RangeFilter < MsgFilter
17
+ def allow?(msg, context)
18
+ obj = context[:self]
19
+ var = obj.instance_variable_get @options[:var]
20
+ return @options[:range].include?(var)
21
+ end
22
+ end
23
+
24
+ # interface (abstract class)
25
+ # instances of this form can be used as filters inside include/exclude lists
26
+ class NameFilter
27
+ # return boolean
28
+ def allow?(name)
29
+ true
30
+ end
31
+ end
32
+ end
33
+
34
+ # custom filter
35
+ my_msg_filter = Tracing::RangeFilter.new({:range => 0..10, :var => :rating})
@@ -0,0 +1,88 @@
1
+ module Tracing
2
+ module Filter
3
+ # enable registration of filters
4
+ module Registration
5
+ attr_accessor :filters
6
+
7
+ def create_filters(reg_filters)
8
+ if reg_filters.kind_of?(Array)
9
+ reg_filters.flatten!
10
+ new_filters = reg_filters.collect do |f|
11
+ if f.kind_of? Hash
12
+ Tracing::BaseFilter.create_filter(f)
13
+ elsif f.kind_of? Tracing::BaseFilter
14
+ f
15
+ end
16
+ end
17
+ new_filters
18
+ elsif filters.kind_of? Hash
19
+ Tracing::BaseFilter.create_filter(reg_filters)
20
+ else
21
+ reg_filters
22
+ end
23
+ end
24
+
25
+ def register_filters(reg_filters)
26
+ @filters ||= []
27
+
28
+ # puts "Register filters, using: #{reg_filters.inspect}"
29
+ new_filters = create_filters(reg_filters)
30
+
31
+ # puts "New filters to register: #{new_filters.inspect}"
32
+ return if !new_filters
33
+ if new_filters
34
+ new_filters.compact!
35
+ return if !(new_filters.size > 0)
36
+ # puts "DO register filters: #{new_filters.inspect}"
37
+ @filters << new_filters.flatten!
38
+ # puts "Filters after reg: " + @filters.inspect
39
+ end
40
+ end
41
+
42
+ def unregister_filters(filters)
43
+ filters.delete(filters)
44
+ end
45
+
46
+ end
47
+
48
+ # enable execution of filters
49
+ module Exec
50
+ def filters_allow?(msg, context)
51
+ # puts "method: filters_allow?"
52
+
53
+ @filters ||= Tracing::TraceExt.filters
54
+ return true if !@filters && !@filters.kind_of?(Array)
55
+
56
+ # default allow return value
57
+ allow = true
58
+ allow = false if (Tracing::TraceExt.final_yield_action == :exclude)
59
+
60
+ @filters.each do |_filter|
61
+ # apply filter
62
+ if _filter
63
+ res = _filter.allow_action(msg, context)
64
+ # puts "res: #{res}"
65
+
66
+ if (res == :include)
67
+ # puts "included - break"
68
+ allow = true
69
+ break
70
+ end
71
+
72
+ if (res == :exclude)
73
+ # puts "excluded - break"
74
+ allow = false
75
+ break
76
+ end
77
+
78
+ # puts "yielding..."
79
+
80
+ end
81
+ end
82
+ # puts "filters_allow?: #{allow}"
83
+ return allow
84
+ end
85
+ end
86
+ end
87
+ end
88
+
@@ -0,0 +1,5 @@
1
+ require 'erb'
2
+ require 'templates/base_template'
3
+ require 'templates/string_template'
4
+ require 'templates/html_template'
5
+ require 'templates/xml_template'
data/lib/rule_match.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Tracing
2
+ module RuleMatch
3
+ class RuleTypeError < RuntimeError; end
4
+
5
+ def matches_any?(name)
6
+ # puts "matches any: #{name}"
7
+ self.any? do |rule|
8
+ if rule.kind_of? Regexp
9
+ # match return position of match, or nil if no match
10
+ # here converted into boolean result
11
+ match = (name =~ rule)
12
+ # puts "matches regexp #{rule}: #{match}"
13
+ match
14
+ elsif rule.kind_of? String
15
+ match = (name == rule)
16
+ # puts "matches string #{rule}: #{match}"
17
+ match
18
+ elsif rule.kind_of? Proc
19
+ rule.call(name)
20
+ elsif rule.kind_of? NameFilter
21
+ rule.allow?(name)
22
+ else
23
+ raise RuleTypeError, "Bad rule type: must be Regexp, String, Proc or NameFilter"
24
+ end
25
+ end
26
+ end
27
+
28
+ def rules_allow_action(name)
29
+ self.each do |rule|
30
+ res = rule.rule_allow_action(name)
31
+ return :exclude if (res == :exclude)
32
+ return :include if (res == :include)
33
+ end
34
+ # default allow action
35
+ return :yield
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,95 @@
1
+ # my_instance_variables = {:template_path => 'taglibs/rapid_core.dryml'}
2
+ # args = {:a => 7}
3
+ # context = {
4
+ # :modules => cls_name.modules,
5
+ # :class_name => cls_name.class_name,
6
+ # :full_class_name => cls_name,
7
+ # :method_name => name,
8
+ # :args => args,
9
+ # :block => false,
10
+ # :vars => my_instance_variables # to carry @template_path etc.
11
+ # }
12
+
13
+ Module_filter_A = {
14
+ :name => 'my modules',
15
+ :module_rules => [{
16
+ # id of modules rule set
17
+ :name => ['my_modules'],
18
+ :include => [/Al/],
19
+ :exclude => [/Be/],
20
+ :default => true
21
+ }
22
+ ]}
23
+
24
+ Class_filter_A = {
25
+ :name => 'my classes',
26
+ :class_rules => [{
27
+ # id of class rule set
28
+ :name => 'my_classes',
29
+ :include => [/MyCl/],
30
+ :exclude => [/NotMy/],
31
+ :default => false
32
+ }]
33
+ }
34
+
35
+ Method_filter_A = {
36
+ :name => 'my methods',
37
+ :method_rules => [{
38
+ # id of method rule set
39
+ :name => 'my_methods',
40
+ :include => [/by.*/, 'compile', 'do_it'],
41
+ :exclude => ['my.*'],
42
+ :default => false
43
+ }]
44
+ }
45
+
46
+ Class_composite_filter_A = {
47
+ # id of composite rule set
48
+ :name => 'Template stuff',
49
+ :classes => [{
50
+ :names => ['My', /My/],
51
+ :method_rules => {
52
+ :include => [/by.*/, 'DRYMLBuilder'],
53
+ :exclude => ['Taglib'],
54
+ :default => false
55
+ }
56
+ }]
57
+ }
58
+
59
+ Module_composite_filter_A = {
60
+ # id of composite rule set
61
+ :name => 'dryml_filter',
62
+ :modules => [{
63
+ :names => ['Hobo:Dryml', /Dryml/],
64
+ :class_rules => {
65
+ :include => [/Template.*/, 'DRYMLBuilder'],
66
+ :exclude => ['Taglib'],
67
+ :default => true
68
+ },
69
+ :method_rules => {
70
+ :include => [/Template.*/, 'DRYMLBuilder'],
71
+ :exclude => ['Taglib'],
72
+ :default => true
73
+ }
74
+ }]
75
+ }
76
+
77
+
78
+ # for specific instance_vars, match on values (after .to_s on var)
79
+ Var_composite_filter_A = {
80
+ :name => 'template_path',
81
+ :vars => [{
82
+ :name => 'template_path', :type => :string,
83
+ :var_rules => {
84
+ :include => [/.*\/taglib\/.*/],
85
+ :exclude => [/.*\/rapid_.*/]
86
+ }
87
+ },
88
+ {
89
+ :name => 'template_path',
90
+ :var_rules => {
91
+ :exclude => [/.*\/rapid_.*/],
92
+ :default => true
93
+ }
94
+ }]
95
+ }
@@ -0,0 +1,21 @@
1
+ module Tracing
2
+ module OutputTemplate
3
+ class BaseTrace
4
+ include Tracing::OutputHandler
5
+
6
+ def handle_after_call(context)
7
+ template = end_template(context)
8
+ output(template, context)
9
+ end
10
+
11
+ def handle_before_call(context)
12
+ template = before_template(context)
13
+ if context[:block]
14
+ template << before_block_template
15
+ end
16
+ output(template, context)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,48 @@
1
+ module Tracing
2
+ module OutputTemplate
3
+ class HtmlTrace < BaseTrace
4
+
5
+ def before_template(context)
6
+ # method_name = context[:method_full_name]
7
+ # args = context[:args].inspect
8
+ template = <<-EOF
9
+ <div class="method-title"><%= context[:method_full_name] %></div>
10
+ <div class="method-body">
11
+ <div class="begin">
12
+ <div class="method-name"><%= context[:method_full_name] %> :: BEGIN</div>
13
+ <div class="args">
14
+ <div class="method-args"><%= context[:args] %> </div>
15
+ #block#
16
+ </div>
17
+ </div>
18
+ EOF
19
+ end
20
+
21
+ def before_block_template
22
+ template = <<-EOF
23
+ <div class="method block-arg">(and a block)</div>
24
+ EOF
25
+ end
26
+
27
+ def end_template(context)
28
+ # method_name = context[:method_full_name]
29
+ # result = context[:result].inspect
30
+ template = <<-EOF
31
+ <div class="end">
32
+ <div class="method-name"><%= context[:method_full_name] %> :: END</div>
33
+ <div class="method-result"><%= context[:result] %></div>
34
+ </div>
35
+ </div>
36
+ EOF
37
+ end
38
+
39
+ # override
40
+ def handle_before_call(context)
41
+ template = before_template(context)
42
+ block_replace = context[:block] ? before_block_template : ""
43
+ template.gsub!(/#block#/, block_replace)
44
+ output(template, context)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ module Tracing
2
+ module OutputTemplate
3
+ class StringTrace < BaseTrace
4
+ def before_template(context)
5
+ template = <<-EOF
6
+ <<= <%= context[:method_full_name] %> : BEGIN
7
+ -----------------------------------------------
8
+ <%= context[:args].inspect %>
9
+ ===============================================
10
+ EOF
11
+ end
12
+
13
+ def before_block_template
14
+ template = <<-EOF
15
+ (and a block)
16
+ -----------------------------------------------
17
+ EOF
18
+ end
19
+
20
+ def end_template(context)
21
+ template = <<-EOF
22
+ <<= <%= context[:method_full_name] %> : END
23
+ -----------------------------------------------
24
+ <%= context[:result] %>
25
+ ===============================================
26
+ EOF
27
+ end
28
+ end
29
+ end
30
+ end