A simple debugging macro



Hello all,

I wrote this simple macro a while back and I've found it to be
extremely useful for old-fashioned trace style debugging. Maybe
others will find it useful as well. Thoughts or criticism are
welcome. Perhaps others have similar macros or other methods of doing
this that are better.

This is the macro:

(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *debug* t)

(defmacro print-vars (&rest vars)
"Prints variables listed in vars to *trace-output*. The first
argument can be a string which will be printed directly."
(when *debug*
`(progn (format *trace-output* "TRACE: ")
,(when (stringp (first vars))
`(format *trace-output* "~A " ,(pop vars)))
,@(loop for var in vars
collect `(format *trace-output* "~A=~S "
',var ,var))
(format *trace-output* "~%" )
(finish-output *trace-output*)))))


This is how you use it:

CL-USER 46 > (let ((a 10))
(print-vars "before fib loop" a)
(loop for n from 1 to a
for x = 1 then y
and y = 1 then (+ x y)
do (print-vars n x y))
(print-vars "after fib loop" (* a a)))
TRACE: before fib loop A=10
TRACE: N=1 X=1 Y=1
TRACE: N=2 X=1 Y=2
TRACE: N=3 X=2 Y=3
TRACE: N=4 X=3 Y=5
TRACE: N=5 X=5 Y=8
TRACE: N=6 X=8 Y=13
TRACE: N=7 X=13 Y=21
TRACE: N=8 X=21 Y=34
TRACE: N=9 X=34 Y=55
TRACE: N=10 X=55 Y=89
TRACE: after fib loop (* A A)=100
NIL


Regards,

Anthony
.



Relevant Pages

  • Re: A simple debugging macro
    ... I wrote this simple macro a while back and I've found it to be ... extremely useful for old-fashioned trace style debugging. ... Inserting a string in a string and printing that. ... TRACE: before fib loop A=10 ...
    (comp.lang.lisp)
  • Re: A simple debugging macro
    ... extremely useful for old-fashioned trace style debugging. ... This is a very good suggestion, I'll change the macro to handle it. ... Inserting a string in a string and printing that. ... TRACE: before fib loop A=10 ...
    (comp.lang.lisp)
  • Re: A simple debugging macro
    ... extremely useful for old-fashioned trace style debugging. ... I usually just remove the PRINT-VARS ... TRACE: before fib loop A=10 ... (defmacro print-vars (&rest vars) ...
    (comp.lang.lisp)
  • [PATCH 07/10] tracing: add raw trace point recording infrastructure
    ... The current event tracer can automatically pick up trace points ... that are registered with the TRACE_FORMAT macro. ... TPPROTO(struct rq *rq, struct task_struct *p, int success), ... +#undef TRACE_EVENT_FORMAT ...
    (Linux-Kernel)
  • Re: [PATCH 07/10] tracing: add raw trace point recording infrastructure
    ... The current event tracer can automatically pick up trace points ... that are registered with the TRACE_FORMAT macro. ... but just want to use the convenience of printf. ...   TRACE_EVENT_FORMAT(name, ...
    (Linux-Kernel)