A.10 library(debug): Print debug messages and test assertions

author
Jan Wielemaker

This library is a replacement for format/3 for printing debug messages. Messages are assigned a topic. By dynamically enabling or disabling topics the user can select desired messages. Debug statements are removed when the code is compiled for optimization.

See manual for details. With XPCE, you can use the call below to start a graphical monitorring tool.

?- prolog_ide(debug_monitor).

Using the predicate assertion/1 you can make assumptions about your program explicit, trapping the debugger if the condition does not hold.

[semidet]debugging(+Topic)
[nondet]debugging(-Topic)
[nondet]debugging(?Topic, ?Bool)
Check whether we are debugging Topic or enumerate the topics we are debugging.
[det]debug(+Topic)
[det]nodebug(+Topic)
Add/remove a topic from being printed. nodebug(_) removes all topics. Gives a warning if the topic is not defined unless it is used from a directive. The latter allows placing debug topics at the start a a (load-)file without warnings.

For debug/1, Topic can be a term Topic > Out, where Out is either a stream or stream-alias or a filename (atom). This redirects debug information on this topic to the given output.

[det]list_debug_topics
List currently known debug topics and their setting.
[det]debug_message_context(+What)
Specify additional context for debug messages. What is one of +Context or -Context and Context is one of thread. time or time(Format), where Format is a format-specification for format_time/3 (default is %T.%3f). Initially, debug/3 show only thread information.
[det]debug(+Topic, +Format, :Args)
Similar to format/3 to user_error, but only prints if Topic is activated through debug/1. Args is a meta-argument to deal with goal for the @-command. Output is first handed to the hook prolog:debug_print_hook/3. If this fails, Format+Args is translated to text using the message-translation (see print_message/2) for the term debug(Format, Args) and then printed to every matching destination (controlled by debug/1) using print_message_lines/3.

The message is preceeded by '% ' and terminated with a newline.

See also
format/3.
[semidet,multifile]prolog:debug_print_hook(+Topic, +Format, +Args)
Hook called by debug/3. This hook is used by the graphical frontend that can be activated using prolog_ide/1:
?- prolog_ide(debug_monitor).
[det]assertion(:Goal)
Acts similar to C assert() macro. It has no effect if Goal succeeds. If Goal fails or throws and exception, the following steps are taken:

[semidet,multifile]prolog:assertion_failed(+Reason, +Goal)
This hook is called if the Goal of assertion/1 fails. Reason is unified with either fail if Goal simply failed or an exception ball otherwise. If this hook fails, the default behaviour is activated. If the hooks throws an exception it will be propagated into the caller of assertion/1.