All predicatesShow sourcestreams.pl -- Manage Prolog streams

This library provides high level primitives for stream operations. It is related to the charsio.pl and codesio.pl libraries. Considering these are de-facto standard Prolog libraries we prefer to leave these untouched.

Source with_output_to(?Output, :Goal, +Options) is det
Run Goal and once/1 while capturing all output to all streams (current_output, user_output and user_error) in the string Output. Options processed:
capture(ListOfStreams)
List of streams to capture. Default is [], causing the predicate to call with_output_to/2. The only admissible list elements are the alias names for the Prolog standard streams. As current_output is always captured, the only two values are user_output and user_error
color(Boolean)
When true, pretend the output is a terminal, causing messages to use ANSI term escape sequences for color.

For example, the following captures an error message. Note that we must catch and print the message inside Goal. If we do not do so the exception of Goal is simply propagated into the environment without binding Output.

?- with_output_to(string(Out),
                  catch(A is log(-1), E, print_message(error, E)),
                  [capture([user_error]), color(true)]).
Out = "\u001B[1;31mERROR: is/2: Arithmetic: \c
       evaluation error: `undefined'\n\u001B[0m",
E = error(evaluation_error(undefined), context(system:(is)/2, _)).