1

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?

Community
  • 1
  • 1
shaft
  • 2,147
  • 2
  • 22
  • 38
  • 1
    Considered Robert Hooke by technomancy? https://github.com/technomancy/robert-hooke You wouldn't have to patch `tools.trace` directly. – Leon Grapenthin Feb 01 '14 at 17:59
  • Thanks for the tip. I will look into it. The latest commit is 1 year old, do you think it's still adivisable to use? – shaft Feb 01 '14 at 21:04
  • 1
    The robert-hooke library is actually quite simple in its implementation and in the functionality it provides, which is very useful. [leiningen](https://github.com/technomancy/leiningen/blob/master/leiningen-core/project.clj#L9) uses it (to enable users [modify built-in tasks](https://github.com/technomancy/leiningen/blob/ff403992e414d86e2b9b775dce6a179bc21d3944/doc/PLUGINS.md#hooks)) so I would say that the reason why it hasn't had any commits lately, is because it is pretty solid already. – juan.facorro Feb 02 '14 at 22:42

0 Answers0