Skip to content

Instantly share code, notes, and snippets.

@LauJensen
Forked from michalmarczyk/trace-ns.clj
Created October 17, 2010 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LauJensen/630550 to your computer and use it in GitHub Desktop.
Save LauJensen/630550 to your computer and use it in GitHub Desktop.
;;; See the inspirational SO question: http://stackoverflow.com/questions/3346382
(require 'clojure.contrib.trace)
(defn trace-ns
"Replaces each function from the given namespace with a version wrapped
in a tracing call. Can be undone with untrace-ns. ns should be a namespace
object or a symbol."
[ns]
(doseq [s (keys (ns-interns ns))
:let [v (ns-resolve ns s)]
:when (and (ifn? @v) (-> v meta :macro not))]
(intern ns
(with-meta s {:traced @v})
(let [f @v] (fn [& args]
(clojure.contrib.trace/trace (str "entering: " s))
(apply f args))))))
(defn untrace-ns
"Reverses the effect of trace-ns, replacing each traced function from the
given namespace with the original, untraced version."
[ns]
(doseq [s (keys (ns-interns ns))
:let [v (ns-resolve ns s)]
:when (:traced (meta v))]
(alter-meta! (intern ns s (:traced (meta v)))
dissoc :traced)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment