-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.cljc
52 lines (47 loc) · 1.77 KB
/
core.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns postmortem.instrument.core
(:require [postmortem.core :as pm]))
(defonce instrumented-vars (atom {}))
(def ^:private ^:dynamic *depth* 0)
(defn- logging-fn [f fname {:keys [session xform with-depth]}]
(let [xform (or xform identity)
sess (if session (constantly session) pm/current-session)
log-item (if with-depth
#(assoc % :depth *depth*)
identity)
save #(pm/spy>> (sess) fname xform (log-item %))
f (fn [args]
(save {:args args})
(let [thrown (volatile! nil)
ret (try
(apply f args)
(catch #?(:clj Throwable :cljs :default) t
(vreset! thrown t)))]
(if @thrown
(do (save {:args args :err @thrown})
(throw @thrown))
(do (save {:args args :ret ret})
ret))))]
(if with-depth
(fn [& args]
(binding [*depth* (inc *depth*)]
(f args)))
(fn [& args]
(f args)))))
(defn instrument-1* [sym v opts]
(let [{:keys [raw wrapped]} (get @instrumented-vars v)
current @v
to-wrapped (if (= wrapped current) raw current)]
(when (or (fn? to-wrapped)
(instance? #?(:clj clojure.lang.MultiFn
:cljs cljs.core.MultiFn)
to-wrapped))
(let [instrumented (logging-fn to-wrapped sym opts)]
(swap! instrumented-vars assoc v
{:raw to-wrapped :wrapped instrumented})
instrumented))))
(defn unstrument-1* [_sym v]
(when-let [{:keys [raw wrapped]} (get @instrumented-vars v)]
(swap! instrumented-vars dissoc v)
(let [current @v]
(when (= wrapped current)
raw))))