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,78 @@
1
+ require 'core_extensions'
2
+ require 'filters/base_filters'
3
+ require 'filters/message_filters'
4
+
5
+ # Procs of this form can be used as filters inside include/exclude lists
6
+ rest_view = Proc.new {|name| name == 'abc' }
7
+
8
+ module Tracing
9
+ # abstract context filter
10
+ class MsgContextFilter
11
+ attr_accessor :options
12
+
13
+ def initialize(options)
14
+ @options = options
15
+ end
16
+
17
+ def allow?(msg, context)
18
+ true
19
+ end
20
+ end
21
+
22
+ # abstract name filter
23
+ class NameFilter
24
+ attr_accessor :options
25
+
26
+ def initialize(options)
27
+ @options = options
28
+ end
29
+
30
+ # return boolean
31
+ def allow?(name)
32
+ true
33
+ end
34
+ end
35
+
36
+ class CustomNameFilter < NameFilter
37
+ def allow?(name)
38
+ name == options[:name]
39
+ end
40
+ end
41
+ end
42
+
43
+ msg_filter = Tracing::MsgContextFilter.new '22'
44
+
45
+ name_filter = Tracing::CustomNameFilter.new('rapid')
46
+
47
+ var_rule_A = {
48
+ :name => 'var rule A',
49
+ :var_name => :template_path,
50
+ :var_rules => {
51
+ :include => [rest_view, name_filter],
52
+ :exclude => [/.*\/rapid_.*/],
53
+ :default => false
54
+ }
55
+ }
56
+
57
+ comp_var_rules_A = {
58
+ :name => 'test template_path',
59
+ :vars => [var_rule_A]
60
+ }
61
+
62
+ cls_name = "Alpha::Beta::Gamma"
63
+ name = "my_method"
64
+ my_instance_variables = {:template_path => 'taglibs/rapid_core.dryml'}
65
+ args = {:a => 7}
66
+ @context = {
67
+ :modules => cls_name.modules,
68
+ :class_name => cls_name.class_name,
69
+ :full_class_name => cls_name,
70
+ :method_name => name,
71
+ :args => args,
72
+ :block => false,
73
+ :vars => my_instance_variables # to carry @template_path etc.
74
+ }
75
+
76
+ var_filter = Tracing::InstanceVarFilter.new(var_rule_A)
77
+
78
+ puts var_filter.allow?("hello", @context)
@@ -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::XmlAppender.new(:tracer => :xml, :to_file => 'log_files/xml/traced.xml')
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
+
@@ -0,0 +1,46 @@
1
+ require 'ftools'
2
+
3
+
4
+ @begin = "<?xml version='1.0' encoding='UTF-8'?>\n<tracing>\n"
5
+ @end = "</tracing>"
6
+
7
+ File.open('text.xml', "w") do |file|
8
+ file.puts @begin
9
+ file.puts @end
10
+ end
11
+ #
12
+ # File.open('text.xml', "a+") do |file|
13
+ # file.seek(-@end.length, IO:SEEK_END)
14
+ # file.puts "hello world"
15
+ # end
16
+
17
+
18
+ # # # Create a new file and write to it
19
+ # File.open('test.rb', 'w+') do |f2|
20
+ # # use "\n" for two lines of text
21
+ # f2.puts "Created by Satish\nThank God!"
22
+ # end
23
+ #
24
+ # f = File.new("test.rb", "a+")
25
+ # #
26
+ # # # SEEK_CUR - Seeks to first integer number parameter plus current position
27
+ # # # SEEK_END - Seeks to first integer number parameter plus end of stream
28
+ # # # (you probably want a negative value for first integer number parameter)
29
+ # # # SEEK_SET - Seeks to the absolute location given by first integer number parameter
30
+ # # # :: is the scope operator - more on this later
31
+ # # f.seek(12, IO::SEEK_SET)
32
+ # # print f.readline
33
+ # str = "hello world</end>"
34
+ # f.seek(-6, IO::SEEK_END)
35
+ # f.puts str
36
+ # f.close
37
+
38
+ def gsub_file(path, regexp, *args, &block)
39
+ content = File.read(path).gsub(regexp, *args, &block)
40
+ File.open(path, 'wb') { |file| file.write(content) }
41
+ end
42
+
43
+ line = '</tracing>'
44
+ gsub_file 'text.xml', /(#{Regexp.escape(line)})/mi do |match|
45
+ "<line>hello</line>\n#{match}"
46
+ end
@@ -0,0 +1,9 @@
1
+ require 'appenders/appender_registration'
2
+ require 'appenders/appender'
3
+ require 'appenders/base_appender'
4
+ require 'appenders/file_appender'
5
+ require 'appenders/logger_appender'
6
+ require 'appenders/stream_appender'
7
+ require 'appenders/xml_appender'
8
+ require 'appenders/html_appender'
9
+ require 'appenders/template_log_appender'
@@ -0,0 +1,86 @@
1
+ require 'trace_ext'
2
+
3
+ module Tracing
4
+ module TraceCalls
5
+
6
+ def self.included(klass)
7
+ suppress_tracing do
8
+ klass.instance_methods(false).each do |existing_method|
9
+ wrap(klass, existing_method)
10
+ end
11
+ end
12
+ def klass.method_added(method) # note: nested definition
13
+ unless @trace_calls_internal
14
+ @trace_calls_internal = true
15
+ TraceCalls.wrap(self, method)
16
+ @trace_calls_internal = false
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.suppress_tracing
22
+ Thread.current[:'suppress tracing'] = true
23
+ yield
24
+ ensure
25
+ Thread.current[:'suppress tracing'] = false
26
+ end
27
+
28
+ def self.ok_to_trace?(name)
29
+ !Thread.current[:'suppress tracing']
30
+ end
31
+
32
+ def self.wrap(klass, method)
33
+ klass.class_eval do
34
+ name = method.to_s
35
+ original_method = instance_method(name)
36
+
37
+ define_method(name) do |*args, &block|
38
+ cls_name = self.class.to_s
39
+ context = {
40
+ :modules => cls_name.modules,
41
+ :full_module_name => cls_name.modules.join("::"),
42
+ :class_name => cls_name.class_name,
43
+ :full_class_name => cls_name,
44
+ :method_name => name,
45
+ :args => args,
46
+ :block => block_given?,
47
+ :self => self,
48
+ :vars => self.instance_variables} # to carry @template_path etc.
49
+ context[:method_full_name] = method_full_name(context)
50
+
51
+ if TraceCalls.ok_to_trace?(name)
52
+ TraceCalls.suppress_tracing do
53
+ if trace_method?(context)
54
+ # puts "tracing before:" + context[:method_full_name]
55
+ handle_before_call(context)
56
+ end
57
+ end
58
+ end
59
+ result = original_method.bind(self).call(*args, &block)
60
+ if TraceCalls.ok_to_trace?(name)
61
+ TraceCalls.suppress_tracing do
62
+ if trace_method?(context)
63
+ # puts "tracing after:" + context[:method_full_name]
64
+ context[:result] = result
65
+ handle_after_call(context)
66
+ end
67
+ end
68
+ end
69
+ res = result
70
+ # if TraceCalls.ok_to_trace?(name)
71
+ # TraceCalls.suppress_tracing do
72
+ # if trace_method?(name)
73
+ # # method_stack.pop
74
+ # end
75
+ # end
76
+ # end
77
+ res
78
+ end
79
+ end
80
+ end
81
+
82
+ suppress_tracing do
83
+ include Tracing::TraceExt
84
+ end
85
+ end
86
+ end
data/lib/trace_ext.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'templates/trace_output_handler'
2
+ require 'trace_filters'
3
+ require 'action_handler'
4
+ require 'trace_appenders'
5
+
6
+ module Tracing
7
+ module TraceExt
8
+ # include Tracing::Filter::Registration
9
+ include Tracing::Filter::Exec
10
+
11
+ class << self
12
+ include Tracing::Filter::Registration
13
+ include Tracing::ActionHandler::Registration
14
+
15
+ attr_accessor :final_yield_action
16
+
17
+ def configure(options)
18
+ # puts "Filters before config: " + Tracing::TraceExt.filters.inspect
19
+
20
+ register_filters(options[:filters])
21
+
22
+ # puts "Filters after config: " + Tracing::TraceExt.filters.inspect
23
+
24
+ register_action_handlers(options[:action_handlers] || options)
25
+ @final_yield_action = options[:final_yield] || :include
26
+ end
27
+
28
+ end
29
+
30
+ def method_full_name(context)
31
+ "#{context[:class_name]}.#{context[:method_name]}"
32
+ end
33
+
34
+ def trace_method?(context)
35
+ filters_allow?('', context)
36
+ end
37
+
38
+ def handle_before_call(context)
39
+ exec_action_handlers('BEGIN' , context)
40
+ end
41
+
42
+ def handle_after_call(context)
43
+ exec_action_handlers('END' , context)
44
+ end
45
+
46
+ def exec_action_handlers(txt, context)
47
+ _handlers = Tracing::TraceExt.action_handlers
48
+
49
+ # puts "exec_action_handlers:" + _handlers.inspect
50
+ return if !_handlers || !_handlers.kind_of?(Array)
51
+ # puts "each handler..."
52
+ _handlers.each do |handler|
53
+ handler.handle(txt, context)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ require 'filters/tracing_filter'
2
+ require 'filters/base_filters'
3
+ require 'filters/composite_filters'
4
+ require 'filters/filter_factory'
@@ -0,0 +1,98 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{trace-util-adv}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = %q{2009-10-28}
13
+ s.description = %q{
14
+ Configure tracing using context sensitive filters, appenders, output templates in a completely non-intrusive fashion.
15
+ Tracing can even be applied runtime as a response when certain conditions occur}
16
+ s.email = %q{kmandrup@gmail.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "CHANGELOG",
25
+ "LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/README",
30
+ "lib/TODO.txt",
31
+ "lib/action_handler.rb",
32
+ "lib/appenders/appender.rb",
33
+ "lib/appenders/appender_registration.rb",
34
+ "lib/appenders/base_appender.rb",
35
+ "lib/appenders/file_appender.rb",
36
+ "lib/appenders/html_appender.rb",
37
+ "lib/appenders/logger_appender.rb",
38
+ "lib/appenders/stream_appender.rb",
39
+ "lib/appenders/template_log_appender.rb",
40
+ "lib/appenders/xml_appender.rb",
41
+ "lib/core_extensions.rb",
42
+ "lib/filters/base_filters.rb",
43
+ "lib/filters/composite_filters.rb",
44
+ "lib/filters/filter_factory.rb",
45
+ "lib/filters/message_filters.rb",
46
+ "lib/filters/tracing_filter.rb",
47
+ "lib/output_templates.rb",
48
+ "lib/rule_match.rb",
49
+ "lib/sample_filters.rb",
50
+ "lib/templates/base_template.rb",
51
+ "lib/templates/html_template.rb",
52
+ "lib/templates/string_template.rb",
53
+ "lib/templates/trace_output_handler.rb",
54
+ "lib/templates/xml_template.rb",
55
+ "lib/test_action_handler.rb",
56
+ "lib/test_appender.rb",
57
+ "lib/test_file_appender.rb",
58
+ "lib/test_filters.rb",
59
+ "lib/test_filters_chain.rb",
60
+ "lib/test_filters_create.rb",
61
+ "lib/test_html_appender.rb",
62
+ "lib/test_special_filters.rb",
63
+ "lib/test_xml_appender.rb",
64
+ "lib/test_xml_gen.rb",
65
+ "lib/trace-util-adv.rb",
66
+ "lib/trace_appenders.rb",
67
+ "lib/trace_calls.rb",
68
+ "lib/trace_ext.rb",
69
+ "lib/trace_filters.rb",
70
+ "spec/spec.opts",
71
+ "spec/spec_helper.rb",
72
+ "spec/trace-util-adv_spec.rb",
73
+ "trace-util-adv.gemspec"
74
+ ]
75
+ s.homepage = %q{http://github.com/kristianmandrup/trace-util-adv}
76
+ s.rdoc_options = ["--charset=UTF-8"]
77
+ s.require_paths = ["lib"]
78
+ s.rubygems_version = %q{1.3.5}
79
+ s.summary = %q{adds advancing tracing capability to your ruby code}
80
+ s.test_files = [
81
+ "spec/spec_helper.rb",
82
+ "spec/trace-util-adv_spec.rb"
83
+ ]
84
+
85
+ if s.respond_to? :specification_version then
86
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
87
+ s.specification_version = 3
88
+
89
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
90
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
91
+ else
92
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
93
+ end
94
+ else
95
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
96
+ end
97
+ end
98
+