gsamokovarov/web-console — pull request #7 by gsamokovarov (2013-07-10 15:44:22+00:00)
Title: Capture standard output streams

There are a lot of methods that have side effects. Some of those side effects are printing to the standard output and error streams.

A great example of such method is the `reload!` console method. It prints `Reloading...` and then empties the `ActiveSupport` constant cache. If we don't capture the standard output, this print will reach the server logs, but not the user.

However, redirecting the standard streams is tricky. We want it to be thread-safe and we also want to capture the output of spawned subprocesses. For that reason I dropped the `StringIO` usages and redirected the streams to temporary files instead. The global streams can not be reopened with `StringIO` instances as those are not `IO` subclasses and they do not hold an underlying file descriptor. To be able to capture the output of subprocesses, we need to redirect to something that holds a file descriptor, so it can be propagated to them. A temporary file is great for that.

_(Although, they are still temporary files, all of the nasty things about them are still valid...)_

On the pros side, this saved us a couple of hacks, like the `IRB` monkey patching. On the cons side, redirecting standard output and standard error to the same temporary file is giving me some output ordering issues (or so, I believe), hence there is a failing test case.

I'm investigating it right now.