You are looking at historical revision 27569 of this page. It may differ significantly from its current revision.

Debug

A couple of Joe-Armstrong-style debugging macros

Abstract

According to Joe Armstrong, "The great gods of programming said, 'Thou shalt put printf statements in your program at the point where you think it’s gone wrong, recompile, and run it.'"

Documentation

debug?

[parameter] debug? → #t

`debug?' turns on or off debugging output, depending on whether it is set to #t or #f; respectively.

(define debug? (make-parameter #t))

trace

[syntax] (trace f) → unspecified

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)))))))))))

debug

[syntax] (debug expressions) → unspecified

Debug the expressions to stderr by pretty-printing each expression and their evaluations.

expressions
The expressions to be debugged
(define-syntax
  debug
  (syntax-rules
    ()
    ((_ x ...)
     (with-output-to-port
       (current-error-port)
       (lambda ()
         (when (debug?)
               (pp `((x ,(handle-exceptions
                           exn
                           ((condition-property-accessor 'exn 'message) exn)
                           x))
                     ...))))))))

debug/syslog

[syntax] (debug/syslog expressions) → unspecified

Debug to syslog.

expressions
The expressions to debug (cf. `debug' supra)
(define-syntax
  debug/syslog
  (er-macro-transformer
    (lambda (expression rename compare)
      `(let ((port (make-syslog-port)))
         (with-error-output-to-port
           port
           (lambda ()
             (when (debug?)
                   (debug ,@(cdr expression))
                   (flush-output port))))))))

About this egg

Author

Peter Danenberg

Colophon

Documented by cock.