Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit a2ad054

Browse files
committed
Add gplog.FatalNoPanicLogStacktrace()
- This function does not call panic() but still writes the stack trace into the log file, so that the user has it for debugging purposes even if he is configured not to print it into the console. This is the variation of FatalWithNoPanic(), which also does not call panic(), but swallows the stack trace.
1 parent 1be9f40 commit a2ad054

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

gplog/gplog.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const (
8181
* cannot be reached. This function will exit the program after printing
8282
* the error message.
8383
* - FatalWithoutPanic: Same as Fatal, but will not trigger panic. Just exit(1).
84+
* - FatalWithoutPanicLogStacktrace(): Same as FatalWithoutPanic, but
85+
* writes the stacktrace into the log file for debug purpose.
8486
*/
8587
type LogPrefixFunc func(string) string
8688
type LogFileNameFunc func(string, string) string
@@ -324,6 +326,37 @@ func FatalWithoutPanic(s string, v ...interface{}) {
324326
exitFunc()
325327
}
326328

329+
func FatalWithoutPanicLogStacktrace(err error, output ...string){
330+
if err != nil {
331+
var outputStr string
332+
if len(output) == 0 {
333+
outputStr = ""
334+
} else {
335+
outputStr = output[0]
336+
}
337+
logMutex.Lock()
338+
defer logMutex.Unlock()
339+
message := GetLogPrefix("CRITICAL")
340+
errorCode = 2
341+
stackTraceStr := ""
342+
if err != nil {
343+
message += fmt.Sprintf("%v", err)
344+
stackTraceStr = formatStackTrace(errors.WithStack(err))
345+
if outputStr != "" {
346+
message += ": "
347+
}
348+
}
349+
message += strings.TrimSpace(fmt.Sprintf(outputStr))
350+
_ = logger.logFile.Output(1, message+stackTraceStr)
351+
if logger.shellVerbosity >= LOGVERBOSE {
352+
_ = logger.logStderr.Output(1, message + stackTraceStr)
353+
} else {
354+
_ = logger.logStderr.Output(1,message)
355+
}
356+
exitFunc()
357+
}
358+
}
359+
327360
type stackTracer interface {
328361
StackTrace() errors.StackTrace
329362
}

gplog/gplog_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ var _ = Describe("logger/log tests", func() {
509509
testhelper.ExpectRegexp(logfile, fatalExpected+expectedMessage)
510510
})
511511
})
512+
Context("FatalWithoutPanicLogStacktrace", func() {
513+
It("prints to the log file with stack trace, then exit(1)", func() {
514+
gplog.SetExitFunc(func() {})
515+
expectedMessage := "logfile error something wrong"
516+
err := errors.WithStack(errors.New(expectedMessage))
517+
expectedWithStack := `.*\[CRITICAL\]:-logfile error something wrong
518+
.*gplog_test\.glob.*
519+
.*gplog_test\.go.*
520+
.*
521+
.*internal\/suite.go.*
522+
runtime\.goexit.*`
523+
gplog.FatalWithoutPanicLogStacktrace(err, "")
524+
testhelper.NotExpectRegexp(stdout, fatalExpected+expectedMessage)
525+
testhelper.ExpectRegexp(stderr, fatalExpected+expectedMessage)
526+
Expect(string(logfile.Contents())).To(MatchRegexp(expectedWithStack))
527+
})
528+
})
512529
})
513530
})
514531
})

0 commit comments

Comments
 (0)