-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thanks @cupakromer for this fix re-implementing `capture` now that Rails 4.2 has made it private.
- Loading branch information
1 parent
3df1f23
commit b85760a
Showing
3 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Ammeter | ||
class OutputCapturer | ||
# Is this thread safe!?!? | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mikegee
|
||
# This won't work with sub-processes | ||
def self.capture(io, &block) | ||
case io | ||
when :stdout | ||
capture_stdout(&block) | ||
when :stderr | ||
capture_stderr(&block) | ||
else | ||
raise "Unknown IO #{io}" | ||
end | ||
end | ||
|
||
def self.capture_stdout(&block) | ||
captured_stream = StringIO.new | ||
|
||
orginal_io, $stdout = $stdout, captured_stream | ||
|
||
block.call | ||
|
||
captured_stream.string | ||
ensure | ||
$stdout = orginal_io | ||
end | ||
|
||
def self.capture_stderr(&block) | ||
captured_stream = StringIO.new | ||
|
||
orginal_io, $stderr = $stderr, captured_stream | ||
|
||
block.call | ||
|
||
captured_stream.string | ||
ensure | ||
$stderr = orginal_io | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
No, its not. I have a script to prove it but I'm not sure it matters. Does it matter?
A thread-safe version might fork a subprocess to invoke the block. I don't have a working implementation yet, and I'm not sure its worth the overhead.