trace-util-adv 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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,47 @@
1
+ module Tracing
2
+ module OutputHandler
3
+
4
+ # TODO: refactor (duplicate of appender method)
5
+ def method_stack
6
+ @method_stack ||= []
7
+ end
8
+
9
+ # override to increase # of spaces for each indenation level!
10
+ def space_count
11
+ 1
12
+ end
13
+
14
+ # calculate indentation level, based on method stack level (# of nested method calls)
15
+ def indentation
16
+ s = ""
17
+ lv = method_stack.empty? ? 0 : method_stack.size-1
18
+ lv.times { s << (" " * space_count) }
19
+ s
20
+ end
21
+
22
+ def output(template, context)
23
+ # get spaces to indent each line
24
+ spaces = indentation
25
+
26
+ # modify template, inserting indentation
27
+ lines = template.split("\n")
28
+ lines.map!{|line| spaces + line + "\n"}
29
+ template = lines.join
30
+
31
+ # send modified template to output handler with context
32
+ output_handler(template, context)
33
+ end
34
+
35
+ # default output action: put string to STDOUT
36
+ # override this to customize trace handling
37
+ def output_handler(template, context)
38
+ # create new erb template
39
+ erb_template = ERB.new template
40
+
41
+ # evaluate erb template for output using context
42
+ str_res = erb_template.result(binding)
43
+ # send final output to action handlers for further processing...
44
+ # action_handlers(str_res, context)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ module Tracing
2
+ module OutputTemplate
3
+ class XmlTrace < BaseTrace
4
+ def before_template(context)
5
+ template = <<-EOF
6
+ <method name="<%= context[:method_full_name] %>">
7
+ <modules><%= context[:full_module_name] %></modules>
8
+ <class><%= context[:class_name].join(',') %></class>
9
+ <args><%= context[:args].inspect %></args>
10
+ #block#
11
+ EOF
12
+ end
13
+
14
+ def before_block_template
15
+ template = <<-EOF
16
+ <block-arg>true</block-arg>
17
+ EOF
18
+ end
19
+
20
+ def end_template(context)
21
+ template = <<-EOF
22
+ <result><%= context[:result] %></result>
23
+ </method>
24
+ EOF
25
+ end
26
+
27
+ # override
28
+ def handle_before_call(context)
29
+ template = before_template(context)
30
+ block_replace = context[:block] ? before_block_template : ""
31
+ template.gsub!(/#block#/, block_replace)
32
+ output(template, context)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,34 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "sample_filters"
4
+ require "rubygems"
5
+ require "duration"
6
+ require "test/unit"
7
+
8
+ class TestFilter < Test::Unit::TestCase
9
+
10
+ attr_reader :ah1, :filters
11
+
12
+ def setup
13
+ # tla1 = Tracing::TemplateLogAppender.new(:options => {:overwrite => false, :time_limit => 2.minutes}, :tracer => :string)
14
+ # action handler is configured with a set of filters and a set of appenders
15
+ # the appenders are called in turn if log statement passes all filters!
16
+ # @ah1 = Tracing::ActionHandler.new(:filters => @filters = Module_filter_A)
17
+
18
+ appender_options = {:options => {:overwrite => false, :time_limit => 2.minutes}, :type => :logger, :tracer => :string}
19
+ # @ah2 = Tracing::ActionHandler.new(:filters => @filters = [Module_filter_A, Method_filter_A], :appenders => appender_options)
20
+
21
+ @ah3 = Tracing::ActionHandler.new(:filters => @filters = [Module_filter_A, Method_filter_A], :appenders => :logger)
22
+ end
23
+
24
+ def teardown
25
+ ## Nothing really
26
+ end
27
+
28
+ def test_filter
29
+ puts "ActionHandler:" + @ah3.inspect
30
+ end
31
+
32
+
33
+ end
34
+
@@ -0,0 +1,29 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "sample_filters"
4
+ require "rubygems"
5
+ require "duration"
6
+ require "test/unit"
7
+
8
+ class TestFilter < Test::Unit::TestCase
9
+
10
+ attr_reader :ah1, :filters
11
+
12
+ def setup
13
+ # Tracing::Appender.register_default_mappings
14
+
15
+ # test all the ways an appender can be initialized!
16
+ @ah1 = Tracing::LoggerAppender.new(:tracer => :html)
17
+ end
18
+
19
+ def teardown
20
+ ## Nothing really
21
+ end
22
+
23
+ def test_filter
24
+ puts @ah1.inspect
25
+ end
26
+
27
+
28
+ end
29
+
@@ -0,0 +1,32 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "sample_filters"
4
+ require "rubygems"
5
+ require "duration"
6
+ require "test/unit"
7
+
8
+ class TestFilter < Test::Unit::TestCase
9
+
10
+ attr_reader :ah1, :filters
11
+
12
+ def setup
13
+ # Tracing::Appender.register_default_mappings
14
+ @file = 'test.txt'
15
+
16
+ # test all the ways an appender can be initialized!
17
+ @file_appender = Tracing::FileAppender.new({:overwrite => true, :to_file => @file})
18
+ end
19
+
20
+ def teardown
21
+ ## Nothing really
22
+ end
23
+
24
+ def test_filter
25
+ @file_appender.write_file(@file, "Hello World")
26
+ marker_txt = 'World'
27
+ @file_appender.insert_into_file(@file, 'My old ', marker_txt)
28
+ end
29
+
30
+
31
+ end
32
+
@@ -0,0 +1,112 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "output_templates"
4
+ require "sample_filters"
5
+ require "rubygems"
6
+ require "duration"
7
+ require "test/unit"
8
+
9
+ # configure use of TraceExt
10
+ module Me
11
+ class My
12
+
13
+ def hello
14
+ puts "Hello cruel World!"
15
+ end
16
+
17
+ def hi_there
18
+ puts "Hello cruel World!"
19
+ end
20
+
21
+ def blip
22
+ puts "Blip!"
23
+ end
24
+
25
+ def blap
26
+ puts "Blap!"
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ Method_filter_hello = {
33
+ :name => 'my methods',
34
+ :method_rules => [{
35
+ # id of method rule set
36
+ :name => 'my_methods',
37
+ :include => [/hi.*/, 'blip', 'blap'],
38
+ :exclude => ['hello'],
39
+ :default => false
40
+ }]
41
+ }
42
+
43
+
44
+ class TestFilter < Test::Unit::TestCase
45
+
46
+ attr_reader :ah1, :filters
47
+
48
+ def setup
49
+ # module filter is created (see sample filters)
50
+ # @module_filter = Tracing::ModuleFilter.new(Module_filter_A)
51
+
52
+ # Appender is configured with some appender options and a Tracer
53
+ # string_tracer = Tracing::OutputTemplate::StringTrace.new
54
+ # template_log_options = {:options => {:overwrite => false, :time_limit => 2.minutes}, :tracer => string_tracer}
55
+ # tla1 = Tracing::TemplateLogAppender.new(:options => {:overwrite => false, :time_limit => 2.minutes}, :tracer => :string)
56
+
57
+ # action handler is configured with a set of filters and a set of appenders
58
+ # the appenders are called in turn if log statement passes all filters!
59
+ @ah1 = Tracing::ActionHandler.new(:filters => @filters = [Module_filter_A, Method_filter_A], :appenders => :logger)
60
+
61
+ appender_options = {:options => {:overwrite => false, :time_limit => 2.minutes}, :type => :logger, :tracer => :string}
62
+
63
+ # @ah2 = Tracing::ActionHandler.new(:filters => @filters = [Module_filter_A, Method_filter_A], :appenders => appender_options)
64
+
65
+ # TraceExt is configured with a set of action handlers
66
+ # on any method call the set of action handlers are called if all initial filters are passed
67
+ # Tracing::TraceExt.register_action_handlers([ah1])
68
+ end
69
+
70
+ def teardown
71
+ ## Nothing really
72
+ end
73
+
74
+ def test_filter
75
+ # module_filter = Tracing::ModuleFilter.new(Module_filter_A)
76
+
77
+ # TraceExt can be configured with a set of 'initial' filters, that determine if the method should have theaspects applied at all
78
+ # Tracing::TraceExt.register_filters([module_filter])
79
+ # , :filters => Class_filter_A
80
+ # Tracing::TraceExt.configure(:action_handlers => [ah1])
81
+ # Tracing::TraceExt.configure(:appenders => :logger)
82
+
83
+ dryml_classes_to_trace = [ 'My']
84
+
85
+ dryml_classes_to_trace.each do |cls|
86
+ str = "Me::#{cls}.class_eval { include Tracing::TraceCalls }"
87
+ puts str
88
+ eval str
89
+ end
90
+
91
+ # Me::My.class_eval { include Tracing::TraceCalls }
92
+
93
+ Tracing::HtmlAppender.default_path = 'log_files/html'
94
+
95
+ # Tracing::TraceExt.configure(:appenders => {:type => :html, :to_file => 'log_files/html/traced.html'})
96
+ Tracing::TraceExt.configure(:type => :html, :to_file => 'traced4.html', :filters => Method_filter_hello)
97
+
98
+ # Tracing::TraceExt.configure(:filters => Class_filter_A)
99
+ #
100
+ my = Me::My.new
101
+ my.hello
102
+ my.hi_there
103
+ my.blip
104
+ my.blap
105
+
106
+
107
+ # my.action_handlers(['a', 'b'], context)
108
+ end
109
+
110
+
111
+ end
112
+
@@ -0,0 +1,100 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "output_templates"
4
+ require "rubygems"
5
+ require "duration"
6
+ require "test/unit"
7
+
8
+ # configure use of TraceExt
9
+ module Me
10
+ class My
11
+
12
+ def hello
13
+ puts "Hello World!"
14
+ end
15
+
16
+ def hi_there
17
+ puts "Hi there!"
18
+ end
19
+
20
+ def blip
21
+ puts "Blip!"
22
+ end
23
+
24
+ def blap
25
+ puts "Blap!"
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ Method_filter_A = {
32
+ :name => 'my methods',
33
+ :method_rules => [{
34
+ # id of method rule set
35
+ :name => 'my_methods',
36
+ :include => [/hi.*/],
37
+ :exclude => ['hello'],
38
+ :default => :yield
39
+ }]
40
+ }
41
+
42
+ # Method_filter_B = {
43
+ # :name => 'my methods',
44
+ # :method_rules => [{
45
+ # # id of method rule set
46
+ # :name => 'my_methods',
47
+ # :include => ['blip', 'blop'],
48
+ # :default => :yield
49
+ # }]
50
+ # }
51
+
52
+
53
+ class TestFilter < Test::Unit::TestCase
54
+
55
+ attr_reader :ah1, :filters
56
+
57
+ def setup
58
+ end
59
+
60
+ def teardown
61
+ ## Nothing really
62
+ end
63
+
64
+ def test_filter
65
+ # Tracing::TraceExt.configure(:appenders => :logger, :filters => [Method_filter_A, Method_filter_B])
66
+ Me::My.class_eval { include Tracing::TraceCalls }
67
+
68
+ Tracing::HtmlAppender.default_path = 'log_files/html'
69
+
70
+ names_A = %w{blip blop}
71
+ names_B = "blip blop"
72
+ names_C = "blip, blop"
73
+
74
+ bfil = {:xmethod_filter => names_B}
75
+
76
+ # method_filter_A = Tracing::Filter.create_filter({:imethod_filter => names_A})
77
+ method_filter_B = Tracing::Filter.create_filter(bfil)
78
+ # method_filter_C = Tracing::Filter.create_filter({:imethod_filter => names_C})
79
+
80
+ # Tracing::TraceExt.configure(:appenders => {:type => :html, :to_file => 'log_files/html/traced.html'})
81
+ meth_filter_A = {:imethod_filter => names_A}
82
+
83
+ puts "mf B: " + method_filter_B.inspect
84
+
85
+ Tracing::TraceExt.configure(:appenders => {:type => :html, :to_file => 'traced4.html'}, :filters => [meth_filter_A], :final_yield => :exclude)
86
+
87
+ puts "Handlers configured:" + Tracing::TraceExt.action_handlers.inspect
88
+
89
+ #
90
+ my = Me::My.new
91
+ my.hello
92
+ my.hi_there
93
+ my.blip
94
+ my.blap
95
+
96
+ end
97
+
98
+
99
+ end
100
+
@@ -0,0 +1,28 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "output_templates"
4
+ require "rubygems"
5
+ require "duration"
6
+ require "test/unit"
7
+
8
+ class TestFilter < Test::Unit::TestCase
9
+
10
+ def teardown
11
+ ## Nothing really
12
+ end
13
+
14
+ def test_filter_create
15
+
16
+ names = %w{blip blop}
17
+ puts "create include method filter, names: #{names}"
18
+ method_filter_B = Tracing::Filter.create_filter({:imethod_filter => names})
19
+
20
+ f = Tracing::BaseFilter.create_filter(method_filter_B)
21
+
22
+ puts method_filter_B.inspect
23
+ puts f.inspect
24
+ end
25
+
26
+
27
+ end
28
+
@@ -0,0 +1,66 @@
1
+ require "core_extensions"
2
+ require "trace_calls"
3
+ require "output_templates"
4
+ require "sample_filters"
5
+ require "rubygems"
6
+ require "duration"
7
+ require "test/unit"
8
+
9
+ class TestFilter < Test::Unit::TestCase
10
+
11
+ attr_reader :ah1, :filters
12
+
13
+ def setup
14
+ # Tracing::Appender.register_default_mappings
15
+
16
+ # test all the ways an appender can be initialized!
17
+ @ah1 = Tracing::HtmlAppender.new(:tracer => :html, :to_file => 'log_files/html/traced.html')
18
+ end
19
+
20
+ def teardown
21
+ ## Nothing really
22
+ end
23
+
24
+ def method_full_name(context)
25
+ "#{context[:class_name]}.#{context[:method_name]}"
26
+ end
27
+
28
+ def test_filter
29
+ cls_name = "Alpha::Beta::Gamma"
30
+ name = "my_method"
31
+ my_instance_variables = {:template_path => 'taglibs/rapid_core.dryml'}
32
+ args = {:a => 7}
33
+ @context = {
34
+ :modules => cls_name.modules,
35
+ :class_name => cls_name.class_name,
36
+ :full_class_name => cls_name,
37
+ :method_name => name,
38
+ :args => args,
39
+ :block => false,
40
+ :vars => my_instance_variables # to carry @template_path etc.
41
+ }
42
+ @context[:method_full_name] = method_full_name(@context)
43
+
44
+ @ah1.allow_append("BEGIN #{name}", @context)
45
+
46
+ name = "my_other_method"
47
+
48
+ @context[:method_name] = name
49
+ @context[:method_full_name] = method_full_name(@context)
50
+
51
+ @ah1.allow_append("BEGIN #{name}", @context)
52
+
53
+ @context[:result] = "32"
54
+ @ah1.allow_append("END #{name}", @context)
55
+
56
+ name = "my_method"
57
+ @context[:method_name] = name
58
+ @context[:method_full_name] = method_full_name(@context)
59
+
60
+ @context[:result] = "27"
61
+ @ah1.allow_append("END #{name}", @context)
62
+ end
63
+
64
+
65
+ end
66
+