Skip to content

Instantly share code, notes, and snippets.

@noprompt noprompt/tagged-out.clj Secret
Last active Aug 29, 2015

Embed
What would you like to do?
(require '[clojure.string :as string])
(defn make-tagged-out [tag]
(let [tag (format "[%s] " tag)
tag-lines (fn [s]
(string/replace s #"(^|\n)" (str "$1" tag)))
old-out *out*]
(proxy [java.io.Writer] []
(flush []
(. old-out (flush)))
(append [c]
(. old-out (append c)))
(write
([^String s]
(locking old-out
(.write old-out (tag-lines s))))))))
(defmacro with-out-tag [tag & body]
`(binding [*out* (make-tagged-out ~tag)]
~@body))
(let [f1 (future
(with-out-tag "a"
(dotimes [_ 10]
(Thread/sleep (max (rand 500) (rand 500)))
(println "Hello from a!"))))
f2 (future
(with-out-tag "b"
(dotimes [_ 10]
(Thread/sleep (max (rand 500) (rand 500)))
(println "Hello from b!"))))])
;; [a] Hello from a!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [a] Hello from a!
;; [b] Hello from b!
;; [b] Hello from b!
;; [a] Hello from a!
;; [a] Hello from a!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.