Skip to content

Instantly share code, notes, and snippets.

@mkremins
Created July 22, 2014 21:35
Show Gist options
  • Save mkremins/01e1f84b076792e462f5 to your computer and use it in GitHub Desktop.
Save mkremins/01e1f84b076792e462f5 to your computer and use it in GitHub Desktop.
Define traced function
; http://www.reddit.com/r/Clojure/comments/2behsz/clojurescript_debugging_tips/cj4mvok
(defmacro defntraced
"Define a function with it's inputs and output logged to the console."
[sym & body]
(let [[_ _ [_ & specs]] (macroexpand `(defn ~sym ~@body))
new-specs
(map
(fn [[args body]]
(let [prns (for [arg args]
`(js/console.log (str '~arg) "=" (pr-str ~arg)))]
(list
args
`(do
(js/console.groupCollapsed (str '~sym " " '~args))
~@prns
(let [res# ~body]
(js/console.log "=>" (pr-str res#))
(js/console.groupEnd)
res#)))))
specs)]
`(def ~sym (fn ~@new-specs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment