重定向clojure';s*out*到core.async通道

重定向clojure';s*out*到core.async通道,clojure,core.async,Clojure,Core.async,我试图让clojure程序中的所有print语句将它们的字符串放在core.async通道上。我有办法做到这一点吗 (我试图让cljsbuild将其编译器消息发送到前端。)gfredericks在#clojure irc中给了我这个参考。他建议我采用nREPL的做法: (defn- session-out "Returns a PrintWriter suitable for binding as *out* or *err*. All of the content written

我试图让clojure程序中的所有print语句将它们的字符串放在core.async通道上。我有办法做到这一点吗


(我试图让
cljsbuild
将其编译器消息发送到前端。)

gfredericks在#clojure irc中给了我这个参考。他建议我采用nREPL的做法:

(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)))
(defn-会话输出)
“将适合绑定的PrintWriter返回为*out*或*err*。所有
写入该PrintWriter的内容(当.flush为ed时)将通过
指定给定会话id的消息中的给定传输。
`频道类型`应为:out或:err(视情况而定)。”
[通道类型会话id传输]
(让[buf(clojure.tools.nrepl.StdOutBuffer.)]
(印刷撰稿人。(代理人[撰稿人][]
(关闭[](.flush^Writer this))
(写入[&[x^Integer off^Integer len]]
(锁定buf
(续)
(数字?x)(附加buf(字符x))
(未关闭)(.附加buf x)
;append的CharSequence重载采用*end*idx,而不是长度!
(实例?字符序列x)(.append buf^字符序列x(int off)(int(+len off)))
:else(.append buf^字符x off len))
(何时)(
(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)))