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 2 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
3 changes: 2 additions & 1 deletion src/etaoin/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2894,6 +2894,7 @@
proxy
profile
headless
log-files
Copy link
Collaborator

Choose a reason for hiding this comment

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

тут не очень, что файлы слиплись в одну мапу. Лучше отдельными ключами log-stdout и log-stderr

log-level
args-driver
path-driver
Expand Down Expand Up @@ -2939,7 +2940,7 @@

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-files)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

тут их нужно будет передать мапой, вроде

{:log-stdout log-stdout
 :log-stderr log-stderr}

(swap! driver assoc
:env env ;; todo process env
:process process)
Expand Down
45 changes: 29 additions & 16 deletions src/etaoin/proc.clj
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
(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))

(defn get-null-file ^java.io.File
[]
(if-let [windows? (str/starts-with? (System/getProperty "os.name") "Windows")]
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.

тут переменная windows? не нужна, поэтому просто if. Но еще лучше вынести ее на уровне def, потому что операционка не меняется в рантайме.

(def windows? ...)

(if windows? 
  ...
  ...)

(io/file "NUL")
(io/file "/dev/null")))

(defn get-log-file
[file-path]
(if file-path (io/file file-path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

положительную ветку перенести на след. строку после условия

(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