Use panic for fail-fast

This change switches away from using glog.Fatalf, and instead uses panic,
should a fail-fast arise (due to a call to util.Fail or a failed assertion
by way of util.Assert).  This leads to a better debugging experience no
matter what flags have been passed to glog.  For example, glog.Fatal* seems
to suppress printing stack traces when --logtostderr is supplied.
This commit is contained in:
joeduffy 2016-12-01 11:43:41 -08:00
parent 334615c6b1
commit 3a7fa9a983
2 changed files with 14 additions and 8 deletions

View file

@ -4,8 +4,6 @@ package util
import (
"fmt"
"github.com/golang/glog"
)
const assertMsg = "An assertion has failed"
@ -13,20 +11,20 @@ const assertMsg = "An assertion has failed"
// Assert checks a condition and Fails if it is false.
func Assert(cond bool) {
if !cond {
glog.Fatal(assertMsg)
failfast(assertMsg)
}
}
// AssertM checks a condition and FailsMs if it is false, logging the given message.
func AssertM(cond bool, msg string) {
if !cond {
glog.Fatalf("%v: %v", assertMsg, msg)
failfast(fmt.Sprintf("%v: %v", assertMsg, msg))
}
}
// AssertMF checks a condition and FailsMFs if it is false, formatting and logging the given message.
func AssertMF(cond bool, msg string, args ...interface{}) {
if !cond {
glog.Fatalf("%v: %v", assertMsg, fmt.Sprintf(msg, args...))
failfast(fmt.Sprintf("%v: %v", assertMsg, fmt.Sprintf(msg, args...)))
}
}

View file

@ -12,15 +12,23 @@ const failMsg = "A failure has occurred"
// Fail unconditionally abandons the process.
func Fail() {
glog.Fatal(failMsg)
failfast(failMsg)
}
// FailM unconditionally abandons the process, logging the given message.
func FailM(msg string) {
glog.Fatalf("%v: %v", failMsg, msg)
failfast(fmt.Sprintf("%v: %v", failMsg, msg))
}
// FailMF unconditionally abandons the process, formatting and logging the given message.
func FailMF(msg string, args ...interface{}) {
glog.Fatalf("%v: %v", failMsg, fmt.Sprintf(msg, args...))
failfast(fmt.Sprintf("%v: %v", failMsg, fmt.Sprintf(msg, args...)))
}
// failfast logs and panics the process in a way that is friendly to debugging.
func failfast(msg string) {
// We first log an error using glog, and then we use panic, not glog, to ensure good debugging no matter what
// glog flags have been passed (e.g., logtostderr).
glog.Error(msg)
panic(msg)
}