Gnocl Cookbook‎ > ‎

Proc debugging -stages

Getting feedback on execution stages and the values of specific variables

The 'puts' command is probably the most ubiquitous debugging tool used by Tcl coders. Sure there are more complex and elaborate ways of watching the execution process and the Tcler's wiki gives lots of good tips in this area. For me, the simpler the better. Here's a short, utility proc that I've been using recently.

proc _S_ { {i {} } args} {
    if {$i == ""} {
        puts  [info level  1]
    } else  {
        puts  "[lindex [info level 1] 0] $i $args"

The procedure below illustrates how it can be used:

proc myProc  {a b c d e} {

_S_ 1
    puts "Do something..."

_S_ 2  "Can pass other values too!"
    puts "Do something more..."

    set x 10
    set y 11

_S_ 3 x $x y $y"
    puts "Do something else..."

_S_ 4
    puts "Done..."
_S_ cleanExit

The first call to _S_ simply returns the name 'myproc' and any arguments passed to it. But, as can be seen in the definition of _S_ it can take two arguments: i which defaults to an empty string, and args which, of course, is the standard Tcl expression for a list of args. Its necessary to have the first argument i, even if nothing is passed, as args alone will throw an up error. So, '_S_ 1' in the script denotes stage one, in the process.  It will result in the name of the procedure, ie myProc being printed to stdout along with the stage reference, ie 1.

Reading through the remainder of the sample shows how the extras can be passed.