Skip to content

Commit 7d37ad0

Browse files
authored
feat: RunOptions can specifying output/error streams (#426)
1 parent 7f49fdf commit 7d37ad0

File tree

8 files changed

+59
-58
lines changed

8 files changed

+59
-58
lines changed

binding_call.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package playwright
22

3-
import (
4-
"log"
5-
)
6-
73
type BindingCall interface {
84
Call(f BindingCallFunction)
95
}
@@ -31,7 +27,7 @@ func (b *bindingCallImpl) Call(f BindingCallFunction) {
3127
if _, err := b.channel.Send("reject", map[string]interface{}{
3228
"error": serializeError(r.(error)),
3329
}); err != nil {
34-
log.Printf("could not reject BindingCall: %v", err)
30+
logger.Printf("could not reject BindingCall: %v\n", err)
3531
}
3632
}
3733
}()
@@ -57,7 +53,7 @@ func (b *bindingCallImpl) Call(f BindingCallFunction) {
5753
"result": serializeArgument(result),
5854
})
5955
if err != nil {
60-
log.Printf("could not resolve BindingCall: %v", err)
56+
logger.Printf("could not resolve BindingCall: %v\n", err)
6157
}
6258
}
6359

browser_context.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"log"
87
"os"
98
"strings"
109
"sync"
@@ -528,7 +527,7 @@ func (b *browserContextImpl) onRoute(route *routeImpl) {
528527
return nil, err
529528
}, true)
530529
if err != nil {
531-
log.Printf("could not update interception patterns: %v", err)
530+
logger.Printf("could not update interception patterns: %v\n", err)
532531
}
533532
}
534533
}

channel.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package playwright
22

33
import (
4-
"log"
54
"reflect"
65
)
76

@@ -59,7 +58,7 @@ func (c *channel) SendNoReply(method string, options ...interface{}) {
5958
return c.connection.sendMessageToServer(c.owner, method, params, true)
6059
}, false)
6160
if err != nil {
62-
log.Printf("SendNoReply failed: %v", err)
61+
logger.Printf("SendNoReply failed: %v\n", err)
6362
}
6463
}
6564

har_router.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package playwright
22

33
import (
44
"errors"
5-
"log"
65
)
76

87
type harRouter struct {
@@ -20,7 +19,7 @@ func (r *harRouter) addContextRoute(context BrowserContext) error {
2019
err := context.Route(r.urlOrPredicate, func(route Route) {
2120
err := r.handle(route)
2221
if err != nil {
23-
log.Println(err)
22+
logger.Println(err)
2423
}
2524
})
2625
if err != nil {
@@ -36,7 +35,7 @@ func (r *harRouter) addPageRoute(page Page) error {
3635
err := page.Route(r.urlOrPredicate, func(route Route) {
3736
err := r.handle(route)
3837
if err != nil {
39-
log.Println(err)
38+
logger.Println(err)
4039
}
4140
})
4241
if err != nil {
@@ -87,7 +86,7 @@ func (r *harRouter) handle(route Route) error {
8786
Headers: deserializeNameAndValueToMap(response.Headers),
8887
})
8988
case "error":
90-
log.Printf("har action error: %v", *response.Message)
89+
logger.Printf("har action error: %v\n", *response.Message)
9190
fallthrough
9291
case "noentry":
9392
}

page.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/base64"
55
"errors"
66
"fmt"
7-
"log"
87
"os"
98
"sync"
109
"time"
@@ -926,7 +925,7 @@ func (p *pageImpl) onRoute(route *routeImpl) {
926925
return nil, err
927926
}, true)
928927
if err != nil {
929-
log.Printf("could not update interception patterns: %v", err)
928+
logger.Printf("could not update interception patterns: %v\n", err)
930929
}
931930
}
932931
}

run.go

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ const (
2121
playwrightCliVersion = "1.42.1"
2222
)
2323

24-
var playwrightCDNMirrors = []string{
25-
"https://playwright.azureedge.net",
26-
"https://playwright-akamai.azureedge.net",
27-
"https://playwright-verizon.azureedge.net",
28-
}
24+
var (
25+
logger = log.Default()
26+
playwrightCDNMirrors = []string{
27+
"https://playwright.azureedge.net",
28+
"https://playwright-akamai.azureedge.net",
29+
"https://playwright-verizon.azureedge.net",
30+
}
31+
)
2932

3033
type PlaywrightDriver struct {
3134
DriverDirectory, DriverBinaryLocation, Version string
@@ -96,35 +99,29 @@ func (d *PlaywrightDriver) Install() error {
9699
if d.options.SkipInstallBrowsers {
97100
return nil
98101
}
99-
if d.options.Verbose {
100-
log.Println("Downloading browsers...")
101-
}
102+
103+
d.log("Downloading browsers...")
102104
if err := d.installBrowsers(d.DriverBinaryLocation); err != nil {
103105
return fmt.Errorf("could not install browsers: %w", err)
104106
}
105-
if d.options.Verbose {
106-
log.Println("Downloaded browsers successfully")
107-
}
107+
d.log("Downloaded browsers successfully")
108+
108109
return nil
109110
}
110111

111112
// Uninstall removes the driver and the browsers.
112113
func (d *PlaywrightDriver) Uninstall() error {
113-
if d.options.Verbose {
114-
log.Println("Removing browsers...")
115-
}
114+
d.log("Removing browsers...")
116115
if err := d.uninstallBrowsers(d.DriverBinaryLocation); err != nil {
117116
return fmt.Errorf("could not uninstall browsers: %w", err)
118117
}
119-
if d.options.Verbose {
120-
log.Println("Removing driver...")
121-
}
118+
119+
d.log("Removing driver...")
122120
if err := os.RemoveAll(d.DriverDirectory); err != nil {
123121
return fmt.Errorf("could not remove driver directory: %w", err)
124122
}
125-
if d.options.Verbose {
126-
log.Println("Uninstall driver successfully")
127-
}
123+
124+
d.log("Uninstall driver successfully")
128125
return nil
129126
}
130127

@@ -138,9 +135,7 @@ func (d *PlaywrightDriver) DownloadDriver() error {
138135
return nil
139136
}
140137

141-
if d.options.Verbose {
142-
log.Printf("Downloading driver to %s", d.DriverDirectory)
143-
}
138+
d.log(fmt.Sprintf("Downloading driver to %s", d.DriverDirectory))
144139

145140
body, err := downloadDriver(d.getDriverURLs())
146141
if err != nil {
@@ -184,14 +179,19 @@ func (d *PlaywrightDriver) DownloadDriver() error {
184179
}
185180
}
186181

182+
d.log("Downloaded driver successfully")
183+
184+
return nil
185+
}
186+
187+
func (d *PlaywrightDriver) log(s string) {
187188
if d.options.Verbose {
188-
log.Println("Downloaded driver successfully")
189+
logger.Println(s)
189190
}
190-
return nil
191191
}
192192

193193
func (d *PlaywrightDriver) run() (*connection, error) {
194-
transport, err := newPipeTransport(d.DriverBinaryLocation)
194+
transport, err := newPipeTransport(d.DriverBinaryLocation, d.options.Stderr)
195195
if err != nil {
196196
return nil, err
197197
}
@@ -206,16 +206,16 @@ func (d *PlaywrightDriver) installBrowsers(driverPath string) error {
206206
}
207207
cmd := exec.Command(driverPath, additionalArgs...)
208208
cmd.SysProcAttr = defaultSysProcAttr
209-
cmd.Stdout = os.Stdout
210-
cmd.Stderr = os.Stderr
209+
cmd.Stdout = d.options.Stdout
210+
cmd.Stderr = d.options.Stderr
211211
return cmd.Run()
212212
}
213213

214214
func (d *PlaywrightDriver) uninstallBrowsers(driverPath string) error {
215215
cmd := exec.Command(driverPath, "uninstall")
216216
cmd.SysProcAttr = defaultSysProcAttr
217-
cmd.Stdout = os.Stdout
218-
cmd.Stderr = os.Stderr
217+
cmd.Stdout = d.options.Stdout
218+
cmd.Stderr = d.options.Stderr
219219
return cmd.Run()
220220
}
221221

@@ -224,7 +224,9 @@ type RunOptions struct {
224224
DriverDirectory string
225225
SkipInstallBrowsers bool
226226
Browsers []string
227-
Verbose bool
227+
Verbose bool // default true
228+
Stdout io.Writer
229+
Stderr io.Writer
228230
}
229231

230232
// Install does download the driver and the browsers. If not called manually
@@ -256,12 +258,21 @@ func Run(options ...*RunOptions) (*Playwright, error) {
256258
}
257259

258260
func transformRunOptions(options []*RunOptions) *RunOptions {
261+
option := &RunOptions{
262+
Verbose: true,
263+
}
259264
if len(options) == 1 {
260-
return options[0]
265+
option = options[0]
261266
}
262-
return &RunOptions{
263-
Verbose: true,
267+
if option.Stdout == nil {
268+
option.Stdout = os.Stdout
269+
}
270+
if option.Stderr == nil {
271+
option.Stderr = os.Stderr
272+
} else {
273+
logger.SetOutput(option.Stderr)
264274
}
275+
return option
265276
}
266277

267278
func getDriverName() string {

transport.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"log"
109
"os"
1110
"os/exec"
1211

@@ -46,7 +45,7 @@ func (t *pipeTransport) Poll() (*message, error) {
4645
if os.Getenv("DEBUGP") != "" {
4746
fmt.Fprint(os.Stdout, "\x1b[33mRECV>\x1b[0m\n")
4847
if err := json.NewEncoder(os.Stdout).Encode(msg); err != nil {
49-
log.Printf("could not encode json: %v", err)
48+
logger.Printf("could not encode json: %v\n", err)
5049
}
5150
}
5251
return msg, nil
@@ -74,7 +73,7 @@ func (t *pipeTransport) Send(msg map[string]interface{}) error {
7473
if os.Getenv("DEBUGP") != "" {
7574
fmt.Fprint(os.Stdout, "\x1b[32mSEND>\x1b[0m\n")
7675
if err := json.NewEncoder(os.Stdout).Encode(msg); err != nil {
77-
log.Printf("could not encode json: %v", err)
76+
logger.Printf("could not encode json: %v\n", err)
7877
}
7978
}
8079
lengthPadding := make([]byte, 4)
@@ -103,14 +102,14 @@ func (t *pipeTransport) isClosed() bool {
103102
}
104103
}
105104

106-
func newPipeTransport(driverCli string) (transport, error) {
105+
func newPipeTransport(driverCli string, stderr io.Writer) (transport, error) {
107106
t := &pipeTransport{
108107
closed: make(chan struct{}, 1),
109108
}
110109

111110
cmd := exec.Command(driverCli, "run-driver")
112111
cmd.SysProcAttr = defaultSysProcAttr
113-
cmd.Stderr = os.Stderr
112+
cmd.Stderr = stderr
114113
stdin, err := cmd.StdinPipe()
115114
if err != nil {
116115
return nil, fmt.Errorf("could not create stdin pipe: %w", err)

websocket.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package playwright
33
import (
44
"encoding/base64"
55
"errors"
6-
"log"
76
)
87

98
type webSocketImpl struct {
@@ -51,7 +50,7 @@ func (ws *webSocketImpl) onFrameSent(opcode float64, data string) {
5150
if opcode == 2 {
5251
payload, err := base64.StdEncoding.DecodeString(data)
5352
if err != nil {
54-
log.Printf("could not decode WebSocket.onFrameSent payload: %v", err)
53+
logger.Printf("could not decode WebSocket.onFrameSent payload: %v\n", err)
5554
return
5655
}
5756
ws.Emit("framesent", payload)
@@ -64,7 +63,7 @@ func (ws *webSocketImpl) onFrameReceived(opcode float64, data string) {
6463
if opcode == 2 {
6564
payload, err := base64.StdEncoding.DecodeString(data)
6665
if err != nil {
67-
log.Printf("could not decode WebSocket.onFrameReceived payload: %v", err)
66+
logger.Printf("could not decode WebSocket.onFrameReceived payload: %v\n", err)
6867
return
6968
}
7069
ws.Emit("framereceived", payload)

0 commit comments

Comments
 (0)