captureStackTrace returns a formatted stack trace string starting after skipping the specified number of frames. The skip parameter should be adjusted so that frames you want to hide (like the ones generated by the tracing functions themselves) are excluded.
(skip int)
| 531 | // adjusted so that frames you want to hide (like the ones generated by the |
| 532 | // tracing functions themselves) are excluded. |
| 533 | func captureStackTraceSkipFrames(skip int) string { |
| 534 | // Allocate room for up to 100 callers; adjust as needed. |
| 535 | pcs := make([]uintptr, 100) |
| 536 | // Skip the specified number of frames. |
| 537 | n := runtime.Callers(skip, pcs) |
| 538 | frames := runtime.CallersFrames(pcs[:n]) |
| 539 | |
| 540 | var stackTraceSB strings.Builder |
| 541 | for { |
| 542 | frame, more := frames.Next() |
| 543 | fmt.Fprintf(&stackTraceSB, "%s\n\t%s:%d\n", frame.Function, frame.File, frame.Line) |
| 544 | if !more { |
| 545 | break |
| 546 | } |
| 547 | } |
| 548 | return stackTraceSB.String() |
| 549 | } |