如何检查REPL内部是否正在评估Clojure代码? [英] How to check whether Clojure code is being evaluated inside a REPL?

查看:56
本文介绍了如何检查REPL内部是否正在评估Clojure代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的代码是从REPL运行还是运行已编译的jar,以不同的方式格式化日志.

I would like to format my logs differently depending on whether my code is being run from a REPL or if I'm running the compiled jar.

有没有简单的方法可以做到这一点?我在想也许雷宁根在运行REPL时会留下痕迹.

Is there any simple way to do this? I was thinking maybe Leiningen leaves a trace somewhere when running the REPL.

推荐答案

(defn current-stack-trace []
      (.getStackTrace (Thread/currentThread)))

(defn is-repl-stack-element [stack-element]
      (and (= "clojure.main$repl" (.getClassName  stack-element))
           (= "doInvoke"          (.getMethodName stack-element))))

(defn is-in-repl []
      (some is-repl-stack-element (current-stack-trace)))

(defn my-log [msg]
      (if (is-in-repl)
          (prn (str "RUNNING IN REPL : " msg))
          (prn msg)))

这篇关于如何检查REPL内部是否正在评估Clojure代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆