An important note on topics
(I will add this to the manual text later):
Jan writes:
"Debug channels are based on unification. That is quite deliberate. Notable it allows creating sub topics using http(post)
, http(get)
, etc. To debug all HTTP, use
?- debug(http(_))
.
With some tactical choice of terms you can make quite sophisticated selections of debug topics easily.
Strings vs. atoms is not really settled. I guess the rough idea is to use atoms for identifiers that you want to test for being (non-)equal and strings for stuff such as format/3 format arguments. Strings can also be useful to represent highly volatile text. Volatile atoms are placed in the atom table and require to be garbage collected.
The above has worked quite well for ECLiPSe that always has strings and did not have atom garbage collection. SWI-Prolog introduced strings much later and does have atom garbage collection, making the best practices a bit difficult.
Surely, use atoms for debug topics!"
An example:
foo(X) :- debug(foo,"This is the foo message: ~q",[X]).
And thus:
?- list_debug_topics. --------------------------------------------- Debug Topic Activated To --------------------------------------------- foo false [] true.
Now switch on the topic consisting of the STRING "foo", not the atom 'foo'. Note the warning.
?- debug("foo"). Warning: "foo": no matching debug topic (yet) true.
We now have two topics that look alike but are not the same:
?- list_debug_topics. --------------------------------------------- Debug Topic Activated To --------------------------------------------- foo false [] foo true [user_error] true.
No debug messages are emitted to stream "user_error":
?- foo(42). true.
Now switch on the topic of atom 'foo'. Note: no warning, presumably indicating that there are debug instructions which use this topic.
?- debug(foo). true.
And thus
?- list_debug_topics. --------------------------------------------- Debug Topic Activated To --------------------------------------------- foo true [user_error] foo true [user_error] true.
Now we get output!
?- foo(42). % This is the foo message: 42 true.
No debug / 2?
From https://github.com/SWI-Prolog/swipl-devel/issues/699
Concerning library(debug): There is format/1 to print parameterless messages format/2 to print parameterfull messages However there is only debug/3 to debug-print parameterfull messages Suggestion the addition of debug/2 to mirror format/1 and allow the printing of parameterless debug messages.
Jan writes:
I'm more inclined to call format/1,2,3 a mistake (inherited from Quintus Prolog). Notably the /2 version is always confusing whether it is stream,format or format,arguments. In my experience debug/2 is not often enough needed to be worth a separate predicate. For anyone disagreeing it is easy enough to define yourself :)