Permalink
Browse files

Adding new logger to replace verbose method. re-factoring ConfigHash

  • Loading branch information...
1 parent 6e03c3f commit 94c8c90b0e97b2744f476cdd78137e6cfd7e04be @rahmal committed Mar 11, 2010
Showing with 87 additions and 45 deletions.
  1. +1 −0 lib/rconfig.rb
  2. +14 −12 lib/rconfig/config_hash.rb
  3. +41 −0 lib/rconfig/logger.rb
  4. +31 −33 lib/rconfig/rconfig.rb
View
@@ -36,6 +36,7 @@
require 'rconfig/core_ext/object'
require 'rconfig/core_ext/hash'
require 'rconfig/config_hash'
+require 'rconfig/logger'
require 'rconfig/properties_file_parser'
require 'rconfig/exceptions'
require 'rconfig/constants'
@@ -40,7 +40,6 @@ def dup
# This also works inside the composite array keys.
def method_missing(method, *args)
method = method.to_s
- # STDERR.puts "CHF#method_missing(#{method.inspect}, #{args.inspect}) on #{self.inspect}:#{self.class}" if method == 'dup'
value = self[method]
case args.size
when 0
@@ -53,19 +52,22 @@ def method_missing(method, *args)
# e.g.: RConfig.application.method(arg_one, args_two, ...)
value = value[args]
end
- # value = convert_value(value)
- value
- end
- ##
- # Why the &*#^@*^&$ isn't HashWithIndifferentAccess actually doing this?
- def [](key)
- key = key.to_s if key.kind_of?(Symbol)
- super(key)
- end
+ # value = convert_value(value)
+ value
+ end
+
+ ##
+ # Why the &*#^@*^&$ isn't HashWithIndifferentAccess doing this?
+ # HashWithIndifferentAccess doesn't override Hash's []! That's
+ # why it's so destructive!
+ def [](key)
+ key = key.to_s if key.kind_of?(Symbol)
+ super(key)
+ end
- # HashWithIndifferentAccess#default is broken!
- define_method(:default_Hash, Hash.instance_method(:default))
+ # HashWithIndifferentAccess#default is broken!
+ define_method(:default_Hash, Hash.instance_method(:default))
##
# Allow hash.default => hash['default']
View
@@ -0,0 +1,41 @@
+require 'logger'
+
+##
+# Creates a logger for small apps or gems that may not be using log4r.
+# I created this to encapsulate all of the logging code in my smaller
+# projects so thay I don't clutter them up with utility code.
+#
+module DefaultLogger
+
+ module MacroMethods
+
+ def create_logger options = {}
+ class_inheritable_accessor :default_logger
+
+ logger = Logger.new options[:file] ? options[:file] : STDOUT
+ logger.level = options[:level] ? options[:level] : Logger::WARN
+ logger.datetime_format = options[:format] ? options[:format] : "%Y-%m-%d %H:%M:%S"
+ logger.progname = options[:app_name] ? options[:app_name] : nil
+
+ self.default_logger = logger
+
+ include DefaultLogger::InstanceMethods
+ extend DefaultLogger::ClassMethods
+ end
+ end
+
+ module ClassMethods
+ def logger
+ self.default_logger
+ end
+ end
+
+ module InstanceMethods
+ def logger
+ self.class.logger
+ end
+ end
+
+end
+Object.class_eval { extend DefaultLogger::MacroMethods }
+
@@ -60,7 +60,8 @@
#
class RConfig
include Singleton, Constants, ClassVariables
-
+
+ create_logger
##
# Convenience method to initialize necessary fields including,
@@ -161,15 +162,19 @@ def self.config_paths
begin
config_paths = ENV['CONFIG_PATH']
rescue
- verbose_log "Forget something? No config paths! ENV['CONFIG_PATH'] is not set.",
- "Hint: Use config_paths= or set_config_path."
+ logger.error {
+ "Forget something? No config paths! ENV['CONFIG_PATH'] is not set.\n" +
+ "Hint: Use config_paths= or set_config_path."
+ }
end
begin
config_paths = [CONFIG_ROOT]
rescue
- verbose_log "Forget something? No config paths! CONFIG_ROOT is not set.",
- "Hint: Use config_paths= or set_config_path."
+ logger.error {
+ "Forget something? No config paths! CONFIG_ROOT is not set.\n" +
+ "Hint: Use config_paths= or set_config_path."
+ }
end
if @@config_paths.blank?
@@ -247,7 +252,7 @@ def self.suffixes(name)
end
result.freeze
- verbose_log "suffixes(#{name}) => #{result.inspect}"
+ logger.debug {"suffixes(#{name}) => #{result.inspect}"}
result
end
@@ -274,7 +279,7 @@ def self.load_config_files(name, force=false)
# Get array of all the existing files file the config name.
config_files = self.get_config_files(name)
- verbose_log "load_config_files(#{name.inspect})"
+ logger.debug "load_config_files(#{name.inspect})"
# Get all the data from all yaml files into as hashes
hashes = config_files.collect do |f|
@@ -284,7 +289,7 @@ def self.load_config_files(name, force=false)
# it's been loaded before.
val, last_mtime, last_loaded = @@cache[filename]
- verbose_log "f = #{f.inspect}",
+ logger.debug "f = #{f.inspect}",
"cache #{name_x} filename = #{filename.inspect}",
"cache #{name_x} val = #{val.inspect}",
"cache #{name_x} last_mtime = #{last_mtime.inspect}",
@@ -295,21 +300,21 @@ def self.load_config_files(name, force=false)
if val.blank? || (now - last_loaded > @@reload_interval)
if force || val.blank? || mtime != last_mtime
- verbose_log "mtime #{name.inspect} #{filename.inspect} " +
+ logger.debug "mtime #{name.inspect} #{filename.inspect} " +
"changed #{mtime != last_mtime} : #{mtime.inspect} #{last_mtime.inspect}"
# Get contents from config file
File.open(filename) do |f|
- verbose_log "RConfig: loading #{filename.inspect}"
+ logger.debug "RConfig: loading #{filename.inspect}"
val = parse_file(f, ext)
val = val[name] if ext == :xml # xml document must have root tag matching the file name.
- verbose_log "RConfig: loaded #{filename.inspect} => #{val.inspect}"
+ logger.debug "RConfig: loaded #{filename.inspect} => #{val.inspect}"
(@@config_file_loaded ||= { })[name] = config_files
end
# Save cached config file contents, and mtime.
@@cache[filename] = [ val, mtime, now ]
- verbose_log "cache[#{filename.inspect}] = #{@@cache[filename].inspect}"
+ logger.debug "cache[#{filename.inspect}] = #{@@cache[filename].inspect}"
# Flush merged hash cache.
@@cache_hash[name] = nil
@@ -324,7 +329,7 @@ def self.load_config_files(name, force=false)
end
hashes.compact!
- verbose_log "load_config_files(#{name.inspect}) => #{hashes.inspect}"
+ logger.debug "load_config_files(#{name.inspect}) => #{hashes.inspect}"
# Keep last loaded config files around in case @@reload_dsabled.
@@cache_config_files[name] = hashes #unless hashes.empty?
@@ -376,7 +381,7 @@ def self.get_config_files(name)
end
end
- verbose_log "get_config_files(#{name}) => #{files.select{|x| x[3]}.inspect}"
+ logger.debug "get_config_files(#{name}) => #{files.select{|x| x[3]}.inspect}"
files
end
@@ -397,7 +402,7 @@ def self.config_files(name)
# Returns true if any files for config have changes since
# last load.
def self.config_changed?(name)
- verbose_log "config_changed?(#{name.inspect})"
+ logger.debug "config_changed?(#{name.inspect})"
name = name.to_s # if name.is_a?(Symbol)
! (@@cache_files[name] === get_config_files(name))
end
@@ -409,7 +414,7 @@ def self.config_changed?(name)
# from all config files for a name.
#
def self.config_hash(name)
- verbose_log "config_hash(#{name.inspect})"
+ logger.debug "config_hash(#{name.inspect})"
name = name.to_s
unless result = @@cache_hash[name]
@@ -419,7 +424,7 @@ def self.config_hash(name)
load_config_files(name)
)
)
- verbose_log "config_hash(#{name.inspect}): reloaded"
+ logger.debug "config_hash(#{name.inspect}): reloaded"
end
result
@@ -444,14 +449,14 @@ def self.check_config_changed(name = nil)
end
end
- verbose_log "check_config_changed(#{name.inspect}) => #{changed.inspect}"
+ logger.debug "check_config_changed(#{name.inspect}) => #{changed.inspect}"
changed.empty? ? nil : changed
end
def self.config_has_changed?(name)
- verbose_log "config_has_changed?(#{name.inspect}), reload_disabled=#{@@reload_disabled}"
+ logger.debug "config_has_changed?(#{name.inspect}), reload_disabled=#{@@reload_disabled}"
changed = false
@@ -491,7 +496,7 @@ def self.make_indifferent(x)
end
x = ConfigHash.new.merge!(x).freeze
end
- verbose_log "make_indefferent: x = #{x.inspect}:#{x.class}"
+ logger.debug "make_indefferent: x = #{x.inspect}:#{x.class}"
when Array
unless x.frozen?
x.collect! do | v |
@@ -545,9 +550,9 @@ def self.[](key, file=:application)
# Get the value specified by the args, in the file specified by th name
#
def self.with_file(name, *args)
- # verbose_log "with_file(#{name.inspect}, #{args.inspect})"; result =
+ # logger.debug "with_file(#{name.inspect}, #{args.inspect})"; result =
args.inject(get_config_file(name)) { | v, i |
- # verbose_log "v = #{v.inspect}, i = #{i.inspect}"
+ # logger.debug "v = #{v.inspect}, i = #{i.inspect}"
case v
when Hash
v[i.to_s]
@@ -557,7 +562,7 @@ def self.with_file(name, *args)
nil
end
}
- # verbose_log "with_file(#{name.inspect}, #{args.inspect}) => #{result.inspect}"; result
+ # logger.debug "with_file(#{name.inspect}, #{args.inspect}) => #{result.inspect}"; result
end
@@ -576,7 +581,7 @@ def self.get_config_file(name)
result = config_hash(name)
- verbose_log "get_config_file(#{name.inspect}) => #{result.inspect}"
+ logger.debug "get_config_file(#{name.inspect}) => #{result.inspect}"
result
end
@@ -689,7 +694,7 @@ def self.create_dottable_hash(value)
#
def self.method_missing(method, *args)
value = with_file(method, *args)
- verbose_log "#{self}.method_missing(#{method.inspect}, #{args.inspect}) => #{value.inspect}"
+ logger.debug "#{self}.method_missing(#{method.inspect}, #{args.inspect}) => #{value.inspect}"
value
end
@@ -766,7 +771,7 @@ def self.fire_on_load(name)
(@@on_load['ANY'] || EMPTY_ARRAY) +
(@@on_load[name] || EMPTY_ARRAY)
callbacks.uniq!
- verbose_log "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
+ logger.debug "fire_on_load(#{name.inspect}): callbacks[#{callbacks.inspect}]" unless callbacks.empty?
callbacks.each{|cb| cb.call()}
end
@@ -791,11 +796,4 @@ def self.filename_for_name(name, dir = config_paths[0], ext = :yml)
File.join(dir, "#{name}.#{ext}")
end
-
- ##
- # Helper method for logging verbose messages.
- def self.verbose_log *args
- $stderr.puts(args.join("\n")) if @@verbose
- end
-
end # class RConfig

0 comments on commit 94c8c90

Please sign in to comment.