Sharing my debugging message discovery

That you can use debug/3 rather than format/2 to print debugging messages is probably old news to other members of this group, but it’s something I only just discovered by picking through the code for concurrent_maplist while figuring out how to do multithreading.

Until now, I’ve been doing “when in doubt, print it out” debugging using format statements. This involved lots of deleting or commenting out, followed by lots of replacing or uncommenting out.

Going through the code in the thread library, I discovered lines like

debug(concurrent, 'Waiting: received ~p', [Exit])

The way this works is that unless debug(concurrent). is called before the code is run, these messages get ignored.

If debug(+Topic). is run first, messages like:

% Waiting: received done(1,["Uno"])

are printed to help figure out what the code is doing.

The details are at A.14 library(debug): Print debug messages and test assertions

2 Likes

And there are more goodies:

  • When compiling in optimised mode (-O), debug/3 is replaced by goal_expansion/2 to true which is eventually removed completely.
  • You can use the Prolog flag message_context to add time stamps and thread names.
  • The tools such as PceEmacs provide menus to list available debug topics, control them and show/hide their messages. This will be integrated in the new web enabled tool and hopefully a bit better …
5 Likes

also useful:

?- prolog_ide(debug_monitor).
1 Like

Just :new: The webstat web dashboard now also allows capturing debug messages. The dashboard is now connected using a websocket to the Prolog process it is monitoring, so Prolog can send push events such as debug messages. That allows for a whole lot of other nice stuff :slight_smile:

5 Likes

This is a great feature, but when working with http one would like debugging to go to a log stream instead. Is there a mode of invocation that can take a stream?

Not really sure I understand what you want. The debug/1 interface already allows you to redirect debug messages to a file using ?- debug(Topic > File). A general hook in the debug library that is used by the web frontend (and the native gui) allows doing whatever you want with the debug messages.

The web interface notably makes searching them relatively easy. It will get some additional features, notably to filter what is shown using the topic and additional columns for time and possibly CPU time.

I’ve also used library(syslog) for sending the debug to a topic in syslog.

Printing error messages there’s a whole subsystem that is very neat.

Solves the whole issue of handling errors from within libraries.

http://www.pathwayslms.com/swipltuts/message/index.html

2 Likes