@@ -4,11 +4,11 @@ import (
44 "context"
55 "fmt"
66 "io/ioutil"
7+ "log"
78 "net"
89 "os"
910
1011 "github.com/cli/cli/v2/internal/codespaces"
11- "github.com/cli/cli/v2/pkg/cmd/codespace/output"
1212 "github.com/cli/cli/v2/pkg/liveshare"
1313 "github.com/spf13/cobra"
1414)
@@ -18,6 +18,7 @@ type sshOptions struct {
1818 profile string
1919 serverPort int
2020 debug bool
21+ debugFile string
2122}
2223
2324func newSSHCmd (app * App ) * cobra.Command {
@@ -35,6 +36,7 @@ func newSSHCmd(app *App) *cobra.Command {
3536 sshCmd .Flags ().IntVarP (& opts .serverPort , "server-port" , "" , 0 , "SSH server port number (0 => pick unused)" )
3637 sshCmd .Flags ().StringVarP (& opts .codespace , "codespace" , "c" , "" , "Name of the codespace" )
3738 sshCmd .Flags ().BoolVarP (& opts .debug , "debug" , "d" , false , "Log debug data to a file" )
39+ sshCmd .Flags ().StringVarP (& opts .debugFile , "debug-file" , "" , "" , "Path of the file log to" )
3840
3941 return sshCmd
4042}
@@ -62,7 +64,7 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
6264
6365 var debugLogger * fileLogger
6466 if opts .debug {
65- debugLogger , err = newFileLogger ("gh-cs-ssh" )
67+ debugLogger , err = newFileLogger (opts . debugFile )
6668 if err != nil {
6769 return fmt .Errorf ("error creating debug logger: %w" , err )
6870 }
@@ -125,26 +127,34 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
125127 }
126128}
127129
128- // fileLogger is a wrapper around an output .Logger configured to write
130+ // fileLogger is a wrapper around an log .Logger configured to write
129131// to a file. It exports two additional methods to get the log file name
130132// and close the file handle when the operation is finished.
131133type fileLogger struct {
132- // TODO(josebalius): should we use https://pkg.go.dev/log#New instead?
133- * output.Logger
134+ * log.Logger
134135
135136 f * os.File
136137}
137138
138139// newFileLogger creates a new fileLogger. It returns an error if the file
139- // cannot be created. The file is created in the operating system tmp directory
140- // under the name parameter.
141- func newFileLogger (name string ) (* fileLogger , error ) {
142- f , err := ioutil .TempFile ("" , name )
143- if err != nil {
144- return nil , err
140+ // cannot be created. The file is created on the specified path, if the path
141+ // is empty it is created in the temporary directory.
142+ func newFileLogger (file string ) (fl * fileLogger , err error ) {
143+ var f * os.File
144+ if file == "" {
145+ f , err = ioutil .TempFile ("" , "" )
146+ if err != nil {
147+ return nil , fmt .Errorf ("failed to create tmp file: %w" , err )
148+ }
149+ } else {
150+ f , err = os .Create (file )
151+ if err != nil {
152+ return nil , err
153+ }
145154 }
155+
146156 return & fileLogger {
147- Logger : output . NewLogger (f , f , false ),
157+ Logger : log . New (f , "" , log . LstdFlags ),
148158 f : f ,
149159 }, nil
150160}
0 commit comments