Use in place of defn; traces each call/return of this fn, including
arguments. Nested calls to deftrace'd functions will print a
tree-like structure.
;;Example courtesy to Jonas Enlund on clojure@googlegroups.com 2011-12-30:
(ns trc.core
(:use [clojure.tools.trace :only [deftrace]]))
(deftrace fib [n]
(if (or (= n 0) (= n 1))
1
(+ (fib (- n 1)) (fib (- n 2)))))
the following is printed when (fib 4) is evaluated:
TRACE t2302: (fib 4)
TRACE t2303: | (fib 3)
TRACE t2304: | | (fib 2)
TRACE t2305: | | | (fib 1)
TRACE t2305: | | | => 1
TRACE t2306: | | | (fib 0)
TRACE t2306: | | | => 1
TRACE t2304: | | => 2
TRACE t2307: | | (fib 1)
TRACE t2307: | | => 1
TRACE t2303: | => 3
TRACE t2308: | (fib 2)
TRACE t2309: | | (fib 1)
TRACE t2309: | | => 1
TRACE t2310: | | (fib 0)
TRACE t2310: | | => 1
TRACE t2308: | => 2
TRACE t2302: => 5
(defmacro deftrace
"Use in place of defn; traces each call/return of this fn, including
arguments. Nested calls to deftrace'd functions will print a
tree-like structure."
[name & definition]
`(do
(def ~name)
(let [f# (fn ~@definition)]
(defn ~name [& args#]
(trace-fn-call '~name f# args#)))))
Comments top
No comments for deftrace. Log in to add a comment.