Logger now uses singleton on the Vagrant::Logger class. Tests included.

This commit is contained in:
Mitchell Hashimoto 2010-02-10 22:12:34 -08:00
parent 39dd78c76f
commit e5903129e1
2 changed files with 56 additions and 13 deletions

View File

@ -12,16 +12,28 @@ error
end end
def logger def logger
# TODO: Buffer messages until config is loaded, then output them? Logger.singleton_logger
if Vagrant.config.loaded?
@logger ||= Vagrant::Logger.new(Vagrant.config.vagrant.log_output)
else
Vagrant::Logger.new(nil)
end
end end
end end
class Logger < ::Logger class Logger < ::Logger
@@singleton_logger = nil
class <<self
def singleton_logger
# TODO: Buffer messages until config is loaded, then output them?
if Vagrant.config.loaded?
@@singleton_logger ||= Vagrant::Logger.new(Vagrant.config.vagrant.log_output)
else
Vagrant::Logger.new(nil)
end
end
def reset_logger!
@@singleton_logger = nil
end
end
def format_message(level, time, progname, msg) def format_message(level, time, progname, msg)
"[#{level} #{time.strftime('%m-%d-%Y %X')}] Vagrant: #{msg}\n" "[#{level} #{time.strftime('%m-%d-%Y %X')}] Vagrant: #{msg}\n"
end end

View File

@ -11,23 +11,54 @@ class UtilTest < Test::Unit::TestCase
end end
context "logger" do context "logger" do
class OtherUtil
extend Vagrant::Util
end
setup do setup do
@config = Vagrant::Config::Top.new @config = Vagrant::Config::Top.new
@config.stubs(:loaded?).returns(true)
@config.vagrant.log_output = STDOUT
Vagrant::Config.stubs(:config).returns(@config) Vagrant::Config.stubs(:config).returns(@config)
Vagrant::Logger.reset_logger!
end
teardown do
Vagrant::Logger.reset_logger!
end end
should "return a logger to nil if config is not loaded" do should "return a logger to nil if config is not loaded" do
@config.expects(:loaded?).returns(false) @config.expects(:loaded?).returns(false)
Vagrant::Logger.expects(:new).with(nil).once.returns("foo") logger = RegUtil.logger
assert_equal "foo", RegUtil.logger assert_nil logger.instance_variable_get(:@logdev)
end end
should "return a logger using the configured output" do should "return a logger using the configured output" do
@config.stubs(:loaded?).returns(true) logger = RegUtil.logger
@config.vagrant.log_output = "foo" logdev = logger.instance_variable_get(:@logdev)
Vagrant::Logger.expects(:new).once.with("foo").returns("bar") assert logger
assert_equal "bar", RegUtil.logger assert !logdev.nil?
assert_equal "bar", RegUtil.logger assert_equal STDOUT, logdev.dev
end
should "only instantiate a logger once" do
Vagrant::Logger.expects(:new).once.returns("GOOD")
RegUtil.logger
RegUtil.logger
end
should "be able to reset the logger" do
Vagrant::Logger.expects(:new).twice
RegUtil.logger
Vagrant::Logger.reset_logger!
RegUtil.logger
end
should "return the same logger across classes" do
logger = RegUtil.logger
other = OtherUtil.logger
assert logger.equal?(other)
end end
end end
end end