tailog 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tailog/version.rb +1 -1
- data/lib/tailog/watch_methods.rb +53 -33
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bc9bb6de653960e66a8228ef816c32ab109d72c
|
4
|
+
data.tar.gz: 2f8dc643971a9aa671f03c933e26918645240143
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cad43120d0b3a0b1c6ec9e8050a931568abd9b31f814074887fd46bececfe20932a74f9de7e096bee5a37cc422f609ad5cbe64b81b7c1254bb91ab929788f2a3
|
7
|
+
data.tar.gz: 8d842e8058e457a9e3bdb41a9d00fbbcf875e4a3c173d22309b733141c1d08d3d9e422a286ba19d4befaaec59be4215f6efca4f868b45fef769ad5a10b4d9b80
|
data/lib/tailog/version.rb
CHANGED
data/lib/tailog/watch_methods.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
require 'active_support/core_ext/string'
|
2
2
|
require 'securerandom'
|
3
3
|
require 'logger'
|
4
|
+
require 'erb'
|
4
5
|
|
5
6
|
module Tailog
|
6
7
|
module WatchMethods
|
7
8
|
class << self
|
9
|
+
attr_accessor :inject_options
|
10
|
+
|
8
11
|
def logger
|
9
12
|
return @logger if @logger
|
10
13
|
@logger = Logger.new(File.join Tailog.log_path, "watch_methods.log")
|
@@ -22,18 +25,27 @@ module Tailog
|
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
|
28
|
+
self.inject_options = {
|
29
|
+
self: true,
|
30
|
+
arguments: true,
|
31
|
+
caller_backtrace: false,
|
32
|
+
result: true,
|
33
|
+
error_backtrace: true
|
34
|
+
}
|
35
|
+
|
36
|
+
def inject targets, options = {}
|
37
|
+
options = Tailog::WatchMethods.inject_options.merge(options)
|
26
38
|
targets.each do |target|
|
27
39
|
begin
|
28
40
|
if target.include? "#"
|
29
|
-
inject_instance_method target
|
41
|
+
inject_instance_method target, options
|
30
42
|
elsif target.include? "."
|
31
|
-
inject_class_method target
|
43
|
+
inject_class_method target, options
|
32
44
|
else
|
33
|
-
inject_constant target
|
45
|
+
inject_constant target, options
|
34
46
|
end
|
35
47
|
rescue => error
|
36
|
-
WatchMethods.logger.error "Inject #{target} FAILED: #{error.class}: #{error.message}"
|
48
|
+
WatchMethods.logger.error "Inject #{target} FAILED: #{error.class}: #{error.message}."
|
37
49
|
end
|
38
50
|
end
|
39
51
|
end
|
@@ -58,30 +70,31 @@ module Tailog
|
|
58
70
|
method.to_s.start_with? RAW_METHOD_PREFIX
|
59
71
|
end
|
60
72
|
|
61
|
-
def inject_constant target
|
73
|
+
def inject_constant target, options
|
62
74
|
constant = target.constantize
|
63
75
|
constant.instance_methods(false).each do |method|
|
64
|
-
inject_instance_method "#{target}##{method}" unless raw_method? method
|
76
|
+
inject_instance_method "#{target}##{method}", options unless raw_method? method
|
65
77
|
end
|
66
78
|
constant.methods(false).each do |method|
|
67
|
-
inject_class_method "#{target}.#{method}" unless raw_method? method
|
79
|
+
inject_class_method "#{target}.#{method}", options unless raw_method? method
|
68
80
|
end
|
69
81
|
end
|
70
82
|
|
71
83
|
def cleanup_constant target
|
72
|
-
target.constantize
|
84
|
+
constant = target.constantize
|
85
|
+
constant.instance_methods(false).each do |method|
|
73
86
|
cleanup_instance_method "#{target}##{method}" unless raw_method? method
|
74
87
|
end
|
75
|
-
|
88
|
+
constant.methods(false).each do |method|
|
76
89
|
cleanup_class_method "#{target}.#{method}" unless raw_method? method
|
77
90
|
end
|
78
91
|
end
|
79
92
|
|
80
|
-
def inject_class_method target
|
93
|
+
def inject_class_method target, options
|
81
94
|
klass, _, method = target.rpartition(".")
|
82
95
|
klass.constantize.class_eval <<-EOS, __FILE__, __LINE__
|
83
96
|
class << self
|
84
|
-
#{build_watch_method target, method}
|
97
|
+
#{build_watch_method target, method, options}
|
85
98
|
end
|
86
99
|
EOS
|
87
100
|
end
|
@@ -95,10 +108,10 @@ module Tailog
|
|
95
108
|
EOS
|
96
109
|
end
|
97
110
|
|
98
|
-
def inject_instance_method target
|
111
|
+
def inject_instance_method target, options
|
99
112
|
klass, _, method = target.rpartition("#")
|
100
113
|
klass.constantize.class_eval <<-EOS, __FILE__, __LINE__
|
101
|
-
#{build_watch_method target, method}
|
114
|
+
#{build_watch_method target, method, options}
|
102
115
|
EOS
|
103
116
|
end
|
104
117
|
|
@@ -109,26 +122,9 @@ module Tailog
|
|
109
122
|
EOS
|
110
123
|
end
|
111
124
|
|
112
|
-
def build_watch_method target, method
|
125
|
+
def build_watch_method target, method, options
|
113
126
|
raw_method = "#{RAW_METHOD_PREFIX}#{method}"
|
114
|
-
|
115
|
-
unless instance_methods.include?(:#{raw_method})
|
116
|
-
alias_method :#{raw_method}, :#{method}
|
117
|
-
def #{method} *args
|
118
|
-
start = Time.now
|
119
|
-
call_id = SecureRandom.uuid
|
120
|
-
Tailog::WatchMethods.logger.info "[\#{call_id}] #{target} CALLED: self: \#{self.inspect}, arguments: \#{args.inspect}"
|
121
|
-
result = send :#{raw_method}, *args
|
122
|
-
Tailog::WatchMethods.logger.info "[\#{call_id}] #{target} FINISHED: \#{(Time.now - start) * 1000} ms, result: \#{result.inspect}"
|
123
|
-
result
|
124
|
-
rescue => error
|
125
|
-
Tailog::WatchMethods.logger.error "[\#{call_id}] #{target} FAILED: \#{error.class} - \#{error.message} => \#{error.backtrace.join(", ")}"
|
126
|
-
raise error
|
127
|
-
end
|
128
|
-
else
|
129
|
-
Tailog::WatchMethods.logger.error "Inject method `#{target}' failed: already injected"
|
130
|
-
end
|
131
|
-
EOS
|
127
|
+
ERB.new(WATCH_METHOD_ERB).result(binding)
|
132
128
|
end
|
133
129
|
|
134
130
|
def build_cleanup_method target, method
|
@@ -142,3 +138,27 @@ module Tailog
|
|
142
138
|
end
|
143
139
|
end
|
144
140
|
end
|
141
|
+
|
142
|
+
WATCH_METHOD_ERB = <<-EOS
|
143
|
+
unless instance_methods.include?(:<%= raw_method %>)
|
144
|
+
alias_method :<%= raw_method %>, :<%= method %>
|
145
|
+
def <%= method %> *args
|
146
|
+
start = Time.now
|
147
|
+
call_id = SecureRandom.uuid
|
148
|
+
|
149
|
+
Tailog::WatchMethods.logger.info "[\#{call_id}] <%= target %> CALLED<% if options[:self] %>, self: \#{self.inspect}<% end %><% if options[:arguments] %>, arguments: \#{args.inspect}<% end %><% if options[:caller_backtrace] %>, backtrace: \#{caller.join(", ")}<% end %>."
|
150
|
+
|
151
|
+
result = send :<%= raw_method %>, *args
|
152
|
+
|
153
|
+
Tailog::WatchMethods.logger.info "[\#{call_id}] <%= target %> FINISHED in \#{(Time.now - start) * 1000} ms<% if options[:result] %>, result: \#{result.inspect}<% end %>."
|
154
|
+
|
155
|
+
result
|
156
|
+
rescue => error
|
157
|
+
Tailog::WatchMethods.logger.error "[\#{call_id}] <%= target %> FAILED: \#{error.class}: \#{error.message}<% if options[:error_backtrace] %>, backtrace: \#{error.backtrace.join(", ")}<% end %>."
|
158
|
+
|
159
|
+
raise error
|
160
|
+
end
|
161
|
+
else
|
162
|
+
Tailog::WatchMethods.logger.error "Inject <%= target %> FAILED: already injected."
|
163
|
+
end
|
164
|
+
EOS
|