Redirect stdout and stderr?

Yeah, I think that’s a pretty reasonable way to write it, but if it’s something you’re doing a lot you could write a convenience wrapper:

function redirect_to_files(dofunc, outfile, errfile)
    open(outfile, "w") do out
        open(errfile, "w") do err
            redirect_stdout(out) do
                redirect_stderr(err) do
                    dofunc()
                end
            end
        end
    end
end

Which you would then use like:

redirect_to_files(prefix * ".log", prefix * ".err") do
    compute(...)
end

or even combine both filenames into a single prefix arg if you know they’re always going to be prefix.log and prefix.err.

5 Likes