OSSubprocess icon indicating copy to clipboard operation
OSSubprocess copied to clipboard

How do I redirect stdout/stderr?

Open khinsen opened this issue 4 years ago • 4 comments

The class comment on OSSUnixSubprocess gives an example for output redirection saying

redirectStderrTo: '/tmp/stderrFile.txt' asFileReference writeStream

This no longer works because of the new file stream implementation. Is there another way to get the same effect, or is output redirection unsupported now?

khinsen avatar Mar 04 '21 17:03 khinsen

Here is something I discovered and tried:

outputFile := FileLocator temp / 'output.txt'.
self assert: outputFile exists not.
stream := StandardFileStream newFileNamed:  outputFile fullName.
stream << 'Hello world!'.
stream close.
self assert: outputFile contents equals: 'Hello world!'

Fine, so let's use this for stdout:

outputFile := FileLocator temp / 'output.txt'.
self assert: outputFile exists not.
stdoutStream := StandardFileStream newFileNamed: outputFile fullName.
self assert: outputFile exists.
OSSUnixSubprocess new
	command: '/bin/ls';
	arguments: #('/usr');
	redirectStdoutTo: stdoutStream;
	redirectStderr;
	runAndWaitOnExitDo: [ :process |
		process isSuccess
			ifFalse: [ self error: process exitStatusInterpreter printString ] ].
self assert: outputFile exists

The last assertion fails, meaning that OSSUnixSubprocess deletes the file at some point.

khinsen avatar Mar 05 '21 07:03 khinsen

That's not good...

This no longer works because of the new file stream implementation.

Do you already have an idea about the cause then?

theseion avatar Mar 06 '21 09:03 theseion

The cause for the recommended use pattern not working is pretty simple: '/tmp/stderrFile.txt' asFileReference writeStream returns a stream implemented by a completely different class, which doesn't have the extension method that OSSUnixSubprocess adds to the deprecated stream implementation. The best solution would be to port these extensions to the new stream classes, but I have no idea if that's a simple task or a major effort.

As for my last example deleting the new file, I have no idea so far why this happens.

khinsen avatar Mar 07 '21 11:03 khinsen

Ok. Thanks for the help!

theseion avatar Mar 07 '21 19:03 theseion