Skip to content

Instantly share code, notes, and snippets.

@noprompt
Last active December 22, 2015 05:59
Show Gist options
  • Save noprompt/6428301 to your computer and use it in GitHub Desktop.
Save noprompt/6428301 to your computer and use it in GitHub Desktop.
ClojureScript console functions/macros
(ns clojurescript.console.macros
(:refer-clojure :exclude [time]))
(defmacro with-group [title & body]
`(do
(.group js/console ~title)
~@body
(.groupEnd js/console)))
(defmacro with-group-collpased [title & body]
`(do
(.groupCollapsed js/console ~title)
~@body
(.groupEnd js/console)))
(defmacro with-profile [title & body]
`(do
(.profile js/console ~title)
~@body
(.profileEnd js/console)))
(defmacro time [& body]
`(do
(.time js/console "Elapsed time")
~@body
(.timeEnd js/console "Elapsed time")))
(ns clojurescript.console
"Functions wrapping the JavaScript `console` API."
(:refer-clojure :exclude [time]))
(def ^:private node?
(and (not= (js* "typeof module") "undefined")
(.-exports js/module)))
(defn js-apply [f target args]
(.apply f target (to-array args)))
;;;; Logging
(defn log
"Display messages to the console."
[& args]
(js-apply (.-log js/console) js/console args))
(when-not node?
(defn debug
"Like `log` but marks the output as debugging information."
[& args]
(js-apply (.-debug js/console) js/console args)))
(defn info
"Like `log` but marks the output as an informative message."
[& args]
(js-apply (.-info js/console) js/console args))
(defn warn
"Like `log` but marks the output as a warning."
[& args]
(js-apply (.-warn js/console) js/console args))
(defn error
"Like `log` but marks the output as an error."
[& args]
(js-apply (.-error js/console) js/console args))
;;;; Message grouping
(when-not node?
(defn group [& args]
(js-apply (.-group js/console) js/console args))
(defn group-collapsed [& args]
(js-apply (.-groupCollapsed js/console) js/console args))
(defn group-end []
(.-groupEnd js/console)))
;;;; Assertion
(defn assert [& args]
(js-apply (.-assert js/console) js/console args))
;;;; Inspection
(defn dir [obj]
(.dir js/console obj))
(when-not node?
(defn dirxml [node]
(.dirxml js/console node)))
(defn trace []
(.trace js/console))
;;;; Profiling
(when-not node?
(defn profile [title]
(.profile js/console title))
(defn profile-end []
(.profileEnd js/console)))
;;;; Timing
(defn time [id]
(.time js/console id))
(defn time-end [id]
(.timeEnd js/console id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment