Sha256: 24795db5a926e6c60996096d9a5b21964a843c13e480adc7a712caf879224646

Contents?: true

Size: 784 Bytes

Versions: 4

Compression:

Stored size: 784 Bytes

Contents

module StdoutAndErrCapture
  def capture_output(output = :out, &block)
    java_import 'java.io.PrintStream'
    java_import 'java.io.ByteArrayOutputStream'
    java_import 'java.lang.System'

    ruby_original_stream = output == :out ? $stdout.dup : $stderr.dup
    java_original_stream = System.send(output) # :out or :err
    ruby_buf = StringIO.new
    java_buf = ByteArrayOutputStream.new

    case output
    when :out
      $stdout = ruby_buf
    when :err
      $stderr = ruby_buf
    end
    System.setOut(PrintStream.new(java_buf))

    block.call

    ruby_buf.string + java_buf.toString
  ensure
    System.setOut(java_original_stream)
    case output
    when :out
      $stdout = ruby_original_stream
    when :err
      $stderr = ruby_original_stream
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
embulk-input-jira-0.0.4 spec/support/stdout_and_err_capture.rb
embulk-input-jira-0.0.3 spec/support/stdout_and_err_capture.rb
embulk-input-jira-0.0.2 spec/support/stdout_and_err_capture.rb
embulk-input-jira-0.0.1 spec/support/stdout_and_err_capture.rb