forked from weavejester/eftest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes capture output buffer not clearing.
- Loading branch information
1 parent
ed75d52
commit 163942e
Showing
3 changed files
with
63 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,56 @@ | ||
(ns eftest.output-capture | ||
(:require [clojure.string :as str]) | ||
(:import (java.io OutputStream ByteArrayOutputStream PrintStream PrintWriter) | ||
(java.nio.charset StandardCharsets))) | ||
(:import [java.io OutputStream ByteArrayOutputStream PrintStream PrintWriter])) | ||
|
||
(def captured-output-contexts (atom {})) | ||
(def context (atom {})) | ||
|
||
(defn thread-id [] | ||
(-> (Thread/currentThread) | ||
(.getId))) | ||
|
||
(defn init-capture-buffer [buffer] | ||
(defn- init-buffer [buffer] | ||
(or buffer (ByteArrayOutputStream.))) | ||
|
||
(defn get-capture-buffer ^ByteArrayOutputStream [] | ||
(let [id (thread-id)] | ||
(-> (swap! captured-output-contexts update id init-capture-buffer) | ||
(get id)))) | ||
|
||
(defn flush-captured-output ^String [] | ||
(let [output (some-> (get-capture-buffer) | ||
.toByteArray | ||
(String. StandardCharsets/UTF_8) | ||
str/trim)] | ||
(when-not (str/blank? output) | ||
output))) | ||
(defn- get-local-buffer ^ByteArrayOutputStream [] | ||
(let [id (.getId (Thread/currentThread))] | ||
(-> (swap! context update id init-buffer) (get id)))) | ||
|
||
(defn create-proxy-output-stream ^OutputStream [] | ||
(defn- create-proxy-output-stream ^OutputStream [] | ||
(proxy [OutputStream] [] | ||
(write | ||
([data] | ||
(when-let [target (get-capture-buffer)] | ||
(when-let [target (get-local-buffer)] | ||
(if (instance? Integer data) | ||
(.write target ^int data) | ||
(.write target ^bytes data 0 (alength ^bytes data))))) | ||
([data off len] | ||
(when-let [target (get-capture-buffer)] | ||
(when-let [target (get-local-buffer)] | ||
(.write target data off len)))))) | ||
|
||
(defn init-capture [] | ||
(let [old-out System/out | ||
old-err System/err | ||
(reset! context {}) | ||
(let [old-out System/out | ||
old-err System/err | ||
proxy-output-stream (create-proxy-output-stream) | ||
new-stream (PrintStream. proxy-output-stream) | ||
new-writer (PrintWriter. proxy-output-stream)] | ||
new-stream (PrintStream. proxy-output-stream) | ||
new-writer (PrintWriter. proxy-output-stream)] | ||
(System/setOut new-stream) | ||
(System/setErr new-stream) | ||
{:captured-writer new-writer | ||
:old-system-out old-out | ||
:old-system-err old-err})) | ||
:old-system-out old-out | ||
:old-system-err old-err})) | ||
|
||
(defn restore-capture [{:keys [old-system-out old-system-err]}] | ||
(System/setOut old-system-out) | ||
(System/setErr old-system-err)) | ||
|
||
(defmacro with-capture [& body] | ||
`(let [context# (init-capture) | ||
writer# (:captured-writer context#)] | ||
(try | ||
(binding [*out* writer#, *err* writer#] | ||
~@body) | ||
(finally | ||
(restore-capture context#))))) | ||
|
||
(defn clear-local-output [] | ||
(.reset (get-local-buffer))) | ||
|
||
(defn read-local-output [] | ||
(some-> (get-local-buffer) .toByteArray String. str/trim)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters