Move ResourceLogger to the Util namespace since thats more of what it is
This commit is contained in:
parent
8e82fb2691
commit
061bdad68a
|
@ -220,7 +220,7 @@ module Vagrant
|
||||||
# the logger will just send the log data to a black hole.
|
# the logger will just send the log data to a black hole.
|
||||||
def load_logger!
|
def load_logger!
|
||||||
resource = vm_name || "vagrant"
|
resource = vm_name || "vagrant"
|
||||||
@logger = ResourceLogger.new(resource, self)
|
@logger = Util::ResourceLogger.new(resource, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Loads the home directory path and creates the necessary subdirectories
|
# Loads the home directory path and creates the necessary subdirectories
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
@ -1,8 +1,8 @@
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class ResourceLoggerTest < Test::Unit::TestCase
|
class ResourceLoggerUtilTest < Test::Unit::TestCase
|
||||||
setup do
|
setup do
|
||||||
@klass = Vagrant::ResourceLogger
|
@klass = Vagrant::Util::ResourceLogger
|
||||||
end
|
end
|
||||||
|
|
||||||
context "singleton logger" do
|
context "singleton logger" do
|
|
@ -101,7 +101,6 @@ Gem::Specification.new do |s|
|
||||||
"lib/vagrant/provisioners/chef.rb",
|
"lib/vagrant/provisioners/chef.rb",
|
||||||
"lib/vagrant/provisioners/chef_server.rb",
|
"lib/vagrant/provisioners/chef_server.rb",
|
||||||
"lib/vagrant/provisioners/chef_solo.rb",
|
"lib/vagrant/provisioners/chef_solo.rb",
|
||||||
"lib/vagrant/resource_logger.rb",
|
|
||||||
"lib/vagrant/ssh.rb",
|
"lib/vagrant/ssh.rb",
|
||||||
"lib/vagrant/systems/base.rb",
|
"lib/vagrant/systems/base.rb",
|
||||||
"lib/vagrant/systems/linux.rb",
|
"lib/vagrant/systems/linux.rb",
|
||||||
|
@ -110,6 +109,7 @@ Gem::Specification.new do |s|
|
||||||
"lib/vagrant/util/glob_loader.rb",
|
"lib/vagrant/util/glob_loader.rb",
|
||||||
"lib/vagrant/util/plain_logger.rb",
|
"lib/vagrant/util/plain_logger.rb",
|
||||||
"lib/vagrant/util/platform.rb",
|
"lib/vagrant/util/platform.rb",
|
||||||
|
"lib/vagrant/util/resource_logger.rb",
|
||||||
"lib/vagrant/util/stacked_proc_runner.rb",
|
"lib/vagrant/util/stacked_proc_runner.rb",
|
||||||
"lib/vagrant/util/template_renderer.rb",
|
"lib/vagrant/util/template_renderer.rb",
|
||||||
"lib/vagrant/util/translator.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_server_test.rb",
|
||||||
"test/vagrant/provisioners/chef_solo_test.rb",
|
"test/vagrant/provisioners/chef_solo_test.rb",
|
||||||
"test/vagrant/provisioners/chef_test.rb",
|
"test/vagrant/provisioners/chef_test.rb",
|
||||||
"test/vagrant/resource_logger_test.rb",
|
|
||||||
"test/vagrant/ssh_session_test.rb",
|
"test/vagrant/ssh_session_test.rb",
|
||||||
"test/vagrant/ssh_test.rb",
|
"test/vagrant/ssh_test.rb",
|
||||||
"test/vagrant/systems/linux_test.rb",
|
"test/vagrant/systems/linux_test.rb",
|
||||||
"test/vagrant/util/busy_test.rb",
|
"test/vagrant/util/busy_test.rb",
|
||||||
"test/vagrant/util/plain_logger_test.rb",
|
"test/vagrant/util/plain_logger_test.rb",
|
||||||
"test/vagrant/util/platform_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/stacked_proc_runner_test.rb",
|
||||||
"test/vagrant/util/template_renderer_test.rb",
|
"test/vagrant/util/template_renderer_test.rb",
|
||||||
"test/vagrant/util/translator_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_server_test.rb",
|
||||||
"test/vagrant/provisioners/chef_solo_test.rb",
|
"test/vagrant/provisioners/chef_solo_test.rb",
|
||||||
"test/vagrant/provisioners/chef_test.rb",
|
"test/vagrant/provisioners/chef_test.rb",
|
||||||
"test/vagrant/resource_logger_test.rb",
|
|
||||||
"test/vagrant/ssh_session_test.rb",
|
"test/vagrant/ssh_session_test.rb",
|
||||||
"test/vagrant/ssh_test.rb",
|
"test/vagrant/ssh_test.rb",
|
||||||
"test/vagrant/systems/linux_test.rb",
|
"test/vagrant/systems/linux_test.rb",
|
||||||
"test/vagrant/util/busy_test.rb",
|
"test/vagrant/util/busy_test.rb",
|
||||||
"test/vagrant/util/plain_logger_test.rb",
|
"test/vagrant/util/plain_logger_test.rb",
|
||||||
"test/vagrant/util/platform_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/stacked_proc_runner_test.rb",
|
||||||
"test/vagrant/util/template_renderer_test.rb",
|
"test/vagrant/util/template_renderer_test.rb",
|
||||||
"test/vagrant/util/translator_test.rb",
|
"test/vagrant/util/translator_test.rb",
|
||||||
|
|
Loading…
Reference in New Issue