Skip to content

Commit

Permalink
draft: dbgt
Browse files Browse the repository at this point in the history
  • Loading branch information
philoskim committed Aug 9, 2021
1 parent 6c45551 commit 977ba30
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 23 deletions.
25 changes: 25 additions & 0 deletions examples/src/clj/examples/etc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,28 @@

(set-line-bullet! "|")


;; The default debug level is 0.
(dbg (+ 10 20))
(dbg (+ 10 20 3) :level 3)
(dbg (+ 10 20 5) :level 5)

(with-level 3
(dbg (+ 10 20))
(dbg (+ 10 20 3) :level 3)
(dbg (+ 10 20 5) :level 5))


(defn my-add [a b]
(dbg (+ a b) :level 2))

(defn my-sub [a b]
(dbg (- a b) :level 3))

(with-level 3
(dbg (my-add 10 20))
(dbg (my-sub 100 10))

(with-level 0
(dbg (* 10 2))))

24 changes: 1 addition & 23 deletions examples/src/clj/examples/lab.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,4 @@

(use 'debux.core)

;; The default debug level is 0.
(dbg (+ 10 20))
(dbg (+ 10 20 3) :level 3)
(dbg (+ 10 20 5) :level 5)

(with-level 3
(dbg (+ 10 20))
(dbg (+ 10 20 3) :level 3)
(dbg (+ 10 20 5) :level 5))


(defn my-add [a b]
(dbg (+ a b) :level 2))

(defn my-sub [a b]
(dbg (- a b) :level 3))

(with-level 3
(dbg (my-add 10 20))
(dbg (my-sub 100 10))

(with-level 0
(dbg (* 10 2))))
(transduce (comp (debug) (map inc) (debug) (filter odd?)) conj (range 10))
38 changes: 38 additions & 0 deletions src/debux/common/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@
(str/join "\n")))
(flush) ))

;; print xform
(defn print-xform-header-with-indent
[form indent-level]
(println (prepend-bullets form indent-level))
(flush))

(defn pprint-xform-with-indent
[mark input-or-output indent-level]
(let [pprint (str/trim (with-out-str (pp/pprint input-or-output)))
bullets (make-bullets (or indent-level 1))
prefix1 (str bullets mark " ")
prefix2 (str bullets " ")]
(println (->> (str/split pprint #"\n")
(map-indexed #(if (zero? %1)
(str prefix1 %2)
(str prefix2 %2)))
(str/join "\n")))
(flush) ))

(defn insert-blank-line []
#?(:clj (do (println " ") (flush))
:cljs (.log js/console " ") ))
Expand Down Expand Up @@ -401,3 +420,22 @@
(print-form-with-indent (form-header '~form))
(pprint-result-with-indent result#)
result#))

(defn print-xform [quoted-xform indent-level]
(fn [rf]
(fn ([] (rf))
([result] (rf result))
([result input]
#_(when indent-level
(print-xform-header-with-indent quoted-xform indent-level))
(pprint-xform-with-indent ">" input indent-level)
(let [output (rf result input)]
(pprint-xform-with-indent"<" output indent-level)
(when (or (nil? indent-level)
(= 1 indent-level))
(insert-blank-line))
output) ))))

(defmacro spy-xform
[xform & [indent-level]]
`(print-xform '~xform ~indent-level))
12 changes: 12 additions & 0 deletions src/debux/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns debux.core
(:require [debux.dbg :as dbg]
[debux.dbgn :as dbgn]
[debux.dbgt :as dbgt]
[debux.cs.core :as cs]
[debux.macro-types :as mt]
[debux.common.util :as ut] ))
Expand Down Expand Up @@ -48,6 +49,17 @@
~(ut/parse-opts opts')))
~form)))

(defmacro dbgt [form & opts]
(let [ns (str *ns*)
line (:line (meta &form))
local-ks (keys &env)
opts' (ut/prepend-src-info opts ns line)]
`(if (ut/debug-enabled? ~ns)
(locking locking*
(dbgt/dbgt ~form (zipmap '~local-ks [~@local-ks])
~(ut/parse-opts opts')))
~form)))

(defmacro dbg-last
[& args]
(let [form (last args)
Expand Down
60 changes: 60 additions & 0 deletions src/debux/dbgt.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(ns debux.dbgt
(:require [debux.common.util :as ut]))

(defmacro dbgt-base
[form locals {:keys [level condition ns line msg n] :as opts} body]
`(let [condition# ~condition]
(if (and (>= (or ~level 0) ut/*debug-level*)
(or ~(not (contains? opts :condition))
condition#))
(binding [ut/*indent-level* (inc ut/*indent-level*)]
(let [src-info# (str (ut/src-info ~ns ~line))
title# (str "dbgt: " (ut/truncate (pr-str '~form))
(and ~msg (str " <" ~msg ">")))
locals# ~locals]
(ut/insert-blank-line)
(ut/print-title-with-indent src-info# title#)

(when ~(:locals opts)
(ut/pprint-locals-with-indent locals#)
(ut/insert-blank-line))

(binding [*print-length* (or ~n (:print-length @ut/config*))]
~body) ))
~form) ))

(defmacro dbg-xforms
[[_ & xforms :as form] locals opts]
(let [xforms' (map-indexed #(vector `(ut/spy-xform ~%2 ~(inc %1))
`~%2)
xforms)]
`(dbgt-base ~form ~locals ~opts
(comp ~@(apply concat xforms') ))))

(defmacro dbg-xform
[xform locals opts]
`(dbgt-base ~xform ~locals ~opts
(comp (ut/spy-xform ~xform) ~xform) ))

(def ^:private dbgt*
'#{clojure.core/comp cljs.core/comp})

;; for debugging transducers
(defmacro dbgt
[form locals & [{:as opts}]]
(if (and (list? form)
(dbgt* (ut/ns-symbol (first form) &env)))
`(dbg-xforms ~form ~locals ~opts)
`(dbg-xform ~form ~locals ~opts)))

(comment

(dbgt (map inc) {} {:ns "ns" :line 12})
(transduce (dbgt (filter even?) {} {:ns "ns" :line 12})
conj (range 10))

(dbgt (comp (map inc) (filter odd?)) {} {:ns "ns" :line 12})
(transduce (dbgt (comp (map inc) (filter even?)) {} {:ns "ns" :line 12})
conj (range 10))

) ; end of comment

0 comments on commit 977ba30

Please sign in to comment.