From 691685fe5ccc45b520290f3106035a28e1779f5f Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Fri, 31 Jul 2020 13:26:33 +0300 Subject: [PATCH 1/5] add logging to files --- src/etaoin/api.clj | 3 ++- src/etaoin/proc.clj | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index c1ae7468..47df4603 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -2894,6 +2894,7 @@ proxy profile headless + log-files log-level args-driver path-driver @@ -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)] (swap! driver assoc :env env ;; todo process env :process process) diff --git a/src/etaoin/proc.clj b/src/etaoin/proc.clj index e4115a6a..1a0330d8 100644 --- a/src/etaoin/proc.clj +++ b/src/etaoin/proc.clj @@ -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")] + (io/file "NUL") + (io/file "/dev/null"))) + +(defn get-log-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 From fadf01d5272040efb97d87d0d04fd9f3259ddc16 Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Fri, 31 Jul 2020 13:51:19 +0300 Subject: [PATCH 2/5] fix test --- test/etaoin/api_test2.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/etaoin/api_test2.clj b/test/etaoin/api_test2.clj index a5b26d4b..6027966b 100644 --- a/test/etaoin/api_test2.clj +++ b/test/etaoin/api_test2.clj @@ -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 {})) From ada7004263730a292c6aeef2035da25d37db2d56 Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Fri, 31 Jul 2020 15:45:03 +0300 Subject: [PATCH 3/5] update --- README.md | 4 ++++ src/etaoin/api.clj | 6 ++++-- src/etaoin/proc.clj | 11 +++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b8745840..389d85ad 100644 --- a/README.md +++ b/README.md @@ -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 + :log-stdout + :log-stderr ;; Path to a custorm browser profile. See the section below. :profile "/Users/ivan/Library/Application Support/Firefox/Profiles/iy4iitbg.Test" diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index 47df4603..678ccde2 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -2894,8 +2894,9 @@ proxy profile headless - log-files log-level + log-stdout + log-stderr args-driver path-driver download-dir @@ -2940,7 +2941,8 @@ proc-args (drv/get-args @driver) _ (log/debugf "Starting process: %s" (str/join \space proc-args)) - process (proc/run proc-args log-files)] + process (proc/run proc-args {:log-stdout log-stdout + :log-sttderr log-stderr})] (swap! driver assoc :env env ;; todo process env :process process) diff --git a/src/etaoin/proc.clj b/src/etaoin/proc.clj index 1a0330d8..ae0768c3 100644 --- a/src/etaoin/proc.clj +++ b/src/etaoin/proc.clj @@ -4,16 +4,19 @@ (: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-let [windows? (str/starts-with? (System/getProperty "os.name") "Windows")] + (if windows? (io/file "NUL") (io/file "/dev/null"))) -(defn get-log-file +(defn get-log-file ^java.io.File [file-path] - (if file-path (io/file file-path) - (get-null-file))) + (if file-path + (io/file file-path) + (get-null-file))) (defn java-params ^"[Ljava.lang.String;" [params] (->> params From 2e8d0743b16d1e629d72f763cbb5603b7dd1435d Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Fri, 31 Jul 2020 15:58:26 +0300 Subject: [PATCH 4/5] update --- README.md | 3 ++- src/etaoin/proc.clj | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 389d85ad..45e5b6cb 100644 --- a/README.md +++ b/README.md @@ -987,7 +987,8 @@ skipped or have nil values. Some of them, if not passed, are taken from the ;; :all is set. :log-level :err ;; to show only errors but not debug - ;; Paths to log files +;; 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 diff --git a/src/etaoin/proc.clj b/src/etaoin/proc.clj index ae0768c3..6b510d68 100644 --- a/src/etaoin/proc.clj +++ b/src/etaoin/proc.clj @@ -4,7 +4,7 @@ (:import java.lang.IllegalThreadStateException java.io.IOException)) -(def windows? (str/starts-with? (System/getProperty "os.name") "Windows")) +(def windows? (str/starts-with? (System/getProperty "os.name") "Windows")) (defn get-null-file ^java.io.File [] From 364a1173812ab4b4f5c981d8741aa4b897bb6016 Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Fri, 31 Jul 2020 16:01:47 +0300 Subject: [PATCH 5/5] fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 45e5b6cb..82897ae6 100644 --- a/README.md +++ b/README.md @@ -987,8 +987,8 @@ skipped or have nil values. Some of them, if not passed, are taken from the ;; :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) + ;; 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