Skip to content

Commit

Permalink
Add a new function to trace to a different file per thread, this time
Browse files Browse the repository at this point in the history
works by changing the root binding for tracer, and uses java's
ThreadLocal to use a different FileWriter for each thread.
  • Loading branch information
Jeff Weiss committed Nov 2, 2012
1 parent 3358cdd commit 414c4ab
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/fn/trace.clj
Expand Up @@ -26,7 +26,26 @@
[name value & [out?]]
(println (text-format name value)))

(defn per-thread-tracer [& [formatter]]
(def thread-local-filewriter
(proxy [ThreadLocal] []
(initialValue [] (->> (Thread/currentThread)
.getName
(format "%s.trace")
java.io.FileWriter.))))

(defn thread-tracer
"Writes trace to a file based on the current thread name"
[name value & [out?]]
(binding [*out* (.get thread-local-filewriter)]
(println (text-format name value out?))))

(defn per-thread-tracer
"Returns a tracer that writes to a file based on the name of the
current thread. Warning: binding tracer to functions produced here
might not have the effect you want. Futures, for instance, will use
the same binding as the parent thread, and cause shuffled tracing
entries in the output."
[& [formatter]]
(let [formatter (or formatter text-format)
tracefile-name (str (.getName (Thread/currentThread)) ".trace")
tr-file-writer (java.io.FileWriter. tracefile-name)]
Expand Down

0 comments on commit 414c4ab

Please sign in to comment.