34
35:- module(nitrace, [nitrace_file/3,
36 nitrace/1,
37 nitrace/3]). 38
39:- use_module(library(apply)). 40:- use_module(library(context_values)). 41:- use_module(library(ontrace)). 42:- use_module(library(prolog_clause), []). 43
44:- meta_predicate nitrace_file(0,+,+). 45nitrace_file(Goal, Alias, OptL) :-
46 absolute_file_name(Alias, File),
47 setup_call_cleanup(
48 open(File, write, Stream),
49 nitrace(Goal, Stream, OptL),
50 close(Stream)).
51
52:- meta_predicate nitrace(0,+,+). 53nitrace(Goal, Stream, OptL) :-
54 ontrace(Goal, nitrace_port(Stream), OptL).
55
56:- meta_predicate nitrace(0). 57nitrace(Goal) :-
58 nitrace(Goal, user_output, []).
59
60frame_pi(Frame, PI) :-
61 prolog_frame_attribute(Frame, predicate_indicator, PI).
62
63nitrace_port(Stream, Port, Frame, PC, ParentL, SubLoc, continue) :-
64 ( maplist(frame_pi, ParentL, CS),
65 with_context_values(
66 print_message(stream, frame(Frame, Port, PC, CS)),
67 [stream, location], [Stream, SubLoc]),
68 fail
69 ; true
70 ).
71
72:- multifile
73 user:message_property/2,
74 prolog:message//1. 75
76user:message_property(stream, stream(Stream)) :-
77 !,
78 get_context_value(stream, Stream).
79user:message_property(stream, prefix(F-A)) :-
80 !,
81 get_context_value(location, Loc),
82 '$messages':swi_location(Loc, [F1-A], []),
83 atomic_list_concat(['~N', F1, '\t'], F).
84
85prolog:message(frame(Frame, redo(Redo), PC, CS)) --> !,
86 '$messages':translate_message(frame(Frame, redo, PC, CS)),
87 [' - redo(~w)'-[Redo]].
88prolog:message(frame(Frame, exitcl, PC, CS)) --> !,
89 '$messages':translate_message(frame(Frame, exit, PC, CS)),
90 [' - clause'].
91prolog:message(frame(Frame, exception(Ex), PC, CS)) --> !,
92 '$messages':translate_message(frame(Frame, exception, PC, CS)),
93 [nl],
94 '$messages':translate_message(Ex).
95prolog:message(frame(Frame, Port, PC, CS)) -->
96 '$messages':translate_message(frame(Frame, Port, PC)),
97 ( {CS = []}
98 ->[]
99 ; [' (caller: ~q)'-[CS]]
100 )