chickadee » debug » debug

(debug expressions) → unspecifiedsyntax

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 `(,(if (or (boolean? 'x)
                              (char? 'x)
                              (number? 'x)
                              (string? 'x)
                              (vector? 'x))
                        x
                        `(x =>
                            ,(handle-exceptions
                               exn
                               (let ((message
                                       ((condition-property-accessor 'exn 'message) exn))
                                     (arguments
                                       ((condition-property-accessor 'exn 'arguments)
                                        exn)))
                                 (format
                                   "Error: ~a~a"
                                   message
                                   (if (null? arguments)
                                     ""
                                     (format
                                       ": ~a"
                                       (string-join (map ->string arguments) ", ")))))
                               x)))
                     ...))))))))