# Help with set\_prolog\_IO/3

**URL:** <https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [January 29, 2020, 7:22am UTC](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798 "2020-01-29T07:22:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oskarstreimanis](https://avatars.discourse-cdn.com/v4/letter/o/b5a626/32.png) [@oskarstreimanis](https://swi-prolog.discourse.group/u/oskarstreimanis)\
**Post date:** [January 29, 2020, 7:22am UTC](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798/1 "2020-01-29T07:22:30Z")

</div>

I’m using: SWI-Prolog 64 bits, version 8.1.10-56

I want the code to: I would like to redirect only std err stream to a file.

But what I’m getting is: At the moment I only managed to write everything to a file. I was wondering how could i redirect only the errors. And additionally I would like to add timestamp to error outputs as well, is it possible?

My code looks like this:

```prolog
create_error_stream :-
    open('/hard_coded_path/output.txt', write, Out, [alias(custom_error)]),
    set_prolog_IO(user_input, custom_error, custom_error), 
    with_output_to(Out, start_main),
    close(Out).

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [January 29, 2020, 10:07am UTC](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798/2 "2020-01-29T10:07:10Z")

</div>

I think this is what you need. You were very close. 😃

This first example demonstrates the basic code without redirecting the error stream.

```prolog
io_test_1 :-
    read_string(user_input, "\n", "\r", _, String),
    (
        String = "Error"
    ->
        print_message(error,String)
    ;
        format('~w~n',[String])
    ).

```

* * *

This code modifies the destination of the error stream using [set\_prolog\_IO/3](https://www.swi-prolog.org/pldoc/man?predicate=set_prolog_IO/3).

This redirects the error stream to a file. The OS is Windows but the file path is easily changed for any specific OS.

```prolog
io_test_2 :-
    setup_call_cleanup(
        (
            open("C:/Users/Eric/Documents/Projects/Prolog/swi-discourse_032/error.txt",write,Error,[]),
            set_prolog_IO(user_input,user_output,Error)
        ),
        (
            read_string(user_input, "\n", "\r", _, String),
            (
                String = "Error"
            ->
                print_message(error,String)
            ;
                format('~w~n',[String])
            )
        ),
        close(Error)
    ).

```

Example run without an error

```prolog
?- io_test_2.
|: Good
Good
true.

```

Example run with an error that is redirected to a file. Note that error does not appear on the console.

```prolog
?- io_test_2.
|: Error
true.

```

Contents of `error.txt`

```prolog
ERROR: Unknown message: "Error"

```

* * *

You might also want to take a look at the recently added [library(intercept)](https://swi-prolog.discourse.group/t/pushed-library-intercept/1691). I have not had a chance to work with it, but might be using it today with [sgml\_parse/2](https://www.swi-prolog.org/pldoc/man?predicate=sgml_parse/2)

* * *

EDIT

> [@oskarstreimanis](#):
>
> additionally I would like to add timestamp to error outputs as well

Any answer I could give would be a pure guess at this point and there is so much that I would not know how to check, (C code underneath SWI-Prolog predicates) and the entry and exit points along the path.

However if you know all of the errors you want to catch originate via [print\_message/2](https://www.swi-prolog.org/pldoc/man?predicate=print_message/2) then it might be possible. Just a guess but simple enough to trace though the [source code](https://www.swi-prolog.org/pldoc/doc/_SWI_/boot/messages.pl?show=src#print_message/2).

---

<div class="post-metadata">

**Author:** ![oskarstreimanis](https://avatars.discourse-cdn.com/v4/letter/o/b5a626/32.png) [@oskarstreimanis](https://swi-prolog.discourse.group/u/oskarstreimanis)\
**Post date:** [January 29, 2020, 10:54am UTC](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798/3 "2020-01-29T10:54:15Z")

</div>

@EricGT,

Thank you for your explicit answer, but I have to apologies for not being clear enough. I want to redirect Prolog generated errors. For example: `ERROR: [Thread httpsd8000_2] SSL(14094416) ssl3_read_bytes: sslv3 alert certificate unknown` .

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [January 29, 2020, 11:09am UTC](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798/4 "2020-01-29T11:09:57Z")

</div>

See [https://www.swi-prolog.org/pldoc/doc\_for?object=message\_hook/3](https://www.swi-prolog.org/pldoc/doc_for?object=message_hook/3)

---

<div class="post-metadata">

**Author:** ![oskarstreimanis](https://avatars.discourse-cdn.com/v4/letter/o/b5a626/32.png) [@oskarstreimanis](https://swi-prolog.discourse.group/u/oskarstreimanis)\
**Post date:** [January 29, 2020, 12:11pm UTC](https://swi-prolog.discourse.group/t/help-with-set-prolog-io-3/1798/5 "2020-01-29T12:11:25Z")

</div>

Yes, thank you!

This is what I was looking for, I can add timestamp with this as well, and even save warnings!

Perfect!
