From 85346a39118e4327e405a18b545b118b5ff4febf Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 7 Dec 2018 11:33:44 -0500 Subject: [PATCH] cmd/golsp: enable logging to a default location Add a default "auto" value to the "--logfile" flag that allows logs to be written to a default location. Change-Id: I1952ad2622b824795906c6b8183b58f88c35fb62 Reviewed-on: https://go-review.googlesource.com/c/153197 Reviewed-by: Ian Cottrell --- cmd/golsp/main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/golsp/main.go b/cmd/golsp/main.go index ce619ae18c..25e6726e93 100644 --- a/cmd/golsp/main.go +++ b/cmd/golsp/main.go @@ -16,6 +16,7 @@ import ( "io" "log" "os" + "path/filepath" "runtime" "runtime/pprof" "runtime/trace" @@ -29,10 +30,10 @@ var ( cpuprofile = flag.String("cpuprofile", "", "write CPU profile to this file") memprofile = flag.String("memprofile", "", "write memory profile to this file") traceFlag = flag.String("trace", "", "write trace log to this file") + logfile = flag.String("logfile", "", "filename to log to. if value is \"auto\", then logging to a default output file is enabled") // Flags for compatitibility with VSCode. - logfile = flag.String("logfile", "", "filename to log to") - mode = flag.String("mode", "", "no effect") + mode = flag.String("mode", "", "no effect") ) func main() { @@ -90,7 +91,11 @@ func main() { out := os.Stderr if *logfile != "" { - f, err := os.Create(*logfile) + filename := *logfile + if filename == "auto" { + filename = filepath.Join(os.TempDir(), fmt.Sprintf("golsp-%d.log", os.Getpid())) + } + f, err := os.Create(filename) if err != nil { log.Fatalf("Unable to create log file: %v", err) }