Output “System.out.println” into Emacs Cider's REPL (or other buffer)

半城伤御伤魂 提交于 2019-12-14 02:57:42

问题


I am using a Java library in Clojure. It does output many things directly to the console using System.out.println.

I am using the latest Cider with Emacs. I am starting Cider with cider-jack-in. However, I can see these outputs. I thought they would be copied to the nrepl-server buffer, but it is not.

I also tried to run (alter-var-root #'*out* (constantly *out*)) in the REPL, but no success.

What am I missing in how this should be done, if it can be done at all.


回答1:


You need to replace the java.io.PrintStream assigned to System.out with one that will forward everything to clojure.core/*out* (which is an instance of java.io.PrintWriter). To do that you have to create an adapter from PrintStream to PrintWriter. I haven't found an existing one in the Java SDK API but there is one in Apache Commons IO (or you can implement it by yourself):

(import (java.io PrintStream)
        (org.apache.commons.io.output WriterOutputStream))

(-> *out*
    (WriterOutputStream.)
    (PrintStream.)
    (System/setOut))

From now on when you call System.out.println anywhere in your JVM the output will be passed to clojure.core/*out*.



来源:https://stackoverflow.com/questions/37414126/output-system-out-println-into-emacs-ciders-repl-or-other-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!