3

I need the swipl console output (the trace output) for one of my projects. I'm trying to use the JPL7 API to do this but I can't seem to find a method to grab output from the swipl console. Is there a way I can do this? Or is there a query I can run that directs the trace output to a file and then work from there?

Thanks in advance.

absolutelydevastated
  • 1,657
  • 1
  • 11
  • 28

1 Answers1

2

you can try to use protocol/1, then start your query prefixed by leash(-all),trace

edit a solution (?) to change file on backtracking: I would save in a module (maybe named trace_protocol :-) and then would use with ?- [trace_protocol]. and subsequently ?- trace,trace_protocol(append(X,Y,[1,2,3])).

:- meta_predicate trace_protocol(0).

trace_protocol :-
    Name = trace_protocol_index,
    catch(nb_getval(Name, N), _Exc, nb_setval(Name, 0)),
    % writeln(ex:Exc), 
    nb_current(Name, N),
    % writeln(nb_current(Name, N)), 
    M is N+1, nb_setval(Name, M),
    % writeln(nb_setval(Name, M)),
    format(atom(PN), '~s_~d.tty', [Name, N]),
    % writeln(trace_protocol:PN),
    protocol(PN).

trace_protocol(Q) :- trace_protocol, forall(Q, trace_protocol).

It took a lot to code, since seems there is a bug in nb_current/2. Should not, but it throws an exception - actually the exception is thrown from library(clpfd), even if that is not directly included in my test module.

The sequentially numbered *.tty files are best shown in terminal, for instance

$ cat *.tty

since there are TTY formatting escape sequences. Maybe such sequences could be turned down with ?- set_prolog_flag(color_term, false).

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • 1
    Thanks! That worked! Except that I had to add `noprotocol` at the end to flush the console output onto the file. – absolutelydevastated May 03 '16 at 09:40
  • Is it possible to change the destination file I am printing console output to at every different solution? – absolutelydevastated May 03 '16 at 10:07
  • no, I don't think it's easy... you could try to 'drive' the query process, and call the protocol/1 on backtracking, maybe adding a timestamp. Let me show the code, but you will have to test... – CapelliC May 03 '16 at 10:13
  • What do you mean by driving the query process? Also, protocol doesn't seem to output the solution. Is there some way to do this? – absolutelydevastated May 03 '16 at 10:33
  • See my last edit, the 'driver' is trace_protocol/1. HTH – CapelliC May 03 '16 at 12:00
  • @absolutelydevastated How did you manage to add `leash(-all)` and `trace` as the prefix in JPL? I tried `new Query("leash", new Term[]{new Atom("-all)})` and it can't get the result `true`, which is different from the result from console. Thanks. – BobHU Jun 08 '19 at 03:32
  • @BobHU Sorry, but I don't really remember much about JPL. There's also a typo in your question - the quote wasn't closed but I'll assume that you actually closed it. In any case, try just doing `new Query("leash(-all)")`? – absolutelydevastated Jun 09 '19 at 10:53
  • @absolutelydevastated It's Okay. It's been years. I did close the quote, though. I have found that without `leash` and `visible`, I can still get the trace file and for `trace`, just passing it empty term list will work. – BobHU Jun 09 '19 at 11:31