Skip to content

Instantly share code, notes, and snippets.

@ninjudd
Created September 29, 2010 20:23
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 ninjudd/8ff63cbfe153c1d382e3 to your computer and use it in GitHub Desktop.
Save ninjudd/8ff63cbfe153c1d382e3 to your computer and use it in GitHub Desktop.
(defmacro multi-outstream [var]
(letfn [(outs [val] (if (instance? clojure.lang.Atom val) (first @val) val))]
`(java.io.PrintStream.
(proxy [java.io.BufferedOutputStream] [nil]
(write
([b#] (.write (~outs ~var) b#))
([b# off# len#] (.write (~outs ~var) b# off# len#)))
(flush [] (.flush (~outs ~var)))))))
(defmacro with-outstream [bindings & forms]
`(do (doseq [[var# outs#] (partition 2 ~bindings)]
(swap! var# conj outs#))
(binding ~bindings ~@forms)
(doseq [[var# outs#] (partition 2 ~bindings)]
(doall (swap! var# (partial remove #(= outs# %)))))))
(def *outs* (atom ()))
(def *errs* (atom ()))
(let [outs (multi-outstream *outs*)
errs (multi-outstream *errs*)]
(System/setOut outs)
(System/setErr errs))
(with-outstream [*outs* outs, *errs* outs]
(future (.println System/out "foo")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment