Module: Vagrant::Util::SafePuts

Included in:
Plugin::V1::Command, Plugin::V2::Command, Vagrant::UI::Basic
Defined in:
lib/vagrant/util/safe_puts.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#safe_puts(message = nil, opts = nil) ⇒ Object

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

Parameters:

  • message (String) (defaults to: nil)

    Message to output.

  • opts (Hash) (defaults to: nil)

    Options hash.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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