-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexit.go
55 lines (43 loc) · 877 Bytes
/
exit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Code from 'http://code.google.com/p/go/source/browse/src/cmd/go/main.go'
package goutil
import (
"log"
"os"
"sync"
)
var exitStatus = 0
var exitMu sync.Mutex
func SetExitStatus(n int) {
exitMu.Lock()
if exitStatus < n {
exitStatus = n
}
exitMu.Unlock()
}
var atexitFuncs []func()
func AtExit(f func()) {
atexitFuncs = append(atexitFuncs, f)
}
func Exit() {
for _, f := range atexitFuncs {
f()
}
os.Exit(exitStatus)
}
func Fatalf(format string, args ...interface{}) {
log.Printf(format, args...)
Exit()
}
func Errorf(format string, args ...interface{}) {
log.Printf(format, args...)
SetExitStatus(1)
}
//var logf = log.Printf
func ExitIfErrors() {
if exitStatus != 0 {
Exit()
}
}