Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cljs devtools support #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ is not necessarily tied to logging to the console or printing to the
screen. Since the trace values are just data the barrier to creativity
is low.

An additional tracer that works well with [cljs-devtools]
(https://github.com/binaryage/cljs-devtools) is included as
`clairvoyant.core/cljs-devtools-tracer`.


### Source code transformation

Expand Down
88 changes: 88 additions & 0 deletions src/clairvoyant/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,91 @@
(.groupEnd js/console)
(.groupEnd js/console)))
(.groupEnd js/console)))))))

(def cljs-devtools-tracer
(let [pr-val (fn pr-val [x] x)
log-binding (fn [form init]
(.groupCollapsed js/console "%c%s"
"font-weight:bold;" (pr-str form)
(pr-val init)))
log-exit (fn [exit]
(.log js/console "=>" (pr-val exit)))
has-bindings? #{'fn*
`fn
'fn
'defn
`defn
'defmethod
`defmethod
'deftype
`deftype
'defrecord
`defrecord
'reify
`reify
'let
`let
'extend-type
`extend-type
'extend-protocol
`extend-protocol}
fn-like? (disj has-bindings? 'let `let)]
(reify
ITraceEnter
(-trace-enter
[_ {:keys [anonymous? arglist args dispatch-val form init name ns op protocol]}]
(cond
(fn-like? op)
(let [title (if protocol
(str protocol " " name " " arglist)
(str ns "/" name
(when dispatch-val
(str " " (pr-str dispatch-val)))
(str " " arglist)
(when anonymous? " (anonymous)")))
arglist (remove '#{&} arglist)]
(.groupCollapsed js/console title)
(.groupCollapsed js/console "bindings"))

(#{'let `let} op)
(let [title (str op)]
(.groupCollapsed js/console title)
(.groupCollapsed js/console "bindings"))

(#{'binding} op)
(log-binding form init)))

ITraceExit
(-trace-exit [_ {:keys [op exit]}]
(cond
(#{'binding} op)
(do (log-exit exit)
(.groupEnd js/console))

(has-bindings? op)
(do (.groupEnd js/console)
(log-exit exit)
(.groupEnd js/console))))

ITraceError
(-trace-error [_ {:keys [op form error ex-data]}]
(cond
(#{'binding} op)
(do
(.error js/console (.-stack error))
(when ex-data
(.groupCollapsed js/console "ex-data")
(.groupCollapsed js/console (pr-val ex-data))
(.groupEnd js/console)
(.groupEnd js/console)))

(has-bindings? op)
(do (.groupEnd js/console)
(do
(.error js/console (.-stack error))
(when ex-data
(.groupCollapsed js/console "ex-data")
(.groupCollapsed js/console (pr-val ex-data))
(.groupEnd js/console)
(.groupEnd js/console)))
(.groupEnd js/console)))))))