module Vagrant::Util::SafePuts

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.

Public Instance Methods

safe_puts(message=nil, opts=nil) click to toggle source

Uses ‘puts` on the given IO object and safely ignores any Errno::EPIPE.

@param [String] message Message to output. @param [Hash] opts Options hash.

# File lib/vagrant/util/safe_puts.rb, line 14
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