Use advice.
Should reduce some boilerplate.
See MacroPy’s tracing.
It’s pretty cool:
from macropy.tracing import macros, trace with trace: sum = 0 for i in range(0, 5): sum = sum + 5 # sum = 0 # for i in range(0, 5): # sum = sum + 5 # range(0, 5) -> [0, 1, 2, 3, 4] # sum = sum + 5 # sum + 5 -> 5 # sum = sum + 5 # sum + 5 -> 10 # sum = sum + 5 # sum + 5 -> 15 # sum = sum + 5 # sum + 5 -> 20 # sum = sum + 5 # sum + 5 -> 25
It traces everyexpression in the block!
- CLOSING NOTE [2013-03-18 Mon 14:01]
We’re doing it with ad-hoc tests right now, might want to switch to some kind of equality-predicate for self-evaluation.
Should we do some kind of type inference, or actually check whether or not the evaluatum is the same as the evaluandum?
(use srfi-13)
(define debug? (make-parameter #t))
(define-syntax debug
(syntax-rules ()
((_ x ...)
(with-output-to-port
(current-error-port)
(lambda ()
(when (debug?)
;; Handle some of the self-evaluating scalars in an ad-hoc
;; fashion (can't do this with e.g. lists and symbols,
;; though); alternatively: compare the expression with its
;; evaluated form for equality over some arbitrary
;; predicate (e.g. `equal?'): this might imply
;; self-evaluation.
(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)))
...))))))))
(define x 2)
(debug (string-join '("'my-nuts" "deez-nuts"))
"harro"
x
'x
(current-output-port)
(lambda () string-join)
'(1 2 3))
- CLOSING NOTE [2012-10-06 Sat 14:11]
Armstrong it is.
Joshua Bloch:
Debuggers are nice and there are times when I would have used a print statement, but instead use a breakpoint. So yes, I use debuggers occasionally, but I don’t feel lost without them, either. So long as I can put print statements in the code, and can read it thoroughly, I can usually find the bugs.
Joe Armstrong:
That way, we can still see good debug info even in the midst of failure. ah, i see: sometimes we do wantThe 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.”
write
and newline
, when a more
compact representation is desirable.
mechanism for debugging to different streams: stdout, syslog,
etc. global flag? (yes, a parameter.)
a wrapper around (current-error-port)
which syslogs it, for
instance.
(with-output-to-syslog [priority] thunk)
which takes a default
priority from some parameter?
(use syslog srfi-13 srfi-16 srfi-39 ports debug)
(define default-priority (make-parameter prio/debug))
(define with-output-to-syslog
(case-lambda
((thunk)
(with-output-to-syslog (default-priority) thunk))
((priority thunk)
(syslog priority (with-output-to-string thunk)))))
(with-output-to-syslog
(lambda () (display '(mein gott full of stars))))
(define make-syslog-port
(case-lambda
(()
(make-syslog-port (default-priority)))
((priority)
(let ((buffer ""))
(make-output-port
(lambda (scribendum)
(set! buffer (string-append/shared buffer scribendum)))
noop
(lambda () (syslog priority buffer)))))))
(define-syntax debug/syslog
(er-macro-transformer
(lambda (expression rename compare)
`(let ((port (make-syslog-port)))
(with-error-output-to-port
port
(lambda ()
(debug ,@(cdr expression))
(flush-output port)))))))
(debug/syslog (+ 2 2) (+ 2 3))
(use debug)
(debug/syslog (+ 2 2) (+ 2 3))
fail:
(use chicken extras ports matchable)
(import-for-syntax matchable)
;;; (write `(((+ 2 2) ,(+ 2 2)) ((+ 2 3) ,(+ 2 3))))
(define-syntax debug
(er-macro-transformer
(lambda (expression rename compare)
(match expression
((_) '(void))
((_ . expressions)
`(write `,,(map (lambda (expression)
`(',expression ,expression))
expressions))
#;`(write `((,',(car expressions) ,(+ 2 2))))
#;
`(begin ,@(map (lambda (expression)
`(write ',expression ,expression))
expressions))
#;
`(write
,@(map (lambda (expression)
``(expression ,,expression))
expressions)))))))
,x (debug (+ 2 2) (+ 2 3))
(use chicken ports test)
(import-for-syntax scheme chicken ports test)
(define-syntax debug
(syntax-rules ()
((_ x ...)
(with-output-to-port
(current-error-port)
(lambda ()
(write `((x ,x) ...))
(newline))))))
(let ((output
(with-output-to-string
(lambda ()
(with-error-output-to-port
(current-output-port)
(lambda ()
(debug '(mein gott) 'etc)))))))
(display
(string=?
"(((quote (mein gott)) (mein gott)) ((quote etc) etc))\n"
output)))