Documentation ¶
Overview ¶
Exit provides a way to exit from a program that runs deferred functions, logs a message to stderr, and then exits with an operating system exit code.
The following example program is in source subdirectory "example".
package main import ( "fmt" "github.com/soniakeys/exit" ) func main() { defer exit.Handler() // do this once at the beginning of the program f() fmt.Println("f returned") } func f() { defer fmt.Println("f cleanup") fmt.Println("f attempt") if !false { exit.Log("Fatal error in f") // use exit.Log anywhere } fmt.Println("f completed") }
This prints "f attempt" and "f cleanup" to stdout, logs "Fatal error in f" to stderr, and exits with a program exit code of 1.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Default = Exit{ExitCode: 1}
Default holds Exit setting for the Log and Logf functions.
Functions ¶
func Code ¶
func Code(code int)
Code runs any deferred functions, then exits the program with the given exit code.
func Handler ¶
func Handler()
Handler is the fatal error handler. Defer at the start of main() before calling any of Code, Log, Logf, Exit.Log, or Exit.Logf.
When Handler is deferred first, Go ensures that all other deferred functions are run first. Handler then handles paincs originating from Code, Log, Logf, Exit.Log, or Exit.Logf by logging a message and then exiting with an exit code.
There are a couple of frills. Empty messages are not logged; the program simply exits with the specified code. With non-empty messages, if a logger is not specified and stderr seems to be directed to a terminal, Handler sets logging flags 0 to suppress the usual date and time stamp.
Types ¶
type Exit ¶
Exit specifies an exit code and logger for logging a final message.
If Logger is nil, the log package default logger is used.