% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \section{library(debug): Print debug messages and test assertions} \label{sec:debug} This library is a replacement for \predref{format}{3} for printing debug messages. Messages are assigned a \textit{topic}. By dynamically enabling or disabling topics the user can select desired messages. Calls to \predref{debug}{3} and \predref{assertion}{1} are removed when the code is compiled for optimization unless the Prolog flag \prologflag{optimise_debug} is set to \const{true}. Using the predicate \predref{assertion}{1} you can make assumptions about your program explicit, trapping the debugger if the condition does not hold. Output and actions by these predicates can be configured using \textit{hooks} to fit your environment. With XPCE, you can use the call below to start a graphical monitoring tool. \begin{code} ?- prolog_ide(debug_monitor). \end{code} \vspace{0.7cm} \begin{description} \predicate[semidet]{debugging}{1}{+Topic} \nodescription \predicate[nondet]{debugging}{1}{-Topic} \nodescription \predicate[nondet]{debugging}{2}{?Topic, ?Bool} Examine debug topics. The form \verb$debugging(+Topic)$ may be used to perform more complex debugging tasks. A typical usage skeleton is: \begin{code} ( debugging(mytopic) -> ; true ), ... \end{code} The other two calls are intended to examine existing and enabled debugging tokens and are typically not used in user programs. \predicate[det]{debug}{1}{+Topic} \nodescription \predicate[det]{nodebug}{1}{+Topic} Add/remove a topic from being printed. \verb$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 of a (load-)file without warnings. For \predref{debug}{1}, \arg{Topic} can be a term \verb$Topic > Out$, where \arg{Out} is either a stream or stream-alias or a filename (an atom). This redirects debug information on this topic to the given output. On Linux systems redirection can be used to make the message appear, even if the \verb$user_error$ stream is redefined using \begin{code} ?- debug(Topic > '/proc/self/fd/2'). \end{code} A platform independent way to get debug messages in the current console (for example, a \verb$swipl-win$ window, or login using \const{ssh} to Prolog running an SSH server from the \const{libssh} pack) is to use: \begin{code} ?- stream_property(S, alias(user_error)), debug(Topic > S). \end{code} Do not forget to disable the debugging using \predref{nodebug}{1} before quitting the console if Prolog must remain running. \predicate[det]{list_debug_topics}{0}{} \nodescription \predicate[det]{list_debug_topics}{1}{+Options} List currently known topics for \predref{debug}{3} and their setting. \arg{Options} is either an atom or string, which is a shorthand for \verb$[search(String)]$ or a normal option list. Defined options are: \begin{description} \termitem{search}{String} Only show topics that match \arg{String}. Match is case insensitive on the printed representation of the term. \termitem{active}{+Boolean} Only print topics that are active (\const{true}) or inactive (\const{false}). \termitem{output}{+To} Only print topics whose target location matches \arg{To}. This option implicitly restricts the output to active topics. \end{description} \predicate[det]{debug_message_context}{1}{+What} Specify additional context for debug messages. \begin{tags} \tag{deprecated} New code should use the Prolog flag \prologflag{message_context}. This predicates adds or deletes topics from this list. \end{tags} \predicate[det]{debug}{3}{+Topic, +Format, :Args} \arg{Format} a message if debug topic is enabled. Similar to \predref{format}{3} to \verb$user_error$, but only prints if \arg{Topic} is activated through \predref{debug}{1}. \arg{Args} is a meta-argument to deal with goal for the @-command. Output is first handed to the hook \qpredref{prolog}{debug_print_hook}{3}. If this fails, \arg{Format}+\arg{Args} is translated to text using the message-translation (see \predref{print_message}{2}) for the term \verb$debug(Format, Args)$ and then printed to every matching destination (controlled by \predref{debug}{1}) using \predref{print_message_lines}{3}. The message is preceded by '\% ' and terminated with a newline. \begin{tags} \tag{See also} \predref{format}{3}. \end{tags} \qpredicate[semidet,multifile]{prolog}{debug_print_hook}{3}{+Topic, +Format, +Args}Hook called by \predref{debug}{3}. This hook is used by the graphical frontend that can be activated using \predref{prolog_ide}{1}: \begin{code} ?- prolog_ide(debug_monitor). \end{code} \predicate[det]{assertion}{1}{:Goal} Acts similar to C \verb$assert()$ macro. It has no effect if \arg{Goal} succeeds. If \arg{Goal} fails or throws an exception, the following steps are taken: \begin{itemize} \item call \qpredref{prolog}{assertion_failed}{2}. If \qpredref{prolog}{assertion_failed}{2} fails, then: \begin{itemize} \item If this is an interactive toplevel thread, print a message, the stack-trace, and finally trap the debugger. \item Otherwise, throw \verb$error(assertion_error(Reason, G),_)$ where Reason is one of \const{fail} or the exception raised. \end{itemize} \end{itemize} \qpredicate[semidet,multifile]{prolog}{assertion_failed}{2}{+Reason, +Goal}This hook is called if the \arg{Goal} of \predref{assertion}{1} fails. \arg{Reason} is unified with either \const{fail} if \arg{Goal} simply failed or an exception call otherwise. If this hook fails, the default behaviour is activated. If the hooks throws an exception it will be propagated into the caller of \predref{assertion}{1}. \end{description}