Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether Clojure code is being evaluated inside a REPL?

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.

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

like image 430
noziar Avatar asked May 23 '15 05:05

noziar


1 Answers

(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)))
like image 59
Alfred Xiao Avatar answered Sep 27 '22 20:09

Alfred Xiao