aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-11-02 11:16:08 -0500
committerStuart Sierra <mail@stuartsierra.com>2009-11-02 11:16:08 -0500
commit6dff158bdec5f06c33877c6c6550699d5e08ca71 (patch)
tree04c0e4a308149875a163f33e885164c442d12d62
parent6f9e9c276bf53b0fce2275abf29a25f9735a6409 (diff)
trace.clj: add dotrace; fixes #39
Patch from Michel Salim
-rw-r--r--src/clojure/contrib/test_contrib/test_trace.clj6
-rw-r--r--src/clojure/contrib/trace.clj15
2 files changed, 19 insertions, 2 deletions
diff --git a/src/clojure/contrib/test_contrib/test_trace.clj b/src/clojure/contrib/test_contrib/test_trace.clj
index 4b283056..dee7122a 100644
--- a/src/clojure/contrib/test_contrib/test_trace.clj
+++ b/src/clojure/contrib/test_contrib/test_trace.clj
@@ -9,4 +9,8 @@
(deftest test-tracing-a-function-that-calls-itself
(let [output (with-out-str (call-myself 1))]
(is (re-find #"^TRACE t\d+: (call-myself 1)\nTRACE t\d+: | (call-myself 0)\nTRACE t\d+: | => nil\nTRACE t\d+: => nil$"
- output)))) \ No newline at end of file
+ output))))
+
+(deftest dotrace-on-core
+ (let [output (with-out-str (dotrace [mod] (mod 11 5)))]
+ (is (re-find #"\(mod 11 5\)" output))))
diff --git a/src/clojure/contrib/trace.clj b/src/clojure/contrib/trace.clj
index 1ac00a70..ccfc5ef1 100644
--- a/src/clojure/contrib/trace.clj
+++ b/src/clojure/contrib/trace.clj
@@ -32,7 +32,7 @@
(ns
- #^{:author "Stuart Sierra",
+ #^{:author "Stuart Sierra, Michel Salim",
:doc "This file defines simple \"tracing\" macros to help you see what your
code is doing."}
clojure.contrib.trace)
@@ -83,3 +83,16 @@ code is doing."}
(defn ~name [& args#]
(trace-fn-call '~name f# args#)))))
+(defmacro dotrace
+ "Given a sequence of function identifiers, evaluate the body
+ expressions in an environment in which the identifiers are bound to
+ the traced functions. Does not work on inlined functions,
+ such as clojure.core/+"
+ [fns & exprs]
+ (if (empty? fns)
+ `(do ~@exprs)
+ (let [func (first fns)
+ fns (next fns)]
+ `(let [f# ~func]
+ (binding [~func (fn [& args#] (trace-fn-call '~func f# args#))]
+ (dotrace ~fns ~@exprs))))))