Skip to content

Commit

Permalink
feat: testing alter-var-root for tracing all-ns fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe-gsilva committed Dec 2, 2024
1 parent 9f67485 commit c0a2b0c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/back/api/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
(migrations/migrate (migrations/build-complete-db-config "back/config.edn"))
(->> system-map
component/start
(reset! system-atom)))
(reset! system-atom))
(trace-all-ns))

(defn stop-system! []
(logs/log :info "stopping system")
Expand Down
8 changes: 8 additions & 0 deletions src/back/api/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
(if v
(assoc m k v)
m))

(defn inspect
"Inspects a variable's contents and returns it without modifying its value."
[v]
(if (instance? clojure.lang.Atom v)
(prn @v)
(prn v))
v)
7 changes: 4 additions & 3 deletions src/dev/shadow/hooks.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@
target-file (io/file target)]
(cond
(not (.exists source-file))
(do (s.util/log {:type ::source-does-not-exist
:source source})
(constantly build-state))
(do
(s.util/log {:type ::source-does-not-exist
:source source})
(constantly build-state))

(and (= mode :dev)
(= (.lastModified source-file) (::source-last-mod build-state))
Expand Down
75 changes: 48 additions & 27 deletions src/dev/utils.clj
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
(ns dev.utils
(:require
[back.api.utils :as u]
[clojure.string :as str]
[com.moclojer.components.logs :as logs]
[com.moclojer.components.migrations :as migrations]
[com.stuartsierra.component :as component]
[dev.utils :as utils]
[pg-embedded-clj.core :as pg-emb]))

(defn start-system-dev!
([sys-atom sys-map]
(start-system-dev! sys-atom sys-map true)
(utils/trace-all-ns))
([sys-atom sys-map init-pg?]
(when init-pg?
(pg-emb/init-pg)
(migrations/migrate (migrations/build-complete-db-config "back/config.edn")))
(->> sys-map
component/start
(reset! sys-atom))))

(defn stop-system-dev!
([sys-atom]
(stop-system-dev! sys-atom true))
([sys-atom halt-pg?]
(logs/log :info "stopping system")
(swap!
sys-atom
(fn [s] (when s (component/stop s))))
(when halt-pg? (pg-emb/halt-pg!))))

(defn- get-allowed-ns []
(defn get-allowed-ns []
(->> (all-ns)
(map ns-name)
(map name)
Expand All @@ -38,7 +17,7 @@
(map symbol)
(into [])))

(defn trace-all-ns
(defn trace-all-ns-1
"Iterate over *ns* functions and replace them with themselves within
a `logs/trace` call that uses each function's arglist as context."
[]
Expand Down Expand Up @@ -76,6 +55,48 @@
(intern
*ns* fsym
(with-meta (eval `(fn [~@args]
(logs/trace ~sym ~argmap
(~func ~@callargs)))) m))
(logs/log "--> could not convert" func "args:" args)))))))
(com.moclojer.components.logs/trace ~sym ~argmap
(~func ~@callargs)))) m))
(prn "--> could not convert" func "args:" args)))))))

(defn trace-all-ns []
(let [a-ns (get-allowed-ns)
fn-names (atom [])]
(doseq [curr-ns a-ns]
(doseq [[sym v] (ns-publics curr-ns)]
(let [f @v
arg-names (map keyword (or (first (:arglists (meta v))) []))]
(swap! fn-names conj (str (:name (meta v))))
(alter-var-root v
(fn [_]
(with-meta
(fn [& args]
(com.moclojer.components.logs/trace
sym
(zipmap arg-names args)
(apply f args)))
(meta v)))))))))

(trace-all-ns)

(defn start-system-dev!
([sys-atom sys-map]
(start-system-dev! sys-atom sys-map true)
(utils/trace-all-ns))
([sys-atom sys-map init-pg?]
(when init-pg?
(pg-emb/init-pg)
(migrations/migrate (migrations/build-complete-db-config "back/config.edn")))
(->> sys-map
component/start
(reset! sys-atom))))

(defn stop-system-dev!
([sys-atom]
(stop-system-dev! sys-atom true))
([sys-atom halt-pg?]
(logs/log :info "stopping system")
(swap!
sys-atom
(fn [s] (when s (component/stop s))))
(when halt-pg? (pg-emb/halt-pg!))))

0 comments on commit c0a2b0c

Please sign in to comment.