Skip to content

Commit

Permalink
Add debugging logging to Dependencies.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4754 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
seckar committed Aug 13, 2006
1 parent 3340674 commit 440655e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
5 changes: 2 additions & 3 deletions activesupport/CHANGELOG
@@ -1,9 +1,9 @@
*SVN*

<<<<<<< .mine
* Add debugging logging to Dependencies. Currently can be enabled with Dependencies.log_activity = true; adding to Initializer and documenting is forthcoming. [Nicholas Seckar]

* Replace Reloadable with improvements to the Dependencies mechanism. [Nicholas Seckar]

=======
* DateTime#to_time gives hour/minute/second resolution. #5747 [jon.evans@pobox.com]

* attr_internal to support namespacing and deprecation. Like attr_* except backed by internally-named instance variable. Set attr_internal_naming_format to change the format from the default '@_%s'. [Jeremy Kemper]
Expand All @@ -12,7 +12,6 @@
self.attr_internal_naming_format = '@%s__rofl'
attr_internal :foo

>>>>>>> .r4727
* Raise fully qualified names upon name errors. #5533 [lars@pinds.com, Nicholas Seckar]

* Add extention to obtain the missing constant from NameError instances. [Nicholas Seckar]
Expand Down
32 changes: 31 additions & 1 deletion activesupport/lib/active_support/dependencies.rb
Expand Up @@ -26,9 +26,15 @@ module Dependencies #:nodoc:
mattr_accessor :autoload_paths
self.autoload_paths = []

# An array of qualified constant names that have been loaded. Adding a name to
# this array will cause it to be unloaded the next time Dependencies are cleared.
mattr_accessor :autoloaded_constants
self.autoloaded_constants = []

# Set to true to enable logging of const_missing and file loads
mattr_accessor :log_activity
self.log_activity = false

def load?
mechanism == :load
end
Expand All @@ -45,11 +51,13 @@ def associate_with(file_name)
end

def clear
log_call
loaded.clear
remove_autoloaded_constants!
end

def require_or_load(file_name, const_path = nil)
log_call file_name, const_path
file_name = $1 if file_name =~ /^(.*)\.rb$/
expanded = File.expand_path(file_name)
return if loaded.include?(expanded)
Expand All @@ -59,6 +67,7 @@ def require_or_load(file_name, const_path = nil)
loaded << expanded

if load?
log "loading #{file_name}"
begin
# Enable warnings iff this file has not been loaded before and
# warnings_on_first_load is set.
Expand All @@ -75,6 +84,7 @@ def require_or_load(file_name, const_path = nil)
raise
end
else
log "requiring #{file_name}"
require file_name
end

Expand Down Expand Up @@ -132,13 +142,17 @@ def autoloadable_module?(path_suffix)
# of names that the file at +path+ may define. See
# +autoloadable_constants_for_path+ for more details.
def load_file(path, const_paths = autoloadable_constants_for_path(path))
log_call path, const_paths

const_paths = [const_paths].compact unless const_paths.is_a? Array
undefined_before = const_paths.reject(&method(:qualified_const_defined?))

load path

autoloaded_constants.concat const_paths.select(&method(:qualified_const_defined?))
newly_defined_paths = const_paths.select(&method(:qualified_const_defined?))
autoloaded_constants.concat newly_defined_paths
autoloaded_constants.uniq!
log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty?
end

# Return the constant path for the provided parent and constant name.
Expand All @@ -151,6 +165,8 @@ def qualified_name_for(mod, name)
# it is not possible to laod the constant into from_mod, try its parent module
# using const_missing.
def load_missing_constant(from_mod, const_name)
log_call from_mod, const_name

qualified_name = qualified_name_for from_mod, const_name
path_suffix = qualified_name.underscore
name_error = NameError.new("uninitialized constant #{qualified_name}")
Expand Down Expand Up @@ -193,6 +209,7 @@ def remove_autoloaded_constants!
else
parent = (names[0..-2] * '::').constantize
end
log "removing constant #{const}"
parent.send :remove_const, names.last
true
end
Expand Down Expand Up @@ -229,6 +246,19 @@ def to_constant_name(desc)
end
end

def log_call(*args)
arg_str = args.collect(&:inspect) * ', '
/in `([a-z_\?\!]+)'/ =~ caller(1).first
selector = $1 || '<unknown>'
log "called #{selector}(#{arg_str})"
end

def log(msg)
if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity
RAILS_DEFAULT_LOGGER.debug "Dependencies: #{msg}"
end
end

end

Object.send(:define_method, :require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load)
Expand Down

0 comments on commit 440655e

Please sign in to comment.