Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I redirect stdout/stderr? #64

Open
khinsen opened this issue Mar 4, 2021 · 4 comments
Open

How do I redirect stdout/stderr? #64

khinsen opened this issue Mar 4, 2021 · 4 comments

Comments

@khinsen
Copy link

khinsen commented Mar 4, 2021

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
Copy link
Author

khinsen commented Mar 5, 2021

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.

@theseion
Copy link
Collaborator

theseion commented Mar 6, 2021

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?

@khinsen
Copy link
Author

khinsen commented Mar 7, 2021

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.

@theseion
Copy link
Collaborator

theseion commented Mar 7, 2021

Ok. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants