Housekeeping for [#93] (f3347c4)

This commit is contained in:
Peter Taoussanis 2015-02-17 11:29:05 +07:00
parent 614460790a
commit f9ae244f55

View File

@ -3,9 +3,9 @@
(:require [clojure.string :as str] (:require [clojure.string :as str]
[io.aviso.exception :as aviso-ex] [io.aviso.exception :as aviso-ex]
[taoensso.encore :as enc]) [taoensso.encore :as enc])
(:import java.io.File (:import [java.util Date Locale]
[java.util Date Locale] [java.text SimpleDateFormat]
[java.text SimpleDateFormat])) [java.io File]))
;;;; Encore version check ;;;; Encore version check
@ -97,6 +97,27 @@
(:current-level config) (:current-level config)
@level-atom)))) @level-atom))))
;;;;
(def ^:private get-hostname
(enc/memoize* 60000
(fn []
(->
(future ; Android doesn't like this on the main thread
(try (.. java.net.InetAddress getLocalHost getHostName)
(catch java.net.UnknownHostException _
"UnknownHost")))
(deref 5000 "UnknownHost")))))
(def ^:private ensure-spit-dir-exists!
(enc/memoize* 60000
(fn [fname]
(when-not (str/blank? fname)
(let [file (File. ^String fname)
dir (.getParentFile (.getCanonicalFile file))]
(when-not (.exists dir)
(.mkdirs dir)))))))
;;;; Default configuration and appenders ;;;; Default configuration and appenders
(defn default-fmt-output-fn (defn default-fmt-output-fn
@ -187,7 +208,8 @@
:min-level nil :enabled? false :async? false :rate-limit nil :min-level nil :enabled? false :async? false :rate-limit nil
:fn (fn [{:keys [ap-config output]}] ; Can use any appender args :fn (fn [{:keys [ap-config output]}] ; Can use any appender args
(when-let [filename (:spit-filename ap-config)] (when-let [filename (:spit-filename ap-config)]
(try (spit filename (str output "\n") :append true) (try (ensure-spit-dir-exists! filename)
(spit filename (str output "\n") :append true)
(catch java.io.IOException _))))}}}) (catch java.io.IOException _))))}}})
(enc/defonce* config (atom example-config)) (enc/defonce* config (atom example-config))
@ -260,31 +282,11 @@
(fn [apfn-args] ; Runtime: (fn [apfn-args] ; Runtime:
(send-off agent (fn [_] (apfn apfn-args))))))))))) (send-off agent (fn [_] (apfn apfn-args)))))))))))
(def ^:private get-hostname
(enc/memoize* 60000
(fn []
(->
(future ; Android doesn't like this on the main thread
(try (.. java.net.InetAddress getLocalHost getHostName)
(catch java.net.UnknownHostException _
"UnknownHost")))
(deref 5000 "UnknownHost")))))
(defn- exists?
[path]
(when-not (str/blank? path) (.exists (File. ^String path))))
(defn- mkdirs "Creates all parent directories for the passed file."
[^File f]
(.mkdirs (.getParentFile (.getCanonicalFile f))))
(defn- wrap-appender-juxt (defn- wrap-appender-juxt
"Wraps compile-time appender juxt with additional runtime capabilities "Wraps compile-time appender juxt with additional runtime capabilities
(incl. middleware) controlled by compile-time config. Like `wrap-appender-fn` (incl. middleware) controlled by compile-time config. Like `wrap-appender-fn`
but operates on the entire juxt at once." but operates on the entire juxt at once."
[config juxtfn] [config juxtfn]
(when-let [spit-file (get-in config [:shared-appender-config :spit-filename])]
(when-not (exists? spit-file) (mkdirs (File. spit-file))))
(->> ; Wrapping applies per juxt, bottom-to-top (->> ; Wrapping applies per juxt, bottom-to-top
juxtfn juxtfn