internal/lsp/debug: fix early closure of logfile

As of https://golang.org/cl/218457, logs are not being captured because
the logfile is prematurely closed due to the scope of the deferred
closure changing.

Change-Id: I1754e5555025c7b2a5da58f621184d6740fd03cb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/219080
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Rob Findley 2020-02-11 12:41:14 -05:00 committed by Robert Findley
parent 7a72f3f8e9
commit f41547ceaf
2 changed files with 13 additions and 5 deletions

View File

@ -54,9 +54,11 @@ func (s *Serve) Run(ctx context.Context, args ...string) error {
return tool.CommandLineErrorf("server does not take arguments, got %v", args)
}
if err := s.app.debug.SetLogFile(s.Logfile); err != nil {
err, closeLog := s.app.debug.SetLogFile(s.Logfile)
if err != nil {
return err
}
defer closeLog()
s.app.debug.ServerAddress = s.Address
s.app.debug.DebugAddress = s.Debug
s.app.debug.Serve(ctx)

View File

@ -258,21 +258,27 @@ func (i *Instance) Prepare(ctx context.Context) {
export.AddExporters(i.ocagent, i.prometheus, i.rpcs, i.traces)
}
func (i *Instance) SetLogFile(logfile string) error {
func (i *Instance) SetLogFile(logfile string) (error, func()) {
// TODO: probably a better solution for deferring closure to the caller would
// be for the debug instance to itself be closed, but this fixes the
// immediate bug of logs not being captured.
closeLog := func() {}
if logfile != "" {
if logfile == "auto" {
logfile = filepath.Join(os.TempDir(), fmt.Sprintf("gopls-%d.log", os.Getpid()))
}
f, err := os.Create(logfile)
if err != nil {
return fmt.Errorf("Unable to create log file: %v", err)
return fmt.Errorf("Unable to create log file: %v", err), nil
}
closeLog = func() {
defer f.Close()
}
defer f.Close()
stdlog.SetOutput(io.MultiWriter(os.Stderr, f))
i.LogWriter = f
}
i.Logfile = logfile
return nil
return nil, closeLog
}
// Serve starts and runs a debug server in the background.