write2File write byte array to file logname: prefix of the file's name, which is like logname.20060102-150405, and the file will locates in the same diretory as the bin file which calls it buf: call stack info
(logname string, desc string, buf []byte)
| 14 | // will locates in the same diretory as the bin file which calls it |
| 15 | // buf: call stack info |
| 16 | func write2File(logname string, desc string, buf []byte) error { |
| 17 | path, _ := filepath.Abs(filepath.Dir(os.Args[0])) |
| 18 | os.Chdir(path) |
| 19 | logpath := fmt.Sprintf("%s.%s", logname, time.Now().Format("20060102-150405")) |
| 20 | |
| 21 | var err error |
| 22 | var file *os.File |
| 23 | if file, err = os.OpenFile(logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644); err == nil { |
| 24 | defer file.Close() |
| 25 | if desc != "" { |
| 26 | file.WriteString(desc + "\n\n") |
| 27 | } |
| 28 | file.WriteString(fmt.Sprintf("current running goroutine num: %d\n\n", runtime.NumGoroutine())) |
| 29 | file.WriteString(string(buf)) |
| 30 | fmt.Println("successfully dump stack info into file") |
| 31 | } |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | // DumpStack used to dump stack info to file |
| 36 | // all: true means dumping all running goroutine stack, else only dumping the one that calls the func |
no test coverage detected