Captures a specific stream, and invokes the specified callback with the text written to the stream instead. It returns a function that can be used to restore the captured stream.
function captureStream(name, callback) {
  var stream = process[name];
  var originalWrite = stream.write;

  stream.write = function(str) {
    callback(name, str);
  };

  return {
    restore: function() {
      stream.write = originalWrite;
    }
  };
}