chickadee » debug » trace

(trace f) → unspecifiedsyntax

Trace the input to and output from a function.

f
The function to be traced
(define-syntax
  trace
  (er-macro-transformer
    (lambda (expression rename compare)
      (match-let
        (((_ f) expression))
        (let ((%set! (rename 'set!))
              (%lambda (rename 'lambda))
              (%call-with-values (rename 'call-with-values))
              (%apply (rename 'apply))
              (%format (rename 'format))
              (%values (rename 'values))
              (%let (rename 'let))
              (%f (rename 'f))
              (%when (rename 'when))
              (%debug? (rename 'debug?)))
          `(,%when
            (,%debug?)
            (,%let
             ((,%f ,f))
             (,%set!
              ,f
              (,%lambda
               x
               (,%format (current-error-port) ";; Arguments to ~a: ~a~%" ',f x)
               (,%let
                ((return-values
                   (,%call-with-values
                    (,%lambda () (,%apply ,%f x))
                    (,%lambda x x))))
                (,%format
                 (current-error-port)
                 ";; Values from ~a: ~a~%"
                 ',f
                 return-values)
                (,%apply ,%values return-values)))))))))))