diff --git a/src/pallet/common/context.clj b/src/pallet/common/context.clj index d50a585..5cdde92 100644 --- a/src/pallet/common/context.clj +++ b/src/pallet/common/context.clj @@ -243,7 +243,7 @@ (defmacro context-history [{:keys [history-kw limit] :or {history-kw :history limit 100}}] - `(fn context-history [context# entry#] + `(fn ~'context-history [context# entry#] (when entry# {::history-kw ~history-kw ~history-kw diff --git a/src/pallet/common/logging/logutils.clj b/src/pallet/common/logging/logutils.clj index fd9aa25..dc6e754 100644 --- a/src/pallet/common/logging/logutils.clj +++ b/src/pallet/common/logging/logutils.clj @@ -11,96 +11,18 @@ (require ns) (catch Exception _))) +(defn- old-version-of-tools-logging? [] + (try + (Class/forName "clojure.tools.logging.Log") + (catch ClassNotFoundException _))) (defmacro tools-logging-compat [] - (if (try - (Class/forName "clojure.tools.logging.Log") - (catch ClassNotFoundException _)) - `(do - ;; pre tools.logging 0.2.0 - (deftype ~'NullLogger - [] - clojure.tools.logging.Log - (impl-enabled? [log# level#] false) - (impl-write! [log# level# throwable# message#])) - (def null-log (delay (NullLogger.))) - - (deftype ~'NullLoggerFactory - [] - clojure.tools.logging.LogFactory - (impl-name [factory#] "null logger") - (impl-get-log [factory# log-ns#] @null-log)) - (def null-logger-factory (delay (NullLoggerFactory.))) - -;;; A stdout logger -;;; Logs everyting to stdout. Can be useful to test logging. - (deftype ~'StdoutLogger - [] - clojure.tools.logging.Log - (impl-enabled? [log# level#] true) - (impl-write! [log# level# throwable# message#] - (println (name level#) message#) - (when throwable# - (stacktrace/print-stack-trace - (stacktrace/root-cause throwable#))))) - (def stdout-log (delay (StdoutLogger.))) - - (deftype ~'StdoutLoggerFactory - [] - clojure.tools.logging.LogFactory - (impl-name [factory#] "stdout logger") - (impl-get-log [factory# log-ns#] @stdout-log)) - (def stdout-logger-factory (delay (StdoutLoggerFactory.))) - - (defmacro with-logger-factory - [factory# & body#] - `(binding [logging/*log-factory* ~factory#] ~@body#))) - `(do - ;; tools.logging 0.2.0 and up -;;; A null logger -;;; Suppresses all logging. Can be useful to quiet test cases. - (deftype ~'NullLogger - [] - clojure.tools.logging.impl.Logger - (enabled? [log# level#] false) - (write! [log# level# throwable# message#])) - - (def null-log (delay (NullLogger.))) - - (deftype ~'NullLoggerFactory - [] - clojure.tools.logging.impl.LoggerFactory - (name [factory#] "null logger") - (get-logger [factory# log-ns#] @null-log)) - (def null-logger-factory (delay (NullLoggerFactory.))) - -;;; A stdout logger -;;; Logs everyting to stdout. Can be useful to test logging. - (deftype ~'StdoutLogger - [] - clojure.tools.logging.impl.Logger - (enabled? [log# level#] true) - (write! [log# level# throwable# message#] - (println (name level#) message#) - (when throwable# - (stacktrace/print-stack-trace - (stacktrace/root-cause throwable#))))) - (def stdout-log (delay (StdoutLogger.))) - - (deftype ~'StdoutLoggerFactory - [] - clojure.tools.logging.impl.LoggerFactory - (name [factory#] "stdout logger") - (get-logger [factory# log-ns#] @stdout-log)) - (def stdout-logger-factory (delay (StdoutLoggerFactory.))) - - (defmacro with-logger-factory - [factory# & body#] - `(binding [logging/*logger-factory* ~factory#] ~@body#))))) + (if (old-version-of-tools-logging?) + (load "logutils/tools_logging_pre_0_2") + (load "logutils/tools_logging_post_0_2"))) (tools-logging-compat) - ;;; Macros to use specific logging implementations in a given scope (defmacro logging-to-stdout "Send log messages to stdout for inspection" diff --git a/src/pallet/common/logging/logutils/tools_logging_post_0_2.clj b/src/pallet/common/logging/logutils/tools_logging_post_0_2.clj new file mode 100644 index 0000000..324b8b7 --- /dev/null +++ b/src/pallet/common/logging/logutils/tools_logging_post_0_2.clj @@ -0,0 +1,46 @@ +;;;; WARNING This file is loaded and not required + +;; tools.logging 0.2.0 and up +;;; A null logger +;;; Suppresses all logging. Can be useful to quiet test cases. +(deftype NullLogger + [] + clojure.tools.logging.impl.Logger + (enabled? [log level] false) + (write! [log level throwable message])) + +(def null-log (delay (NullLogger.))) + +(deftype NullLoggerFactory + [] + clojure.tools.logging.impl.LoggerFactory + (name [factory] "null logger") + (get-logger [factory log-ns] @null-log)) + +(def null-logger-factory (delay (NullLoggerFactory.))) + +;;; A stdout logger +;;; Logs everyting to stdout. Can be useful to test logging. +(deftype StdoutLogger + [] + clojure.tools.logging.impl.Logger + (enabled? [log level] true) + (write! [log level throwable message] + (println (name level) message) + (when throwable + (stacktrace/print-stack-trace + (stacktrace/root-cause throwable))))) + +(def stdout-log (delay (StdoutLogger.))) + +(deftype StdoutLoggerFactory + [] + clojure.tools.logging.impl.LoggerFactory + (name [factory] "stdout logger") + (get-logger [factory log-ns] @stdout-log)) + +(def stdout-logger-factory (delay (StdoutLoggerFactory.))) + +(defmacro with-logger-factory + [factory# & body#] + `(binding [logging/*logger-factory* ~factory#] ~@body#)) diff --git a/src/pallet/common/logging/logutils/tools_logging_pre_0_2.clj b/src/pallet/common/logging/logutils/tools_logging_pre_0_2.clj new file mode 100644 index 0000000..ea722b5 --- /dev/null +++ b/src/pallet/common/logging/logutils/tools_logging_pre_0_2.clj @@ -0,0 +1,44 @@ +;;;; WARNING This file is loaded and not required + +;; pre tools.logging 0.2.0 +(deftype NullLogger + [] + clojure.tools.logging.Log + (impl-enabled? [log level] false) + (impl-write! [log level throwable message])) + +(def null-log (delay (NullLogger.))) + +(deftype NullLoggerFactory + [] + clojure.tools.logging.LogFactory + (impl-name [factory] "null logger") + (impl-get-log [factory log-ns] @null-log)) + +(def null-logger-factory (delay (NullLoggerFactory.))) + +;;; A stdout logger +;;; Logs everyting to stdout. Can be useful to test logging. +(deftype StdoutLogger + [] + clojure.tools.logging.Log + (impl-enabled? [log level] true) + (impl-write! [log level throwable message] + (println (name level) message) + (when throwable + (stacktrace/print-stack-trace + (stacktrace/root-cause throwable))))) + +(def stdout-log (delay (StdoutLogger.))) + +(deftype StdoutLoggerFactory + [] + clojure.tools.logging.LogFactory + (impl-name [factory] "stdout logger") + (impl-get-log [factory log-ns] @stdout-log)) + +(def stdout-logger-factory (delay (StdoutLoggerFactory.))) + +(defmacro with-logger-factory + [factory# & body#] + `(binding [logging/*log-factory* ~factory#] ~@body#))