I am +1 on this patch, had one clarification question and if you think there is no issue we can commit this.
What would happen if an exception was thrown in the plan command during the time when we start recording the output and before you print it out the stream.
I am thinking that user would see an exception but not the output of the recorded statements, do we need put this in a try..catch and record the exception also in the stream and print it out ? or should we just throw and let the user lose those statements ?
Also in the current code fragment the possibility of a runtime exception is very low, but I am wondering from a code maintenance standpoint , if new code is added to that fragment, should we define a formal behavior ?