1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00
go-cqhttp/global/signal.go
povsister e2cafbd7d6
添加无需额外配置直接打印stack的功能
*unix: kill -USR1 <gocq_pid>
windows: echo dumpstack >\\.\pipe\go-cqhttp-<pid>

stackdump将直接以<exec_name>.<pid>.stacks.<timestamp>.log
的文件名保存在当前工作目录下
2021-06-26 14:33:37 +08:00

53 lines
1010 B
Go

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)
}