forked from hashicorp/vagrant
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
safe_puts to avoid EPIPE [hashicorpGH-819]
- Loading branch information
Showing
6 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Vagrant | ||
module Util | ||
# This module provides a `safe_puts` method which outputs to | ||
# the given IO object, and rescues any broken pipe errors and | ||
# ignores them. This is useful in cases where you're outputting | ||
# to stdout, for example, and the stdout is closed, but you want to | ||
# keep running. | ||
module SafePuts | ||
# Uses `puts` on the given IO object and safely ignores any | ||
# Errno::EPIPE. | ||
# | ||
# @param [String] message Message to output. | ||
# @param [Hash] opts Options hash. | ||
def safe_puts(message=nil, opts=nil) | ||
message ||= "" | ||
opts = { | ||
:io => $stdout, | ||
:printer => :puts | ||
}.merge(opts || {}) | ||
|
||
begin | ||
opts[:io].send(opts[:printer], message) | ||
rescue Errno::EPIPE | ||
# This is what makes this a `safe` puts. | ||
return | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|