Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jiacai2050/13b1b45af7349a3ff6d2a79bdd37f7f5 to your computer and use it in GitHub Desktop.
Save jiacai2050/13b1b45af7349a3ff6d2a79bdd37f7f5 to your computer and use it in GitHub Desktop.
Code to redirect output nrepl output to a particular client repl.
;; After connecting an nrepl client repl to an nrepl server, some
;; output will still appear on stdout from the server.
;; Try these snippets - they will both produce output on the nrepl server
;; Java libraries commonly do both these things.
(.println System/out "Hello stdout.")
(.start (Thread. #(println "Hello from a new thread.")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Now run this code on the repl where you wish to see all output.
;; You will need to add the dependency [commons-io "2.4"] to your
;; leiningen dependencies.
(import 'org.apache.commons.io.output.WriterOutputStream)
(import 'java.io.PrintStream)
;; First, we redirect the raw stdout of the server to this repl
(System/setOut (PrintStream. (WriterOutputStream. *out*)
true)) ;; Auto-flush the PrintStream
;; Next, we alter the root binding of *out* so that new threads
;; send their output to THIS repl rather than the original System/out.
(alter-var-root #'*out* (fn [_] *out*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Now the snippets from before should both send output to this repl.
(.println System/out "Hello stdout.")
(.start (Thread. #(println "Hello from a new thread.")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment