From 1d9d82973b56f792d8427908b8d979150311de11 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 21 Oct 2020 09:45:00 -0400 Subject: [PATCH] move panicwrap handler to logging package --- panic.go => internal/logging/panic.go | 17 +++++++---------- main.go | 4 ++-- 2 files changed, 9 insertions(+), 12 deletions(-) rename panic.go => internal/logging/panic.go (80%) diff --git a/panic.go b/internal/logging/panic.go similarity index 80% rename from panic.go rename to internal/logging/panic.go index 6690a93e9..a7205ce7d 100644 --- a/panic.go +++ b/internal/logging/panic.go @@ -1,8 +1,9 @@ -package main +package logging import ( "fmt" "io" + "io/ioutil" "os" "strings" @@ -15,7 +16,7 @@ const panicOutput = ` !!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!! Terraform crashed! This is always indicative of a bug within Terraform. -A crash log has been placed at "crash.log" relative to your current +A crash log has been placed at %[1]q relative to your current working directory. It would be immensely helpful if you could please report the crash with Terraform[1] so that we can fix this. @@ -23,7 +24,7 @@ When reporting bugs, please include your terraform version. That information is available on the first line of crash.log. You can also get it by running 'terraform --version' on the command line. -SECURITY WARNING: the "crash.log" file that was created may contain +SECURITY WARNING: the %[1]q file that was created may contain sensitive information that must be redacted before it is safe to share on the issue tracker. @@ -36,14 +37,10 @@ on the issue tracker. // within Terraform. It is guaranteed to run after the resulting process has // exited so we can take the log file, add in the panic, and store it // somewhere locally. -func panicHandler(logF *os.File) panicwrap.HandlerFunc { +func PanicHandler(logF *os.File) panicwrap.HandlerFunc { return func(m string) { - // Right away just output this thing on stderr so that it gets - // shown in case anything below fails. - fmt.Fprintf(os.Stderr, fmt.Sprintf("%s\n", m)) - // Create the crash log file where we'll write the logs - f, err := os.Create("crash.log") + f, err := ioutil.TempFile(".", "crash.*.log") if err != nil { fmt.Fprintf(os.Stderr, "Failed to create crash log file: %s", err) return @@ -66,6 +63,6 @@ func panicHandler(logF *os.File) panicwrap.HandlerFunc { // Tell the user a crash occurred in some helpful way that // they'll hopefully notice. fmt.Printf("\n\n") - fmt.Println(strings.TrimSpace(panicOutput)) + fmt.Printf(strings.TrimSpace(panicOutput), f.Name()) } } diff --git a/main.go b/main.go index e87a90231..33d39e323 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/terraform/command/cliconfig" "github.com/hashicorp/terraform/command/format" "github.com/hashicorp/terraform/httpclient" + "github.com/hashicorp/terraform/internal/logging" "github.com/hashicorp/terraform/version" "github.com/mattn/go-colorable" "github.com/mattn/go-shellwords" @@ -65,8 +66,7 @@ func realMain() int { go copyOutput(outR, doneCh) // Create the configuration for panicwrap and wrap our executable - wrapConfig.Handler = panicHandler(logTempFile) - wrapConfig.Writer = os.Stderr + wrapConfig.Handler = logging.PanicHandler(logTempFile) wrapConfig.Stdout = outW wrapConfig.IgnoreSignals = ignoreSignals wrapConfig.ForwardSignals = forwardSignals