Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spit-appender lock for graalvm #334

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions src/taoensso/timbre/appenders/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@

;;;; Spit appender (clj only)

#?(:clj
(defn- write-to-file [data fname append? output self]
(try
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String output)
(.newLine w))

(catch java.io.IOException e
(if (:spit-appender/retry? data)
(throw e) ; Unexpected error
(do
(jio/make-parents fname)
(self (assoc data :spit-appender/retry? true))))))))

#?(:clj
(defn spit-appender
"Returns a simple `spit` file appender for Clojure."
Expand All @@ -82,29 +96,15 @@

(have? enc/nblank-str? fname)

{:enabled? true
:fn
(let [lock (Object.)]
(fn self [data]
(let [{:keys [output_]} data
output (force output_)
locklocal lock ; Ensure obj on stack for verifiers, Ref. CLJ-1472
]
(try
(when locking? (monitor-enter locklocal)) ; For thread safety, Ref. #251
(with-open [^java.io.BufferedWriter w (jio/writer fname :append append?)]
(.write w ^String output)
(.newLine w))

(catch java.io.IOException e
(if (:spit-appender/retry? data)
(throw e) ; Unexpected error
(do
(jio/make-parents fname)
(self (assoc data :spit-appender/retry? true)))))
(finally
(when locking?
(monitor-exit locklocal)))))))}))
(let [lock (Object.)]
{:enabled? true
:fn
(fn self [{:keys [output_] :as data}]
(let [output (force output_)]
(if locking?
(locking lock
(write-to-file data fname append? output self))
(write-to-file data fname append? output self))))})))

(comment
(spit-appender)
Expand Down