Skip to content

Commit

Permalink
Add closure-define to enable/disable tracing
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
danielcompton committed Mar 14, 2018
1 parent b7ee93c commit 144c923
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ All notable changes to this project will be documented in this file. This change
## [0.5.0] - Unreleased

Initial release after fork from debux.

### Added

* Closure-define to enable/disable tracing. Defaults to disabled, and is removed by Closure dead code elimination. To enable/disable tracing, set your compiler options to `:closure-defines {"debux.cs.core.trace_enabled_QMARK_" true}`
36 changes: 29 additions & 7 deletions src/debux/core.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
(ns debux.core
(:require [debux.dbgn :as dbgn]
[debux.macro-types :as mt]
[debux.common.util :as ut]))
[debux.common.util :as ut]
[clojure.walk :as walk]))

(def reset-indent-level! ut/reset-indent-level!)
(def set-print-seq-length! ut/set-print-seq-length!)

(def ^boolean trace-enabled? false)

(defn ^boolean is-trace-enabled?
"See https://groups.google.com/d/msg/clojurescript/jk43kmYiMhA/IHglVr_TPdgJ for more details"
[]
trace-enabled?)


;;; debugging APIs

(defmacro dbgn [form & opts]
Expand Down Expand Up @@ -36,22 +45,35 @@
(dbgn/dbgn ~@form {})
#_(trace-fn-call '~name f# args#))))

(defn clean-up-macro [form]
(walk/postwalk (fn [x] (if (and (symbol? x) (= "clojure.core" (namespace x)))
(symbol (name x))
x))
form))


(defmacro fntrace
[& definition]
(let [args (first definition)
form (rest definition)]
`(fn ~args
(dbgn/dbgn ~@form {}))))
`(if (is-trace-enabled?)
(fn ~args
(debux.dbgn/dbgn ~@form {}))
(fn ~@definition))))



;(fntrace [x] (inc x))

#_(defmacro deftrace2
[form]
`(dbgn/dbgn ~form {}))

#_(deftrace simple-fn "" [inte missing]
(let [a inte]
(->> (inc a)
inc
dec)))
(let [a inte]
(->> (inc a)
inc
dec)))



Expand Down
22 changes: 17 additions & 5 deletions src/debux/cs/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

#?(:cljs (enable-console-print!))

#?(:cljs (goog-define trace-enabled? false)
:clj (def ^boolean trace-enabled? false))

(defn ^boolean is-trace-enabled?
"See https://groups.google.com/d/msg/clojurescript/jk43kmYiMhA/IHglVr_TPdgJ for more details"
[]
trace-enabled?)

(def reset-indent-level! ut/reset-indent-level!)
(def set-print-seq-length! ut/set-print-seq-length!)

Expand Down Expand Up @@ -34,13 +42,17 @@
fn-form (if (string? (first definition)) (rest definition) definition)
form (rest fn-form)
arg-list (first fn-form)]
`(defn ~name ~doc-string ~arg-list
(debux.dbgn/dbgn ~@form {})
#_(trace-fn-call '~name f# args#))))
`(if (is-trace-enabled?)
(defn ~name ~doc-string ~arg-list
(debux.dbgn/dbgn ~@form {})
#_(trace-fn-call '~name f# args#))
(defn ~name ~@definition))))

(defmacro fntrace
[& definition]
(let [args (first definition)
form (rest definition)]
`(fn ~args
(debux.dbgn/dbgn ~@form {}))))
`(if (is-trace-enabled?)
(fn ~args
(debux.dbgn/dbgn ~@form {}))
(fn ~@definition))))

0 comments on commit 144c923

Please sign in to comment.