How can I inject custom code into the clojure tracing library? (https://github.com/clojure/tools.trace)
The library is producing traces like this: (see Debugging in Clojure?)
TRACE t4328: (fib 3)
TRACE t4329: | (fib 2)
TRACE t4330: | | (fib 1)
TRACE t4330: | | => 1
TRACE t4331: | | (fib 0)
TRACE t4331: | | => 0
TRACE t4329: | => 1
TRACE t4332: | (fib 1)
TRACE t4332: | => 1
TRACE t4328: => 2
I am particularly interested in:
- measuring the time of an invocation
- rendering of the input/output data.
- redirect the output
Examples of what I would like to produce:
Measure time:
TRACE t4328: (fib 3) (100 ms)
TRACE t4329: | (fib 2) (200 ms)
TRACE t4330: | | (fib 1) (150 ms)
.....
Rendering: Custom rendering per arg/return value
TRACE t4328: (fib number) (a small number was given)
TRACE t4329: | (fib number) (an even number was returned)
TRACE t4330: | | (fib number) (attention: number is too big)
Stacktrace:
(fib number) (fib.clj line 1)
| (fib number) (fib.clj line 2)
| | (fib number) (fib.clj line ...)
output to disk:
(fib 3)
| (fib 2)
| | (fib 1)
| | => 1
I am not sure if the library was designed to allow such customizations, however since the whole lib is merely a single file (https://github.com/clojure/tools.trace/blob/master/src/main/clojure/clojure/tools/trace.clj), I don't mind to patch it directly.
A question from 2010 (clojure: adding a debug trace to every function in a namespace?) is similar, but the suggested answer uses a custom version of trace-ns
.
There the custom code is injected manually:
(clojure.contrib.trace/trace (str "entering: " s))
In short: Is there a more generic way today to inject my custom code?