From d9137218650ea3ebd0ef5d1d59d5687bd4e420f4 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Tue, 15 Dec 2015 12:51:50 +0700 Subject: [PATCH] [#133] Faster + more flexible str joins (Clojure 1.7+) Needs transducers, otherwise falls back to the old method. --- src/taoensso/timbre.cljx | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/taoensso/timbre.cljx b/src/taoensso/timbre.cljx index a59826f..6c8f79f 100644 --- a/src/taoensso/timbre.cljx +++ b/src/taoensso/timbre.cljx @@ -5,21 +5,21 @@ (:require [clojure.string :as str] [io.aviso.exception :as aviso-ex] - [taoensso.encore :as enc :refer (have have? qb)] + [taoensso.encore :as enc :refer (compile-if have have? qb)] [taoensso.timbre.appenders.core :as core-appenders]) #+cljs (:require [clojure.string :as str] - [taoensso.encore :as enc :refer () :refer-macros (have have?)] + [taoensso.encore :as enc :refer () :refer-macros (compile-if have have?)] [taoensso.timbre.appenders.core :as core-appenders]) #+cljs (:require-macros [taoensso.timbre :as timbre-macros :refer ()])) (if (vector? taoensso.encore/encore-version) - (enc/assert-min-encore-version [2 26 0]) - (enc/assert-min-encore-version 2.26)) + (enc/assert-min-encore-version [2 29 1]) + (enc/assert-min-encore-version 2.29)) ;;;; Config @@ -222,6 +222,23 @@ ;;;; Utils +(enc/compile-if (do enc/str-join true) ; Encore v2.29.1+ with transducers + (defn- str-join [xs] + (enc/str-join " " + (map + (fn [x] + (let [x (enc/nil->str x)] ; Undefined, nil -> "nil" + (cond + (record? x) (pr-str x) + ;; (enc/lazy-seq? x) (pr-str x) ; Dubious? + :else x)))) + xs)) + (defn- str-join [xs] (enc/spaced-str-with-nils xs))) + +(comment + (defrecord MyRec [x]) + (str-join ["foo" (MyRec. "foo")])) + (defn- ->delay [x] (if (delay? x) x (delay x))) (defn- vsplit-err1 [[v1 :as v]] (if-not (enc/error? v1) [nil v] (enc/vsplit-first v))) (comment @@ -337,7 +354,7 @@ (when-not (nil? msg-type) (when-let [vargs (have [:or nil? vector?] (force vargs_))] (case msg-type - :p (enc/spaced-str vargs) + :p (str-join vargs) :f (let [[fmt args] (enc/vsplit-first vargs)] (enc/format* fmt args)))))) ?data @@ -581,7 +598,7 @@ ;;;; Deprecated -(defn str-println [& xs] (enc/spaced-str xs)) +(defn str-println [& xs] (str-join xs)) (defmacro with-log-level [level & body] `(with-level ~level ~@body)) (defmacro with-logging-config [config & body] `(with-config ~config ~@body)) (defn logging-enabled? [level compile-time-ns] (log? level (str compile-time-ns)))