From 011de1b8aef0c03c46f8c2ed468945021a1b16e1 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 25 May 2021 09:16:00 +0300 Subject: [PATCH] Tracing of nested fx handlers on :trace logging level --- src/status_im/utils/fx.clj | 19 +++++++++++++++++-- src/status_im/utils/fx.cljs | 4 +++- src/status_im/utils/handlers.cljs | 4 ++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/status_im/utils/fx.clj b/src/status_im/utils/fx.clj index 76fdbdbc84..6cee20d9fe 100644 --- a/src/status_im/utils/fx.clj +++ b/src/status_im/utils/fx.clj @@ -10,6 +10,9 @@ (fn [cofx# [_# ~@argsyms]] (~name cofx# ~@argsyms)))) events)) +(defn- fully-qualified-name [sym] + (str *ns* "/" (name sym))) + (defmacro defn "Defines an fx producing function Takes the same arguments as the defn macro @@ -50,6 +53,18 @@ (clojure.core/defn ~(with-meta name m) ([~@argsyms] (fn [cofx#] (~(with-meta name m) cofx# ~@argsyms))) ([cofx# ~@args] + (when js/goog.DEBUG + (when (taoensso.timbre/level>= + :trace + (:level taoensso.timbre/*config*)) + (println + (clojure.string/join + (concat + (repeat + (deref status-im.utils.handlers/handler-nesting-level) + "│ ") + ["├─"])) + ~(str (clojure.core/name name) " " *ns*)))) (if (and (map? cofx#) (not (nil? (:db cofx#)))) (let [res# (let [~cofx cofx#] ~@fdecl)] @@ -61,9 +76,9 @@ m# (when (and (map? k#) (contains? k# :db)) - (throw (js/Error. (str "fx/defn's result is used as fx producing function in " ~name)))) + (throw (js/Error. (str "fx/defn's result is used as fx producing function in " ~(fully-qualified-name name))))) (get m# k# nil))))) res#) - (throw (js/Error. (str "fx/defn expects a map of cofx as first argument got " cofx# " in function " ~name)))))) + (throw (js/Error. (str "fx/defn expects a map of cofx as first argument got " cofx# " in function " ~(fully-qualified-name name))))))) ~@(register-events events interceptors (with-meta name m) argsyms)) (throw (Exception. (str "fx/defn expects a vector of keyword as value for :events key in attr-map in function " name)))))) diff --git a/src/status_im/utils/fx.cljs b/src/status_im/utils/fx.cljs index d6e9b0396b..93223e6e6e 100644 --- a/src/status_im/utils/fx.cljs +++ b/src/status_im/utils/fx.cljs @@ -2,7 +2,7 @@ (:require-macros status-im.utils.fx) (:require [status-im.ethereum.json-rpc :as json-rpc] [taoensso.timbre :as log] - status-im.utils.handlers) + [status-im.utils.handlers :as handlers]) (:refer-clojure :exclude [merge reduce])) (defn- update-db [cofx fx] @@ -47,6 +47,8 @@ :data-source/tx and effects are handled specially and their results (list of transactions) are compacted to one transactions list (for each effect). " [{:keys [db] :as cofx} & args] + (when js/goog.DEBUG + (swap! handlers/handler-nesting-level inc)) (let [[first-arg & rest-args] args initial-fxs? (map? first-arg) fx-fns (if initial-fxs? rest-args args)] diff --git a/src/status_im/utils/handlers.cljs b/src/status_im/utils/handlers.cljs index 7e2e961173..574ce3268a 100644 --- a/src/status_im/utils/handlers.cljs +++ b/src/status_im/utils/handlers.cljs @@ -36,12 +36,16 @@ (let [[first _] (get-coeffect ctx :event)] first)) +(def handler-nesting-level (atom 0)) + (def debug-handlers-names "Interceptor which logs debug information to js/console for each event." (->interceptor :id :debug-handlers-names :before (fn debug-handlers-names-before [context] + (when js/goog.DEBUG + (reset! handler-nesting-level 0)) (log/debug "Handling re-frame event: " (pretty-print-event context)) context)))