github.com/suiyunonghen/DxCommonLib@v0.5.3/system/trace_frame.go (about) 1 package system 2 3 import ( 4 "bytes" 5 "runtime" 6 "strconv" 7 "strings" 8 ) 9 10 func TraceFrame2Buffer(frameC int, buffer *bytes.Buffer) { 11 if frameC < 1 { 12 return 13 } 14 for i := 1; i < frameC+1; i++ { 15 if pc, file, line, ok := runtime.Caller(i); !ok { 16 break 17 } else { 18 if strings.HasPrefix(file,"runtime/") || strings.Index(file,"!dx!common!lib@") > 0{ 19 break 20 } 21 if i > 1 { 22 buffer.Write([]byte{'\r', '\n'}) 23 } 24 idx := strings.LastIndexByte(file, '/') 25 if idx == -1 { 26 buffer.WriteString(file) 27 } else { 28 idx = strings.LastIndexByte(file[:idx], '/') 29 if idx == -1 { 30 buffer.WriteString(file) 31 } else { 32 buffer.WriteString(file[idx+1:]) 33 } 34 } 35 buffer.WriteByte(':') 36 buffer.WriteString(strconv.Itoa(line)) 37 funcName := runtime.FuncForPC(pc).Name() 38 idx = strings.IndexByte(funcName, '.') 39 if idx > 0 { 40 funcName = funcName[idx+1:] 41 } 42 buffer.WriteByte('.') 43 buffer.WriteString(funcName) 44 if strings.Index(file,"/main.go")>0{ 45 break 46 } 47 } 48 } 49 } 50 51 func TraceFrame(frameC int, bt []byte) []byte { 52 if frameC < 1 { 53 return nil 54 } 55 for i := 1; i < frameC+1; i++ { 56 if pc, file, line, ok := runtime.Caller(i); !ok { 57 break 58 } else { 59 if strings.HasPrefix(file,"runtime/") || strings.Index(file,"!dx!common!lib@") > 0{ 60 break 61 } 62 if i > 1 { 63 bt = append(bt, '\r', '\n') 64 } 65 idx := strings.LastIndexByte(file, '/') 66 if idx == -1 { 67 bt = append(bt, file...) 68 } else { 69 idx = strings.LastIndexByte(file[:idx], '/') 70 if idx == -1 { 71 bt = append(bt, file...) 72 } else { 73 bt = append(bt, file[idx+1:]...) 74 } 75 } 76 bt = append(bt, ':') 77 bt = append(bt, strconv.Itoa(line)...) 78 funcName := runtime.FuncForPC(pc).Name() 79 idx = strings.IndexByte(funcName, '.') 80 if idx > 0 { 81 funcName = funcName[idx+1:] 82 } 83 bt = append(bt, '.') 84 bt = append(bt, funcName...) 85 if strings.Index(file,"/main.go")>0{ 86 break 87 } 88 } 89 } 90 return bt 91 }