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

add logging to files #257

Merged
merged 5 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,10 @@ 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 log files
Copy link
Collaborator

@igrishaev igrishaev Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут бы уточнить, что к логам именно вебдрайвера, а то не понятно

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

: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"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут двойной пробел после with?


(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