Re: capturing print_message to a file

Jan Wielemaker <[email protected]> Thu, 3 Apr 2014 16:47:24 +0200
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
Hi Alan,

On 04/03/2014 04:13 PM, Alan Baljeu wrote:
> This module I couldn't find (6.4.1):
>
> 65 ?-
> check_installation:run_collect_messages(cp:test_member_con_instance,
> Result, Messages).
> ERROR: [Thread 2] toplevel: Undefined procedure:
> check_installation:run_collect_messages/3 (DWIM could not correct goal)
>
> [debug] 67 ?- [library(check_installation)].
> ERROR: [Thread 2] source_sink `library(check_installation)' does not exist

This library is new in 7.1

> The code exhibits an interesting use of asserta to make a temporary
> hook. The predicate would be nice to have documented and in a public
> module not related to installation.

Possibly yes, although a full implementation also needs to support
non-determinism.

> Anyway by now I have put together similar:
>
> %% the main thing is I dump to stdout so I can redirect it.

In fact, you can also redirect user_error.  Boils down to this:

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

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

Now, you can do this:

:- 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),
		once(Goal),
		set_error(OldErr)),
	    close(Err)).

> user:message_hook(_Message, trace, Lines) :- maplist(spew, Lines).
>
> spew(flush) :- !,nl.
> spew(X) :- atom(X), !, write(X).
> spew(X-Y) :- format(X, Y).
> spew(X) :- format('Unknown: ~w', [X]).

Your spew is called print_message_lines/3, which also documents
the terms that may appear in the message representation.  The
message infrastructure is mostly copied from Quintus and one of
these things that deserves a proper tutorial as well :-(

> %% turn on trace and redirect it:
> :- meta_predicate chr_trace_log(:).

:- meta_predicate chr_trace_log(0).

if you want the tools to understand your code and you want goal
expansion to work on arguments.

	Cheers --- Jan


> chr_trace_log(Method) :-
> once(chr_leash(none))
> , setup_call_cleanup((chr_trace
>               , debug
>       , tell('chr_trace.log'))
> , Method
> , (flush
>    , told
>    , chr_notrace)).
>
>
> Alan Baljeu
>
>
> ----- Original Message -----
> From: Jan Wielemaker <[email protected]>
> To: Alan Baljeu <[email protected]>; Prolog
> <[email protected]>
> Cc:
> Sent: Thursday, April 3, 2014 3:50:26 AM
> Subject: Re: [SWIPL] capturing print_message to a file
>
> On 04/02/2014 10:26 PM, Alan Baljeu wrote:
>
>  > I see all this stuff flying by onscreen, so I want a file.  It's not
>  > stdout so I guess it's stderr.  Is redirecting the stream the
>  > intended way to go, or is the message_hook stuff the way to go?
>
> Maybe this:
>
> http://www.swi-prolog.org/pldoc/doc/home/vnc/prolog/lib/swipl/library/check_installation.pl?show=src#run_collect_messages/3
>
>      Cheers --- Jan
>
>
>  >
>  > Alan Baljeu -------------- next part -------------- HTML attachment
>  > scrubbed and removed _______________________________________________
>  > SWI-Prolog mailing list [email protected]
>  > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
>  >