Allow explicit naming of `spy` forms

This commit is contained in:
Peter Taoussanis 2012-09-21 21:02:58 +07:00
parent edf2a13817
commit c05b7ca392
1 changed files with 6 additions and 4 deletions

View File

@ -265,12 +265,13 @@
(log* ~level {} ~@sigs)))
(defmacro spy
"Evaluates expression and logs its form and result. Always returns the result.
Defaults to :debug logging level."
"Evaluates named expression and logs its result. Always returns the result.
Defaults to :debug logging level and unevaluated expression as name."
([expr] `(spy :debug ~expr))
([level expr]
([level expr] `(spy ~level '~expr ~expr))
([level name expr]
`(try
(let [result# ~expr] (log ~level '~expr ~expr) result#)
(let [result# ~expr] (log ~level ~name ~expr) result#)
(catch Exception e#
(log ~level '~expr (str "\n" (stacktrace/pst-str e#)))
(throw e#)))))
@ -306,5 +307,6 @@
(time (dotimes [n 10000] (trace "This won't log"))) ; Overhead 5ms/10ms
(time (dotimes [n 5] (info "foo" "bar")))
(spy (* 6 5 4 3 2 1))
(spy :debug :factorial6 (* 6 5 4 3 2 1))
(info (Exception. "noes!") "bar")
(spy (/ 4 0)))