Skip to content

Instantly share code, notes, and snippets.

@noprompt
Last active August 29, 2015 13:58
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 noprompt/b2893da6dd4fb6551874 to your computer and use it in GitHub Desktop.
Save noprompt/b2893da6dd4fb6551874 to your computer and use it in GitHub Desktop.
(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