From e3ff9c7ac3f91593eb5a19f8578d2671f2bc7675 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 20 Sep 2010 09:58:19 -0600 Subject: [PATCH] Resource logger now logs to a "logs" directory in the home path --- config/default.rb | 1 - lib/vagrant/config/vagrant.rb | 1 - lib/vagrant/environment.rb | 7 ++++++- lib/vagrant/test_helpers.rb | 1 - lib/vagrant/util/resource_logger.rb | 7 ++++++- test/vagrant/util/resource_logger_test.rb | 18 +++++++++--------- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/config/default.rb b/config/default.rb index 6d713476a..fb65e539b 100644 --- a/config/default.rb +++ b/config/default.rb @@ -1,6 +1,5 @@ Vagrant::Config.run do |config| # default config goes here - config.vagrant.log_output = STDOUT config.vagrant.dotfile_name = ".vagrant" config.vagrant.home = "~/.vagrant" config.vagrant.host = :detect diff --git a/lib/vagrant/config/vagrant.rb b/lib/vagrant/config/vagrant.rb index e06ef85dc..45cd0e023 100644 --- a/lib/vagrant/config/vagrant.rb +++ b/lib/vagrant/config/vagrant.rb @@ -4,7 +4,6 @@ module Vagrant configures :vagrant attr_accessor :dotfile_name - attr_accessor :log_output attr_accessor :home attr_accessor :host diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 2aeadd1e4..30f2c2724 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -7,7 +7,7 @@ module Vagrant # storing references to the various instances. class Environment ROOTFILE_NAME = "Vagrantfile" - HOME_SUBDIRS = ["tmp", "boxes"] + HOME_SUBDIRS = ["tmp", "boxes", "logs"] DEFAULT_VM = :default attr_reader :parent # Parent environment (in the case of multi-VMs) @@ -73,6 +73,11 @@ module Vagrant home_path.join("boxes") end + # Path to the Vagrant logs directory + def log_path + home_path.join("logs") + end + # Returns the name of the resource which this environment represents. # The resource is the VM name if there is a VM it represents, otherwise # it defaults to "vagrant" diff --git a/lib/vagrant/test_helpers.rb b/lib/vagrant/test_helpers.rb index f89981fb8..96c1e8c65 100644 --- a/lib/vagrant/test_helpers.rb +++ b/lib/vagrant/test_helpers.rb @@ -21,7 +21,6 @@ module Vagrant str = args.shift || "" File.open(path.to_s, "w") do |f| f.puts "Vagrant::Config.run do |config|" - f.puts "config.vagrant.log_output = nil" f.puts "config.vagrant.home = '#{home_path}'" f.puts str f.puts "end" diff --git a/lib/vagrant/util/resource_logger.rb b/lib/vagrant/util/resource_logger.rb index ba3a75396..c1cc0439e 100644 --- a/lib/vagrant/util/resource_logger.rb +++ b/lib/vagrant/util/resource_logger.rb @@ -31,7 +31,12 @@ module Vagrant # instantiated, then the given environment will be used to # create a new logger. def singleton_logger(env=nil) - @@singleton_logger ||= PlainLogger.new(env.config.vagrant.log_output) + return PlainLogger.new(nil) if !env.loaded? + + @@singleton_logger ||= begin + file = env.log_path.join("#{Time.now.to_i}.log") + PlainLogger.new(file) + end end # Resets the singleton logger (only used for testing). diff --git a/test/vagrant/util/resource_logger_test.rb b/test/vagrant/util/resource_logger_test.rb index 32a37a12e..7d4dcc3ea 100644 --- a/test/vagrant/util/resource_logger_test.rb +++ b/test/vagrant/util/resource_logger_test.rb @@ -12,29 +12,29 @@ class ResourceLoggerUtilTest < Test::Unit::TestCase @result = mock("result") end - should "return a nil plain logger if the config is not loaded" do + should "return a nil plain logger if the environment is not loaded" do env = vagrant_env - env.config.stubs(:loaded?).returns(false) + env.stubs(:loaded?).returns(false) Vagrant::Util::PlainLogger.expects(:new).with(nil).returns(@result) assert_equal @result, @klass.singleton_logger(env) end - should "return a logger with the specified output if environment is ready" do - output = mock("output") + should "return a logger with the output file set if environment is ready" do env = vagrant_env - env.config.vagrant.log_output = output - Vagrant::Util::PlainLogger.expects(:new).with(output).returns(@result) + Vagrant::Util::PlainLogger.expects(:new).returns(@result).with() do |path| + assert path.to_s =~ /logs/ + true + end + assert_equal @result, @klass.singleton_logger(env) end should "only load the logger once" do - output = mock("output") env = vagrant_env - env.config.vagrant.log_output = output - Vagrant::Util::PlainLogger.expects(:new).with(output).returns(@result) + Vagrant::Util::PlainLogger.expects(:new).with(anything).returns(@result) assert_equal @result, @klass.singleton_logger(env) assert_equal @result, @klass.singleton_logger(env) assert_equal @result, @klass.singleton_logger(env)