1
votes

I'm trying to make all print statements in a clojure program put their strings on a core.async channel. Is there a way I can do this?

(I'm trying to get cljsbuild to send its compiler messages to a frontend.)

2

2 Answers

2
votes
(def is (java.io.PipedInputStream.))
(def os (java.io.PipedOutputStream. is))

(def channel (chan))

(.start (Thread. (fn []
                   (let [buffer (byte-array 1024)]
                     (while true
                       (let [n (.read is buffer 0 1024)]
                         (>!! channel (vec (take n buffer)))))))))

(binding [*out* (java.io.OutputStreamWriter. os)]
  (println "Hello, world!"))
1
votes

gfredericks gave me this reference in the #clojure irc. He recommended I adapt what nREPL does:

(defn- session-out
  "Returns a PrintWriter suitable for binding as *out* or *err*.  All of
   the content written to that PrintWriter will (when .flush-ed) be sent on the
   given transport in messages specifying the given session-id.
   `channel-type` should be :out or :err, as appropriate."
  [channel-type session-id transport]
  (let [buf (clojure.tools.nrepl.StdOutBuffer.)]
    (PrintWriter. (proxy [Writer] []
                    (close [] (.flush ^Writer this))
                    (write [& [x ^Integer off ^Integer len]]
                      (locking buf
                        (cond
                          (number? x) (.append buf (char x))
                          (not off) (.append buf x)
                          ; the CharSequence overload of append takes an *end* idx, not length!
                          (instance? CharSequence x) (.append buf ^CharSequence x (int off) (int (+ len off)))
                          :else (.append buf ^chars x off len))
                        (when (<= *out-limit* (.length buf))
                          (.flush ^Writer this))))
                    (flush []
                      (let [text (locking buf (let [text (str buf)]
                                                (.setLength buf 0)
                                                text))]
                        (when (pos? (count text))
                          (t/send (or (:transport *msg*) transport)
                                  (response-for *msg* :session session-id
                                                channel-type text))))))
                  true)))

from nREPL source here