Re: [eclipse-clp-users] Stream log_output

From: Joachim Schimpf <joachim.schimpf_at_infotech.monash.edu.au>
Date: Thu, 11 Feb 2010 14:52:32 +1100
I'm copying this to the mailing list, since it is of general interest.

Dr. Thorsten Winterer wrote:
> I've got a question about the stream log_output. I would like to control
> the output format, so that entries in the log are easier to find. For
> instance, each log output should have a timestamp.

Here is some code that lets you do that.  The idea is that
open_stamped_stream/3 opens a new writeable stream Stream, that will
forward everything that is written to it to the existing OutStream,
prefixing every line with the string Tag and a timestamp:

open_stamped_stream(Tag, OutStream, Stream) :-
	open(queue(""), update, Stream),
	event_create(stamp_and_forward(Stream, Tag, OutStream), [], Event),
	set_stream_property(Stream, event, Event).

    stamp_and_forward(Stream, Tag, OutStream) :-
	( read_string(Stream, end_of_line, _, Line) ->
	    get_flag(unix_time, T),
	    local_time_string(T, "%Y-%m-%d %H:%M:%S:  ", Time),
	    printf(OutStream, "%s%s%s%n", [Tag,Time,Line]),
	    stamp_and_forward(Stream, Tag, OutStream)
	;
	    true
	).


Try it out:

?- open_stamped_stream("Boo ", output, out).

?- writeln(out, hello).
Boo 2010-02-11 14:48:32:  hello



To add timestamps to log_output, simply do:

?- open_stamped_stream("LOG ", output, log_output).
Yes (0.00s cpu)
?- minimize(member(X, [9, 6, 8, 4, 6, 3]), X).
LOG 2010-02-11 14:50:08:  Found a solution with cost 9
LOG 2010-02-11 14:50:08:  Found a solution with cost 6
LOG 2010-02-11 14:50:08:  Found a solution with cost 4
LOG 2010-02-11 14:50:08:  Found a solution with cost 3
LOG 2010-02-11 14:50:08:  Found no solution with cost -1.0Inf .. 2
X = 3
Yes (0.00s cpu)


Cheers,
Joachim
Received on Thu Feb 11 2010 - 04:51:41 CET

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET