mirror of https://github.com/sirupsen/logrus.git
Go fmt alt exit addition
This commit is contained in:
parent
a7755c5c03
commit
ff52e76f67
29
alt_exit.go
29
alt_exit.go
|
@ -23,38 +23,37 @@ package logrus
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var handlers = []func(){}
|
var handlers = []func(){}
|
||||||
|
|
||||||
func runHandler(handler func()) {
|
func runHandler(handler func()) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Error: Logrus exit handler error:", err)
|
fmt.Fprintln(os.Stderr, "Error: Logrus exit handler error:", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
handler()
|
handler()
|
||||||
}
|
}
|
||||||
|
|
||||||
func runHandlers() {
|
func runHandlers() {
|
||||||
for _, handler := range handlers {
|
for _, handler := range handlers {
|
||||||
runHandler(handler)
|
runHandler(handler)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
|
// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
|
||||||
func Exit(code int) {
|
func Exit(code int) {
|
||||||
runHandlers()
|
runHandlers()
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterExitHandler adds a Logrus atexit handler, call logrus.Exit to invoke
|
// RegisterExitHandler adds a Logrus atexit handler, call logrus.Exit to invoke
|
||||||
// all handlers. The handlers will also be invoked when any Fatal log entry is
|
// all handlers. The handlers will also be invoked when any Fatal log entry is
|
||||||
// made.
|
// made.
|
||||||
func RegisterExitHandler(handler func()) {
|
func RegisterExitHandler(handler func()) {
|
||||||
handlers = append(handlers, handler)
|
handlers = append(handlers, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,41 +1,41 @@
|
||||||
package logrus
|
package logrus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
"io/ioutil"
|
||||||
"testing"
|
"os/exec"
|
||||||
"io/ioutil"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRegister(t *testing.T) {
|
func TestRegister(t *testing.T) {
|
||||||
current := len(handlers)
|
current := len(handlers)
|
||||||
RegisterExitHandler(func() {})
|
RegisterExitHandler(func() {})
|
||||||
if len(handlers) != current+1 {
|
if len(handlers) != current+1 {
|
||||||
t.Fatalf("can't add handler")
|
t.Fatalf("can't add handler")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandler(t *testing.T) {
|
func TestHandler(t *testing.T) {
|
||||||
gofile := "/tmp/testprog.go"
|
gofile := "/tmp/testprog.go"
|
||||||
if err := ioutil.WriteFile(gofile, testprog, 0666); err != nil {
|
if err := ioutil.WriteFile(gofile, testprog, 0666); err != nil {
|
||||||
t.Fatalf("can't create go file")
|
t.Fatalf("can't create go file")
|
||||||
}
|
}
|
||||||
|
|
||||||
outfile := "/tmp/testprog.out"
|
outfile := "/tmp/testprog.out"
|
||||||
arg := time.Now().UTC().String()
|
arg := time.Now().UTC().String()
|
||||||
err := exec.Command("go", "run", gofile, outfile, arg).Run()
|
err := exec.Command("go", "run", gofile, outfile, arg).Run()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("completed normally, should have failed")
|
t.Fatalf("completed normally, should have failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(outfile)
|
data, err := ioutil.ReadFile(outfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't read output file %s", outfile)
|
t.Fatalf("can't read output file %s", outfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(data) != arg {
|
if string(data) != arg {
|
||||||
t.Fatalf("bad data")
|
t.Fatalf("bad data")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var testprog = []byte(`
|
var testprog = []byte(`
|
||||||
|
@ -72,4 +72,3 @@ func main() {
|
||||||
logrus.Fatal("Bye bye")
|
logrus.Fatal("Bye bye")
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue