Skip to content

Commit 028706f

Browse files
committed
Log the command line and stdout/err from Chrome
... in the hope that this sheds osme light on why it's not listening on the websocket
1 parent bd38a60 commit 028706f

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

internal/api/js/chrome/chrome.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (w coloredLogWriter) Write(p []byte) (n int, err error) {
111111
func RunHeadless(logPrefix string, onConsoleLog func(s string), requiresPersistence bool, listenPort int) (*Browser, error) {
112112
ansiRedForeground := "\x1b[31m"
113113
ansiYellowForeground := "\x1b[33m"
114-
ansiResetForeground := "\x1b[39m"
114+
ansiBlueForeground := "\x1b[34m"
115115

116116
// colorifyError returns a log format function which prints its input with a given prefix and colour.
117117
colorifyError := func(colour string, prefix string) func(format string, args ...any) {
@@ -132,6 +132,23 @@ func RunHeadless(logPrefix string, onConsoleLog func(s string), requiresPersiste
132132
// increase the WS timeout from 20s (default) to 30s as we see timeouts with 20s in CI
133133
opts = append(opts, chromedp.WSURLReadTimeout(30*time.Second))
134134

135+
// Capture stdout/stderr from Chrome, and log it.
136+
opts = append(opts, chromedp.CombinedOutput(coloredLogWriter{colour: ansiBlueForeground, logPrefix: logPrefix + " chrome:", output: os.Stdout}))
137+
138+
// Hook into chromedp to log the command that is about to be executed. The easiest way to do that seems to be to
139+
// set a ModifyCmdFunc.
140+
opts = append(opts, chromedp.ModifyCmdFunc(func(cmd *exec.Cmd) {
141+
writer := coloredLogWriter{colour: ansiBlueForeground, logPrefix: logPrefix, output: os.Stdout}
142+
fmt.Fprintf(writer, "Executing: %v\n", cmd.Args)
143+
144+
// Replicate the behaviour of the default ModifyCmdFunc: tell the kernel to send the child a SIGKILL when the
145+
// parent thread dies.
146+
if cmd.SysProcAttr == nil {
147+
cmd.SysProcAttr = new(syscall.SysProcAttr)
148+
}
149+
cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL
150+
}))
151+
135152
allocCtx, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...)
136153
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithBrowserOption(
137154
chromedp.WithBrowserLogf(colorifyError(ansiYellowForeground, "LOG")),

0 commit comments

Comments
 (0)