Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4754

Show
Ignore:
Timestamp:
08/13/06 03:29:04 (2 years ago)
Author:
ulysses
Message:

Add debugging logging to Dependencies.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r4728 r4754  
    11*SVN* 
    22 
    3 <<<<<<< .mine 
     3* Add debugging logging to Dependencies. Currently can be enabled with Dependencies.log_activity = true; adding to Initializer and documenting is forthcoming. [Nicholas Seckar] 
     4 
    45* Replace Reloadable with improvements to the Dependencies mechanism. [Nicholas Seckar] 
    56 
    6 ======= 
    77* DateTime#to_time gives hour/minute/second resolution.  #5747 [jon.evans@pobox.com] 
    88 
     
    1313    attr_internal :foo 
    1414 
    15 >>>>>>> .r4727 
    1615* Raise fully qualified names upon name errors. #5533 [lars@pinds.com, Nicholas Seckar] 
    1716 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r4730 r4754  
    2727  self.autoload_paths = [] 
    2828 
     29  # An array of qualified constant names that have been loaded. Adding a name to 
     30  # this array will cause it to be unloaded the next time Dependencies are cleared. 
    2931  mattr_accessor :autoloaded_constants 
    3032  self.autoloaded_constants = [] 
     33   
     34  # Set to true to enable logging of const_missing and file loads 
     35  mattr_accessor :log_activity 
     36  self.log_activity = false 
    3137   
    3238  def load? 
     
    4652 
    4753  def clear 
     54    log_call 
    4855    loaded.clear 
    4956    remove_autoloaded_constants! 
     
    5158 
    5259  def require_or_load(file_name, const_path = nil) 
     60    log_call file_name, const_path 
    5361    file_name = $1 if file_name =~ /^(.*)\.rb$/ 
    5462    expanded = File.expand_path(file_name) 
     
    6068 
    6169    if load? 
     70      log "loading #{file_name}" 
    6271      begin 
    6372        # Enable warnings iff this file has not been loaded before and 
     
    7685      end 
    7786    else 
     87      log "requiring #{file_name}" 
    7888      require file_name 
    7989    end 
     
    133143  # +autoloadable_constants_for_path+ for more details. 
    134144  def load_file(path, const_paths = autoloadable_constants_for_path(path)) 
     145    log_call path, const_paths 
     146     
    135147    const_paths = [const_paths].compact unless const_paths.is_a? Array 
    136148    undefined_before = const_paths.reject(&method(:qualified_const_defined?)) 
     
    138150    load path 
    139151     
    140     autoloaded_constants.concat const_paths.select(&method(:qualified_const_defined?)) 
     152    newly_defined_paths = const_paths.select(&method(:qualified_const_defined?)) 
     153    autoloaded_constants.concat newly_defined_paths 
    141154    autoloaded_constants.uniq! 
     155    log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty? 
    142156  end 
    143157   
     
    152166  # using const_missing. 
    153167  def load_missing_constant(from_mod, const_name) 
     168    log_call from_mod, const_name 
     169     
    154170    qualified_name = qualified_name_for from_mod, const_name 
    155171    path_suffix = qualified_name.underscore 
     
    194210        parent = (names[0..-2] * '::').constantize 
    195211      end 
     212      log "removing constant #{const}" 
    196213      parent.send :remove_const, names.last 
    197214      true 
     
    227244      when Module then desc.name 
    228245      else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" 
     246    end 
     247  end 
     248   
     249  def log_call(*args) 
     250    arg_str = args.collect(&:inspect) * ', ' 
     251    /in `([a-z_\?\!]+)'/ =~ caller(1).first 
     252    selector = $1 || '<unknown>'  
     253    log "called #{selector}(#{arg_str})" 
     254  end 
     255   
     256  def log(msg) 
     257    if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity 
     258      RAILS_DEFAULT_LOGGER.debug "Dependencies: #{msg}" 
    229259    end 
    230260  end