From 061bdad68a823b236b7de0322012f7af36a633cb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 19 Jul 2010 21:46:49 -0700 Subject: [PATCH] Move ResourceLogger to the Util namespace since thats more of what it is --- lib/vagrant/environment.rb | 2 +- lib/vagrant/resource_logger.rb | 126 ----------------- lib/vagrant/util/resource_logger.rb | 128 ++++++++++++++++++ .../{ => util}/resource_logger_test.rb | 4 +- vagrant.gemspec | 6 +- 5 files changed, 134 insertions(+), 132 deletions(-) delete mode 100644 lib/vagrant/resource_logger.rb create mode 100644 lib/vagrant/util/resource_logger.rb rename test/vagrant/{ => util}/resource_logger_test.rb (97%) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c001756ed..c70084b1c 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -220,7 +220,7 @@ module Vagrant # the logger will just send the log data to a black hole. def load_logger! resource = vm_name || "vagrant" - @logger = ResourceLogger.new(resource, self) + @logger = Util::ResourceLogger.new(resource, self) end # Loads the home directory path and creates the necessary subdirectories diff --git a/lib/vagrant/resource_logger.rb b/lib/vagrant/resource_logger.rb deleted file mode 100644 index 82b37633c..000000000 --- a/lib/vagrant/resource_logger.rb +++ /dev/null @@ -1,126 +0,0 @@ -require 'thread' - -module Vagrant - # Represents a logger for a specific resource within Vagrant. Each - # logger should be initialized and set to represent a single - # resource. Each logged message will then appear in the following - # format: - # - # [resource] message - # - # This class is thread safe. The backing class which actually does - # all the logging IO is protected. - # - # This class also handles progress meters of multiple resources and - # handles all the proper interleaving and console updating to - # display the progress meters in a way which doesn't conflict with - # other incoming log messages. - class ResourceLogger - @@singleton_logger = nil - @@progress_reporters = nil - @@writer_lock = Mutex.new - - # The resource which this logger represents. - attr_reader :resource - - # The environment that this logger is part of - attr_reader :env - - # The backing logger which actually handles the IO. This logger - # should be a subclass of the standard library Logger, in general. - # IMPORTANT: This logger must be thread-safe. - attr_reader :logger - - class << self - # Returns a singleton logger. If one has not yet be - # instantiated, then the given environment will be used to - # create a new logger. - def singleton_logger(env=nil) - if env && env.config && env.config.loaded? - @@singleton_logger ||= Util::PlainLogger.new(env.config.vagrant.log_output) - else - Util::PlainLogger.new(nil) - end - end - - # Resets the singleton logger (only used for testing). - def reset_singleton_logger! - @@singleton_logger = nil - end - - # Returns the progress parts array which contains the various - # progress reporters. - def progress_reporters - @@progress_reporters ||= {} - end - end - - def initialize(resource, env) - @resource = resource - @env = env - @logger = self.class.singleton_logger(env) - end - - [:debug, :info, :error, :fatal].each do |method| - define_method(method) do |message| - @@writer_lock.synchronize do - # We clear the line in case progress reports have been going - # out. - print(cl_reset) - logger.send(method, "[#{resource}] #{message}") - end - - # Once again flush the progress reporters since we probably - # cleared any existing ones. - flush_progress - end - end - - # Sets a progress report for the resource that this logger - # represents. This progress report is interleaved within the output. - def report_progress(progress, total, show_parts=true) - # Simply add the progress reporter to the list of progress - # reporters - self.class.progress_reporters[resource] = { - :progress => progress, - :total => total, - :show_parts => show_parts - } - - # And force an update to occur - flush_progress - end - - # Clears the progress report for this resource - def clear_progress - self.class.progress_reporters.delete(resource) - end - - def flush_progress - # Don't do anything if there are no progress reporters - return if self.class.progress_reporters.length <= 0 - - @@writer_lock.synchronize do - reports = [] - - # First generate all the report percentages and output - self.class.progress_reporters.each do |name, data| - percent = (data[:progress].to_f / data[:total].to_f) * 100 - line = "#{name}: #{percent.to_i}%" - line << " (#{data[:progress]} / #{data[:total]})" if data[:show_parts] - reports << line - end - - # Output it to stdout - print "#{cl_reset}[progress] #{reports.join(" ")}" - $stdout.flush - end - end - - def cl_reset - reset = "\r" - reset += "\e[0K" unless Mario::Platform.windows? - reset - end - end -end diff --git a/lib/vagrant/util/resource_logger.rb b/lib/vagrant/util/resource_logger.rb new file mode 100644 index 000000000..43941d83a --- /dev/null +++ b/lib/vagrant/util/resource_logger.rb @@ -0,0 +1,128 @@ +require 'thread' + +module Vagrant + module Util + # Represents a logger for a specific resource within Vagrant. Each + # logger should be initialized and set to represent a single + # resource. Each logged message will then appear in the following + # format: + # + # [resource] message + # + # This class is thread safe. The backing class which actually does + # all the logging IO is protected. + # + # This class also handles progress meters of multiple resources and + # handles all the proper interleaving and console updating to + # display the progress meters in a way which doesn't conflict with + # other incoming log messages. + class ResourceLogger + @@singleton_logger = nil + @@progress_reporters = nil + @@writer_lock = Mutex.new + + # The resource which this logger represents. + attr_reader :resource + + # The environment that this logger is part of + attr_reader :env + + # The backing logger which actually handles the IO. This logger + # should be a subclass of the standard library Logger, in general. + # IMPORTANT: This logger must be thread-safe. + attr_reader :logger + + class << self + # Returns a singleton logger. If one has not yet be + # instantiated, then the given environment will be used to + # create a new logger. + def singleton_logger(env=nil) + if env && env.config && env.config.loaded? + @@singleton_logger ||= PlainLogger.new(env.config.vagrant.log_output) + else + PlainLogger.new(nil) + end + end + + # Resets the singleton logger (only used for testing). + def reset_singleton_logger! + @@singleton_logger = nil + end + + # Returns the progress parts array which contains the various + # progress reporters. + def progress_reporters + @@progress_reporters ||= {} + end + end + + def initialize(resource, env) + @resource = resource + @env = env + @logger = self.class.singleton_logger(env) + end + + [:debug, :info, :error, :fatal].each do |method| + define_method(method) do |message| + @@writer_lock.synchronize do + # We clear the line in case progress reports have been going + # out. + print(cl_reset) + logger.send(method, "[#{resource}] #{message}") + end + + # Once again flush the progress reporters since we probably + # cleared any existing ones. + flush_progress + end + end + + # Sets a progress report for the resource that this logger + # represents. This progress report is interleaved within the output. + def report_progress(progress, total, show_parts=true) + # Simply add the progress reporter to the list of progress + # reporters + self.class.progress_reporters[resource] = { + :progress => progress, + :total => total, + :show_parts => show_parts + } + + # And force an update to occur + flush_progress + end + + # Clears the progress report for this resource + def clear_progress + self.class.progress_reporters.delete(resource) + end + + def flush_progress + # Don't do anything if there are no progress reporters + return if self.class.progress_reporters.length <= 0 + + @@writer_lock.synchronize do + reports = [] + + # First generate all the report percentages and output + self.class.progress_reporters.each do |name, data| + percent = (data[:progress].to_f / data[:total].to_f) * 100 + line = "#{name}: #{percent.to_i}%" + line << " (#{data[:progress]} / #{data[:total]})" if data[:show_parts] + reports << line + end + + # Output it to stdout + print "#{cl_reset}[progress] #{reports.join(" ")}" + $stdout.flush + end + end + + def cl_reset + reset = "\r" + reset += "\e[0K" unless Mario::Platform.windows? + reset + end + end + end +end diff --git a/test/vagrant/resource_logger_test.rb b/test/vagrant/util/resource_logger_test.rb similarity index 97% rename from test/vagrant/resource_logger_test.rb rename to test/vagrant/util/resource_logger_test.rb index 66139cf09..dec8307d6 100644 --- a/test/vagrant/resource_logger_test.rb +++ b/test/vagrant/util/resource_logger_test.rb @@ -1,8 +1,8 @@ require "test_helper" -class ResourceLoggerTest < Test::Unit::TestCase +class ResourceLoggerUtilTest < Test::Unit::TestCase setup do - @klass = Vagrant::ResourceLogger + @klass = Vagrant::Util::ResourceLogger end context "singleton logger" do diff --git a/vagrant.gemspec b/vagrant.gemspec index 2f7e8bbfe..32fff1e4d 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -101,7 +101,6 @@ Gem::Specification.new do |s| "lib/vagrant/provisioners/chef.rb", "lib/vagrant/provisioners/chef_server.rb", "lib/vagrant/provisioners/chef_solo.rb", - "lib/vagrant/resource_logger.rb", "lib/vagrant/ssh.rb", "lib/vagrant/systems/base.rb", "lib/vagrant/systems/linux.rb", @@ -110,6 +109,7 @@ Gem::Specification.new do |s| "lib/vagrant/util/glob_loader.rb", "lib/vagrant/util/plain_logger.rb", "lib/vagrant/util/platform.rb", + "lib/vagrant/util/resource_logger.rb", "lib/vagrant/util/stacked_proc_runner.rb", "lib/vagrant/util/template_renderer.rb", "lib/vagrant/util/translator.rb", @@ -191,13 +191,13 @@ Gem::Specification.new do |s| "test/vagrant/provisioners/chef_server_test.rb", "test/vagrant/provisioners/chef_solo_test.rb", "test/vagrant/provisioners/chef_test.rb", - "test/vagrant/resource_logger_test.rb", "test/vagrant/ssh_session_test.rb", "test/vagrant/ssh_test.rb", "test/vagrant/systems/linux_test.rb", "test/vagrant/util/busy_test.rb", "test/vagrant/util/plain_logger_test.rb", "test/vagrant/util/platform_test.rb", + "test/vagrant/util/resource_logger_test.rb", "test/vagrant/util/stacked_proc_runner_test.rb", "test/vagrant/util/template_renderer_test.rb", "test/vagrant/util/translator_test.rb", @@ -277,13 +277,13 @@ Gem::Specification.new do |s| "test/vagrant/provisioners/chef_server_test.rb", "test/vagrant/provisioners/chef_solo_test.rb", "test/vagrant/provisioners/chef_test.rb", - "test/vagrant/resource_logger_test.rb", "test/vagrant/ssh_session_test.rb", "test/vagrant/ssh_test.rb", "test/vagrant/systems/linux_test.rb", "test/vagrant/util/busy_test.rb", "test/vagrant/util/plain_logger_test.rb", "test/vagrant/util/platform_test.rb", + "test/vagrant/util/resource_logger_test.rb", "test/vagrant/util/stacked_proc_runner_test.rb", "test/vagrant/util/template_renderer_test.rb", "test/vagrant/util/translator_test.rb",