StackWithFilters returns a formatted stack trace of the goroutine that calls it. It calls runtime.Stack with a large enough buffer to capture the entire trace. The parameter `filters` is a slice of strings, which are used to filter the path of the caller. TODO Improve the performance using debug.S
(filters []string, skip ...int)
| 39 | // |
| 40 | // TODO Improve the performance using debug.Stack. |
| 41 | func StackWithFilters(filters []string, skip ...int) string { |
| 42 | number := 0 |
| 43 | if len(skip) > 0 { |
| 44 | number = skip[0] |
| 45 | } |
| 46 | var ( |
| 47 | name string |
| 48 | space = " " |
| 49 | index = 1 |
| 50 | buffer = bytes.NewBuffer(nil) |
| 51 | ok = true |
| 52 | pc, file, line, start = callerFromIndex(filters) |
| 53 | ) |
| 54 | for i := start + number; i < maxCallerDepth; i++ { |
| 55 | if i != start { |
| 56 | pc, file, line, ok = runtime.Caller(i) |
| 57 | } |
| 58 | if ok { |
| 59 | if filterFileByFilters(file, filters) { |
| 60 | continue |
| 61 | } |
| 62 | if fn := runtime.FuncForPC(pc); fn == nil { |
| 63 | name = "unknown" |
| 64 | } else { |
| 65 | name = fn.Name() |
| 66 | } |
| 67 | if index > 9 { |
| 68 | space = " " |
| 69 | } |
| 70 | fmt.Fprintf(buffer, "%d.%s%s\n %s:%d\n", index, space, name, file, line) |
| 71 | index++ |
| 72 | } else { |
| 73 | break |
| 74 | } |
| 75 | } |
| 76 | return buffer.String() |
| 77 | } |
no test coverage detected
searching dependent graphs…