-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathsignal.go
52 lines (44 loc) · 1010 Bytes
/
signal.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
package global
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
var (
mainStopCh chan struct{}
mainOnce sync.Once
dumpMutex sync.Mutex
)
func dumpStack() {
dumpMutex.Lock()
defer dumpMutex.Unlock()
log.Info("开始 dump 当前 goroutine stack 信息")
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
fileName := fmt.Sprintf("%s.%d.stacks.%d.log", filepath.Base(os.Args[0]), os.Getpid(), time.Now().Unix())
fd, err := os.Create(fileName)
if err != nil {
log.Errorf("保存 stackdump 到文件时出现错误: %v", err)
log.Warnf("无法保存 stackdump. 将直接打印\n %s", buf)
return
}
defer fd.Close()
_, err = fd.Write(buf)
if err != nil {
log.Errorf("写入 stackdump 失败: %v", err)
log.Warnf("无法保存 stackdump. 将直接打印\n %s", buf)
return
}
log.Infof("stackdump 已保存至 %s", fileName)
}