Skip to content

Commit

Permalink
add logging to files (#257)
Browse files Browse the repository at this point in the history
* add logging to files

* fix test

* update

* update

* fix
  • Loading branch information
Uunnamed authored Jul 31, 2020
1 parent 75bebb7 commit 35f94c6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,11 @@ skipped or have nil values. Some of them, if not passed, are taken from the
;; :err (aliases :error, :severe, :crit, :critical), :all. When not passed,
;; :all is set.
:log-level :err ;; to show only errors but not debug

;; Paths to the driver's log files as strings.
;; When not set, the output goes to /dev/null (or NUL on Windows)
:log-stdout
:log-stderr

;; Path to a custorm browser profile. See the section below.
:profile "/Users/ivan/Library/Application Support/Firefox/Profiles/iy4iitbg.Test"
Expand Down
5 changes: 4 additions & 1 deletion src/etaoin/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,8 @@
profile
headless
log-level
log-stdout
log-stderr
args-driver
path-driver
download-dir
Expand Down Expand Up @@ -2939,7 +2941,8 @@

proc-args (drv/get-args @driver)
_ (log/debugf "Starting process: %s" (str/join \space proc-args))
process (proc/run proc-args)]
process (proc/run proc-args {:log-stdout log-stdout
:log-sttderr log-stderr})]
(swap! driver assoc
:env env ;; todo process env
:process process)
Expand Down
48 changes: 32 additions & 16 deletions src/etaoin/proc.clj
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
(ns etaoin.proc
(:require [clojure.java.io :as io])
(:import java.lang.Runtime
java.lang.IllegalThreadStateException
java.io.IOException))
(:require [clojure.java.io :as io]
[clojure.string :as str])
(:import java.lang.IllegalThreadStateException
java.io.IOException))

(def windows? (str/starts-with? (System/getProperty "os.name") "Windows"))

(defn get-null-file ^java.io.File
[]
(if windows?
(io/file "NUL")
(io/file "/dev/null")))

(defn get-log-file ^java.io.File
[file-path]
(if file-path
(io/file file-path)
(get-null-file)))

(defn java-params ^"[Ljava.lang.String;" [params]
(->> params
(map str)
(into-array String)))

(defn run [args]
(let [binary (first args)
readme-link "https://github.com/igrishaev/etaoin#installing-the-browser-drivers"
pb (java.lang.ProcessBuilder. (java-params args))]
(.redirectOutput pb (java.io.File/createTempFile "driver.out" ".log"))
(.redirectError pb (java.io.File/createTempFile "driver.err" ".log"))
(try
(.start pb)
(catch java.io.IOException e
(throw (ex-info
(format "Cannot find a binary file `%s` for the driver.
(defn run
([args] (run args {}))
([args {:keys [log-stdout log-stderr]}]
(let [binary (first args)
readme-link "https://github.com/igrishaev/etaoin#installing-the-browser-drivers"
pb (java.lang.ProcessBuilder. (java-params args))]
(.redirectOutput pb (get-log-file log-stdout))
(.redirectError pb (get-log-file log-stderr))
(try
(.start pb)
(catch java.io.IOException e
(throw (ex-info
(format "Cannot find a binary file `%s` for the driver.
Please ensure you have the driver installed and specify the path to it.
For driver installation, check out the official readme file from Etaoin: %s" binary readme-link)
{:args args} e))))))
{:args args} e)))))))

;; todo store those streams

Expand Down
2 changes: 1 addition & 1 deletion test/etaoin/api_test2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

(deftest test-firefox-driver-args
(let [args (atom [])]
(with-redefs-fn {#'etaoin.proc/run #(reset! args %)}
(with-redefs-fn {#'etaoin.proc/run (fn [a _] (reset! args a))}
#(do (testing "No custom args"
(-> (create-driver :firefox {:port 1234})
(run-driver {}))
Expand Down

0 comments on commit 35f94c6

Please sign in to comment.