Capture stdout/stderr when loading plugins so that it doesn't just

happen
This commit is contained in:
Mitchell Hashimoto 2013-03-01 11:55:06 -08:00
parent 2b37185994
commit b482870173
3 changed files with 36 additions and 3 deletions

View File

@ -42,9 +42,11 @@ if ENV["VAGRANT_LOG"] && ENV["VAGRANT_LOG"] != ""
end
end
require 'pathname'
require 'childprocess'
require 'json'
require 'pathname'
require 'stringio'
require 'childprocess'
require 'i18n'
# OpenSSL must be loaded here since when it is loaded via `autoload`
@ -166,6 +168,12 @@ module Vagrant
#
# @param [String] name Name of the plugin to load.
def self.require_plugin(name)
# Redirect stdout/stderr so that we can output it in our own way.
previous_stderr = $stderr
previous_stdout = $stdout
$stderr = StringIO.new
$stdout = StringIO.new
# Attempt the normal require
begin
require name
@ -189,9 +197,23 @@ module Vagrant
end
end
# Get the string data out from the stdout/stderr captures
stderr = $stderr.string
stdout = $stdout.string
if !stderr.empty? || !stdout.empty?
raise Errors::PluginLoadFailedWithOutput,
:plugin => name,
:stderr => stderr,
:stdout => stdout
end
# And raise an error itself
raise Errors::PluginLoadFailed, :plugin => name
raise Errors::PluginLoadFailed,
:plugin => name
end
ensure
$stderr = previous_stderr
$stdout = previous_stdout
end
end

View File

@ -311,6 +311,10 @@ module Vagrant
error_key(:plugin_load_failed)
end
class PluginLoadFailedWithOutput < VagrantError
error_key(:plugin_load_failed_with_output)
end
class PluginNotFound < VagrantError
error_key(:plugin_not_found)
end

View File

@ -196,6 +196,13 @@ en:
continue to show when you use `plugin install` with a 1.0.x plugin.
plugin_load_failed: |-
Failed to load the "%{plugin}" plugin. View logs for more details.
plugin_load_failed_with_output: |-
Failed to load the "%{plugin}" plugin. The output from loading
the plugin is shown below. View the logs for complete details.
stdout: %{stdout}
stderr: %{stderr}
plugin_not_found: |-
The plugin '%{name}' could not be found. Please install this plugin
prior to attempting to do anything with it.