Class: Byebug::DAP::CapturedIO Private

Inherits:
Object
  • Object
show all
Defined in:
lib/byebug/dap/helpers/captured_io.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Captures STDOUT and STDERR. See CapturedOutput.

Instance Method Summary collapse

Constructor Details

#initialize(forward_stdout, forward_stderr) ⇒ CapturedIO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Capture STDOUT and STDERR and create a new Byebug::DAP::CapturedIO.gem:byebug:Byebuggem:byebug:Byebug::DebugThread running #capture. See Byebug::DAP::CapturedOutput#initialize.

Parameters:

  • forward_stdout (Boolean)

    if true, captured STDOUT is forwarded to the original STDOUT.

  • forward_stderr (Boolean)

    if true, captured STDERR is forwarded to the original STDERR.



10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#captureObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

#logstd:IO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return an IO that can be used for logging.

Returns:

  • (std:IO)


22
23
24
25
26
27
28
29
30
# File 'lib/byebug/dap/helpers/captured_io.rb', line 22

def log
  if defined?(LOG)
    LOG
  elsif @stderr
    @stderr.original
  else
    STDERR
  end
end

#restoreObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Restore the original STDOUT and STDERR.



33
34
35
36
37
# File 'lib/byebug/dap/helpers/captured_io.rb', line 33

def restore
  @stop = true
  @stdout.restore
  @stderr.restore
end