trace
Jump to function (5) ›
trace/deftrace#
(deftrace fn-name-sym & fdecl)
Like defn, but every call to the defined function prints its arguments and result via trace-fn. Recursive calls are traced too. Supports an optional docstring and metadata map like defn.
Example:
(deftrace fact [n] (if (<= n 1) 1 (* n (fact (dec n)))))
(fact 2)
; TRACE t1: (fact 2)
; TRACE t2: | (fact 1)
; TRACE t2: | => 1
; TRACE t1: => 2
trace/dotrace#
(dotrace fn-syms & body)
Temporarily traces the given global functions while body runs, then restores them. fn-syms is a vector of symbols naming global functions. Process-global like with-redefs, so intended for debugging and tests, not concurrent production code.
Example:
(defn add [a b] (+ a b))
(dotrace [add] (add 1 2))
; TRACE t1: (add 1 2)
; TRACE t1: => 3
trace/reset-trace-state!#
(reset-trace-state!)
Resets the trace depth and the trace id counter to their initial values. Useful to make trace output deterministic in tests.
Example:
(reset-trace-state!)
trace/trace#
(trace value)
(trace tag value)
Prints value to the standard error stream and returns it unchanged. With a tag, prints TRACE tag: value; without, prints TRACE: value. Like dbg but without source-location capture, so it also works when composed point-free.
Example:
(trace :sum (+ 1 2)) ; stderr: TRACE :sum: 3
; => 3
trace/trace-fn#
(trace-fn fn-name f)
Returns a function that behaves like f but prints every call with its arguments and its result to the standard error stream. Nested calls of traced functions are indented by depth and numbered, so recursion and call chains stay readable. fn-name is the label used in the output.
Example:
(def fib-t (trace-fn "fib" fib))
(fib-t 2)
; TRACE t1: (fib 2)
; TRACE t2: | (fib 1)
; TRACE t2: | => 1
; TRACE t1: => 1