-
-
Save LauJensen/630550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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