@@ -19,7 +19,7 @@ def hook_builtins?
1919 Mutex . new . synchronize do
2020 @hook_builtins = true if @hook_builtins . nil?
2121
22- return false unless @hook_builtins
22+ next false unless @hook_builtins
2323
2424 @hook_builtins = false
2525 true
@@ -101,48 +101,52 @@ def hook_builtins
101101 hook_loaded_code = lambda do |hooks_by_class , builtin |
102102 hooks_by_class . each do |class_name , hooks |
103103 Array ( hooks ) . each do |hook |
104- if builtin && hook . package . require_name && hook . package . require_name != 'ruby'
105- require hook . package . require_name
106- end
107-
108- Array ( hook . method_names ) . each do |method_name |
109- method_name = method_name . to_sym
110-
104+ HookLog . builtin class_name do
105+ if builtin && hook . package . require_name && hook . package . require_name != 'ruby'
106+ begin
107+ require hook . package . require_name
108+ rescue
109+ HookLog . load_error hook . package . require_name , "Unable to require #{ hook . package . require_name } : #{ $!} " if HookLog . enabled?
110+ next
111+ end
112+ end
113+
111114 begin
112115 base_cls = Object . const_get class_name
113116 rescue NameError
117+ HookLog . load_error class_name , "Class #{ class_name } not found in global scope" if HookLog . enabled?
114118 next
115119 end
116-
117- HookLog . builtin_begin class_name , method_name if HookLog . enabled?
118-
119- hook_method = lambda do |entry |
120- cls , method = entry
121- next if config . never_hook? ( cls , method )
122-
123- hook . package . handler_class . new ( hook . package , cls , method ) . activate
124- end
125-
126- methods = [ ]
127- # irb(main):001:0> Kernel.public_instance_method(:system)
128- # (irb):1:in `public_instance_method': method `system' for module `Kernel' is private (NameError)
129- if base_cls == Kernel
130- methods << [ base_cls , base_cls . instance_method ( method_name ) ] rescue nil
131- end
132- methods << [ base_cls , base_cls . public_instance_method ( method_name ) ] rescue nil
133- methods << [ base_cls , base_cls . protected_instance_method ( method_name ) ] rescue nil
134- if base_cls . respond_to? ( :singleton_class )
135- methods << [ base_cls . singleton_class , base_cls . singleton_class . public_instance_method ( method_name ) ] rescue nil
136- methods << [ base_cls . singleton_class , base_cls . singleton_class . protected_instance_method ( method_name ) ] rescue nil
137- end
138- methods . compact!
139- if methods . empty?
140- HookLog . log "Method #{ method_name } not found on #{ base_cls . name } " if HookLog . enabled?
141- else
142- methods . each ( &hook_method )
120+
121+ Array ( hook . method_names ) . each do |method_name |
122+ method_name = method_name . to_sym
123+
124+ hook_method = lambda do |entry |
125+ cls , method = entry
126+ next if config . never_hook? ( cls , method )
127+
128+ hook . package . handler_class . new ( hook . package , cls , method ) . activate
129+ end
130+
131+ methods = [ ]
132+ # irb(main):001:0> Kernel.public_instance_method(:system)
133+ # (irb):1:in `public_instance_method': method `system' for module `Kernel' is private (NameError)
134+ if base_cls == Kernel
135+ methods << [ base_cls , base_cls . instance_method ( method_name ) ] rescue nil
136+ end
137+ methods << [ base_cls , base_cls . public_instance_method ( method_name ) ] rescue nil
138+ methods << [ base_cls , base_cls . protected_instance_method ( method_name ) ] rescue nil
139+ if base_cls . respond_to? ( :singleton_class )
140+ methods << [ base_cls . singleton_class , base_cls . singleton_class . public_instance_method ( method_name ) ] rescue nil
141+ methods << [ base_cls . singleton_class , base_cls . singleton_class . protected_instance_method ( method_name ) ] rescue nil
142+ end
143+ methods . compact!
144+ if methods . empty?
145+ HookLog . load_error [ base_cls . name , method_name ] . join ( '[#.]' ) , "Method #{ method_name } not found on #{ base_cls . name } " if HookLog . enabled?
146+ else
147+ methods . each ( &hook_method )
148+ end
143149 end
144-
145- HookLog . builtin_end class_name , method_name if HookLog . enabled?
146150 end
147151 end
148152 end
@@ -159,17 +163,14 @@ def trace_location(trace_point)
159163
160164 def trace_end ( trace_point )
161165 location = trace_location ( trace_point )
162- begin
163- HookLog . usercode_begin location if HookLog . enabled?
164-
165- return unless @trace_locations . add? ( location )
166-
166+ return unless @trace_locations . add? ( location )
167+ HookLog . on_load location do
167168 path = trace_point . path
168169 enabled = !@notrace_paths . member? ( path ) && config . path_enabled? ( path )
169170 unless enabled
170171 HookLog . log 'Not hooking - path is not enabled' if HookLog . enabled?
171172 @notrace_paths << path
172- return
173+ next
173174 end
174175
175176 cls = trace_point . self
@@ -191,7 +192,7 @@ def trace_end(trace_point)
191192 method = begin
192193 hook_cls . instance_method ( method_id )
193194 rescue NameError
194- HookLog . log "Method #{ hook_cls } #{ fn } is not accessible: #{ $!} " if HookLog . enabled?
195+ HookLog . load_error [ hook_cls , method_id ] . join ( '[#.]' ) , "Method #{ hook_cls } #{ method_id } is not accessible: #{ $!} " if HookLog . enabled?
195196 next
196197 end
197198
@@ -239,8 +240,6 @@ def trace_end(trace_point)
239240 @module_load_times [ location ] += elapsed
240241 end
241242 end
242- ensure
243- HookLog . usercode_end ( location ) if HookLog . enabled?
244243 end
245244 end
246245 end
0 commit comments