Write plunit output to file

I’m using: SWI-Prolog version 7-7-17

How can I direct the output of plunit to a file? When I call

setup_call_cleanup( open(File, Mode, Stream),
with_output_to(Stream, run_tests),
close(Stream)
)

I still get all the dots printed to the console and the file is empty.

The feedback from PlUnit is sent to the user_error stream. Now it depends a little on what you want to achieve. If this is a batch run from a shell script I’d just use the shell to redirect stderr. If not, things get a little nastier as there is no with_output_to/2 variant for user_error. You can bind it to another stream using

<create a stream Stream>
set_stream(Stream, alias(user_error))

but this way seems a bit hacky to me.

Here’s what I use: % usage: redirect_error(run_tests, ‘output_file.txt’).

current_error(Stream) :- stream_property(Stream, alias(user_error)), !. % force det.

set_error(Stream) :-    set_stream(Stream, alias(user_error)).

:- meta_predicate redirect_error(0, +).

redirect_error(Goal, File) :- current_error(OldErr),

setup_call_cleanup(
open(File, write, Err),
setup_call_cleanup(
    set_error(Err),
    ignore(with_output_to(Err, Goal)),
    set_error(OldErr)),
close(Err)).