Skip to content

Instantly share code, notes, and snippets.

@pedroteixeira
Created June 11, 2010 01:54
Show Gist options
  • Save pedroteixeira/433930 to your computer and use it in GitHub Desktop.
Save pedroteixeira/433930 to your computer and use it in GitHub Desktop.
(defn intercept
"Call the function body-fn, by installing bindings to intercept all functions with a generic handler. The handler is called with the Var to the original function, its original value and original arguments."
[functions handler body-fn]
(with-bindings
(reduce
(fn [bs fn-var]
(let [f (deref fn-var)]
(merge bs
{fn-var
(fn [& args] (handler fn-var f args))})))
{} functions)
(body-fn)))
(defn dotrace
"Just like dotrace but works with a seq of function Vars"
[functions body-fn]
(intercept
functions
(fn [f-var f args]
(trace/trace-fn-call f-var f args))
body-fn))
(defn wrap-log-trace
[handler functions path]
(fn [request]
(with-out-append-writer path
(dotrace functions #(handler request)))))
(comment
(defroutes routes (GET "/path" [])
(wrap! routes
(:log-trace
(flatten (map #(vals (ns-publics (find-ns %)))
['compojure.core
'my.namespace]))
"/tmp/trace.log")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment