Skip to content

Commit

Permalink
[wip] Scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Mar 3, 2024
1 parent f066e09 commit edd7876
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 16 deletions.
130 changes: 120 additions & 10 deletions src/taoensso/telemere.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
;; - Update Timbre (signal API, config API, signal fields, backport improvements)

;;;; TODO
;; - Finish system streams interop (see scratch)
;; - `clojure.tools.logging/log-capture!`, `with-logs`, etc.
;; - Via Timbre: core handlers, any last utils?
;; - Cljs (.log js/console <js/Error>) better than string stacktrace (clickable, etc.)
;;
;; - Tests for utils (hostname, formatters, etc.)?
;; - Remaining docstrings and TODOs
;; - Document kinds: #{:log :spy :trace :event :error <user>}
;; - Document kinds: #{:log :spy :trace :event :error :system/out :system/err <user>}
;; - General polish
;;
;; - Reading plan
Expand Down Expand Up @@ -330,21 +331,27 @@
(when-let [cause (ex-cause error)] (str nl nl "Caused by:" nl (format-error cause))))))

:clj
([{:keys [fonts sort]
;; TODO Review API, esp. re: *color-enabled*, etc.
([{:keys [use-fonts? sort-stacktrace-by fonts]
:or
{fonts clj-commons.format.exceptions/default-fonts
sort :chronological #_:depth-first}}
{use-fonts? :auto
sort-stacktrace-by :chronological #_:depth-first
fonts clj-commons.format.exceptions/default-fonts}}
error]

(binding [fmt-ansi/*color-enabled* (not (empty? fonts))
fmt-ex/*fonts* fonts
(binding [fmt-ansi/*color-enabled*
(if (enc/identical-kw? use-fonts? :auto)
nil ; => Guess based on environment
use-fonts?)

fmt-ex/*fonts* fonts
fmt-ex/*traditional*
(case sort
(case sort-stacktrace-by
:depth-first true ; Traditional
:chronological false ; Modern (default)
(enc/unexpected-arg! sort
(enc/unexpected-arg! sort-stacktrace-by
{:context `format-error
:param 'sort
:param 'sort-stacktrace-by
:expected #{:depth-first :chronological}}))]

(fmt-ex/format-exception error)))))
Expand Down Expand Up @@ -375,6 +382,8 @@

(= (force (get signal :msg_)) msg))))

#?(:clj (declare system-streams->telemere?))

#?(:clj
(defn interop-check
"Tests Telemere's interop with `clojure.tools.logging` and SLF4J.
Expand Down Expand Up @@ -403,7 +412,18 @@
(org.slf4j.LoggerFactory/getLogger "InteropTestTelemereLogger")]

{:send->telemere? (instance? com.taoensso.telemere.slf4j.TelemereLogger sl)
:receiving? (interop-test! "Interop test: SLF4J->Telemere" (fn [msg] (.info sl msg)))}))))})))
:receiving? (interop-test! "Interop test: SLF4J->Telemere" (fn [msg] (.info sl msg)))}))))

:system-streams
{:send->telemere? (system-streams->telemere?)
:receiving?
(interop-test!
"Interop test: system-streams->Telemere"
(fn [msg]
;; TODO
;; (binding [*out*])
;; (println msg)
))}})))

(comment (check-interop))

Expand All @@ -429,3 +449,93 @@
(signal! {:level :info, :run "run", :allow? true })
(signal! {:level :info, :run "run", :trace? false})
(signal! {:level :info, :run "run"}))]))

;;;; TODO Scratch
;; - Keep this?
;; - Change anything?
;; - Interop test?

#?(:clj (enc/defonce ^:private ^:dynamic *orig-out* "?*out*" nil))
#?(:clj (enc/defonce ^:private ^:dynamic *orig-err* "?*err*" nil))
#?(:clj (enc/defonce ^:private orig-streams_ "?[<System/out> <System/err>]" (atom nil)))

#?(:clj (def default-system-out-opts {:kind :system/out, :level :info}))
#?(:clj (def default-system-err-opts {:kind :system/err, :level :error}))

#?(:clj
(defn- print-stream
"TODO On each flush"
^java.io.PrintStream [{:as sig-opts :keys [kind level id]}]
(let [baos
(proxy [java.io.ByteArrayOutputStream] []
(flush []
(let [^java.io.ByteArrayOutputStream this this]
(proxy-super flush)
(let [msg (.toString this)]
(proxy-super reset)
(when-not (.isEmpty msg)
(binding [*out* *orig-out*, *err* *orig-err*]
(impl/signal!
{:location nil
:ns nil
:kind kind
:level level
:id id
:msg msg})))))))]

(java.io.PrintStream. baos true
java.nio.charset.StandardCharsets/UTF_8))))

#?(:clj
(defmacro with-system-streams->telemere
"TODO On each flush"
([form] `(with-system-streams->telemere nil ~form))
([{:keys [out err]
:or
{out default-system-out-opts
err default-system-err-opts}} form]

`(binding [*orig-out* (or *orig-out* *out*)
*orig-err* (or *orig-err* *err*)
*out* (if-let [out# ~out] (java.io.OutputStreamWriter. (print-stream out#)) *out*)
*err* (if-let [err# ~err] (java.io.OutputStreamWriter. (print-stream err#)) *err*)]
~form))))

(comment
(with-handler :hid1 (fn [x] (println x)) {}
(do (with-system-streams->telemere (println "hello")))))

#?(:clj
(let [monitor (Object.)]

(defn system-streams->reset!
"TODO"
[]
(locking monitor
(when-let [[[out err]] (reset-vals! orig-streams_ nil)]
(System/setOut out)
(System/setErr err)))
true)

(defn- system-streams->telemere? [] (boolean @orig-streams_))
(defn system-streams->telemere!
"TODO"
([] (system-streams->telemere! nil))

([{:keys [out err]
:or
{out default-system-out-opts
err default-system-err-opts}}]

(let [out (when out (print-stream out))
err (when err (print-stream err))]

(locking monitor
(compare-and-set! orig-streams_ nil [System/out System/err])
(when out (System/setOut out))
(when err (System/setErr err)))
true)))))

(comment
(system-streams->telemere! {})
(system-streams->reset!))
25 changes: 19 additions & 6 deletions test/taoensso/telemere_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#?(:clj (defmacro wsv [form] `(impl/-with-signal (fn [] ~form) {:force-msg? true, :return :signal})))

(do
(def ^:private ex1 (ex-info "TestEx" {}))
(def ^:private ex1? #(= % ex1))
(def ^:private ex1-rv? #(= (:error %) ex1))
(def ^:private ex1-pred (enc/pred ex1?)))
(def ex1 (ex-info "TestEx" {}))
(def ex1? #(= % ex1))
(def ex1-rv? #(= (:error %) ex1))
(def ex1-pred (enc/pred ex1?)))

(def ^:dynamic *dynamic-var* nil)

;;;;

Expand Down Expand Up @@ -255,9 +257,20 @@
(is (= c1 7) "1x run + 4x handler middleware + 2x call middleware")
(is (= c2 8) "2x run + 4x handler middleware + 2x call middleware")
(is (= c3 15) "3x run + 8x handler middleware + 4x call middleware")
(is (= c4 19) "3x run + 12x handler middleware + 4x call middleware")]))))])
(is (= c4 19) "3x run + 12x handler middleware + 4x call middleware")]))))

(testing "Handler binding conveyance"
(let [a (atom nil)
wh1
(sigs/wrap-handler :hid1 (fn [x] (reset! a *dynamic-var*))
nil #?(:clj {:async {:mode :dropping}} :cljs nil))]

(binding [*dynamic-var* "bound", impl/*sig-handlers* [wh1]] (sig! {:level :info}))

#?(:clj (Thread/sleep 500))
(is (= @a "bound"))))])

(def ^:private ^:dynamic *throwing-handler-middleware?* false)
(def ^:dynamic *throwing-handler-middleware?* false)

(deftest _throwing
(let [sv_ (atom :nx)
Expand Down

0 comments on commit edd7876

Please sign in to comment.