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

Option to redirect devserver stdout/stderr to a file #1452

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions testsuite/devserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type DevServerOptions struct {
LogLevel string
// Additional arguments to the dev server.
ExtraArgs []string
// Where to redirect stdout and stderr, if empty they will be redirected to the current process.
StdoutLogFile string
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
StdoutLogFile string
Stdout io.Writer
Stderr io.Writer

This is a more flexible approach, the caller can provide a file if they want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, will update the PR soon-ish. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}

// Temporal CLI based DevServer
Expand Down Expand Up @@ -110,7 +112,15 @@ func StartDevServer(ctx context.Context, options DevServerOptions) (*DevServer,

args := prepareCommand(&options, host, port, clientOptions.Namespace)

stdout, stderr, err := stdStreams(options.StdoutLogFile)
if err != nil {
return nil, err
}

cmd := newCmd(exePath, args...)
cmd.Stdout = stdout
cmd.Stderr = stderr

clientOptions.Logger.Info("Starting DevServer", "ExePath", exePath, "Args", args)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed starting: %w", err)
Expand Down Expand Up @@ -375,3 +385,16 @@ func (s *DevServer) Client() client.Client {
func (s *DevServer) FrontendHostPort() string {
return s.frontendHostPort
}

func stdStreams(stdoutLogFile string) (io.Writer, io.Writer, error) {
if stdoutLogFile == "" {
return os.Stdout, os.Stderr, nil
}

file, err := os.OpenFile(stdoutLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return nil, nil, fmt.Errorf("open %q: %w", stdoutLogFile, err)
}

return file, file, nil
}
1 change: 0 additions & 1 deletion testsuite/process_nonwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
// newCmd creates a new command with the given executable path and arguments.
func newCmd(exePath string, args ...string) *exec.Cmd {
cmd := exec.Command(exePath, args...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion testsuite/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newCmd(exePath string, args ...string) *exec.Cmd {
// isolate the process and signals sent to it from the current console
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr

return cmd
}

Expand Down