class Byebug::DAP::CapturedIO

Captures STDOUT and STDERR. See {CapturedOutput}. @api private

Public Class Methods

new(forward_stdout, forward_stderr) click to toggle source

Capture STDOUT and STDERR and create a new {gem:byebug:Byebug::DebugThread} running {#capture}. See {CapturedOutput#initialize}. @param forward_stdout [Boolean] if true, captured STDOUT is forwarded to the original STDOUT. @param forward_stderr [Boolean] if true, captured STDERR is forwarded to the original STDERR.

# File lib/byebug/dap/helpers/captured_io.rb, line 10
def initialize(forward_stdout, forward_stderr)
  @forward_stdout = forward_stdout
  @forward_stderr = forward_stderr
  @stdout = CapturedOutput.new STDOUT
  @stderr = CapturedOutput.new STDERR
  @stop = false

  Byebug::DebugThread.new { capture }
end

Public Instance Methods

log() click to toggle source

Return an IO that can be used for logging. @return [std:IO]

# File lib/byebug/dap/helpers/captured_io.rb, line 22
def log
  if defined?(LOG)
    LOG
  elsif @stderr
    @stderr.original
  else
    STDERR
  end
end
restore() click to toggle source

{CapturedOutput#restore Restore} the original STDOUT and STDERR.

# File lib/byebug/dap/helpers/captured_io.rb, line 33
def restore
  @stop = true
  @stdout.restore
  @stderr.restore
end

Private Instance Methods

capture() click to toggle source

In a loop, read from the captured STDOUT and STDERR and send an output event to the active session's client (if there is an active session), and optionally forward the output to the original STDOUT/STDERR. @api private @!visibility public

# File lib/byebug/dap/helpers/captured_io.rb, line 46
def capture
  until @stop do
    r, = IO.select([@stdout.captured, @stderr.captured])

    r.each do |r|
      case r
      when @stdout.captured
        b = @stdout.captured.read_nonblock(1024)
        @stdout.original.write(b) if @forward_stdout
        send(:stdout, b)

      when @stderr.captured
        b = @stderr.captured.read_nonblock(1024)
        @stderr.original.write(b) if @forward_stderr
        send(:stderr, b)
      end
    end
  end

rescue EOFError, Errno::EBADF
rescue StandardError => e
  log.puts "#{e.message} (#{e.class})", *e.backtrace
end
send(source, data) click to toggle source
# File lib/byebug/dap/helpers/captured_io.rb, line 70
def send(source, data)
  session = Byebug::Context.interface
  return unless session.is_a?(Session)

  session.event! 'output', category: source.to_s, output: data

rescue IOError, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED
  # client disconnected
end