Module: WebConsole::Stream

Extended by:
Mutex_m
Defined in:
lib/web_console/stream.rb

Class Method Summary collapse

Class Method Details

.threadsafe_capture!(*streams) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/web_console/stream.rb', line 5

def self.threadsafe_capture!(*streams)
  streams = [$stdout, $stderr] if streams.empty?
  synchronize do
    begin
      streams_copy = streams.collect(&:dup)
      replacement  = Tempfile.new(name)
      streams.each do |stream|
        stream.reopen(replacement)
        stream.sync = true
      end
      yield
      streams.each(&:rewind)
      replacement.read
    ensure
      replacement.unlink
      streams.each_with_index do |stream, i|
        stream.reopen(streams_copy[i])
      end
    end
  end
end