Similar to debug.Stack() except it skips the top 3 frames which include the logger itself to keep the logs focused on the actual relevant stack traces.
()
| 88 | // Similar to debug.Stack() except it skips the top 3 frames which include the logger itself |
| 89 | // to keep the logs focused on the actual relevant stack traces. |
| 90 | func stackTrace() string { |
| 91 | const depth = 32 |
| 92 | var pcs [depth]uintptr |
| 93 | n := runtime.Callers(3, pcs[:]) // skip runtime.Callers + stackTrace |
| 94 | if len(pcs) < n { |
| 95 | return "" |
| 96 | } |
| 97 | frames := runtime.CallersFrames(pcs[:n]) |
| 98 | trace := "" |
| 99 | for { |
| 100 | frame, more := frames.Next() |
| 101 | // Note: Additionally set -trimpath when building to not leak anything from the file path |
| 102 | trace += fmt.Sprintf("%s\n\t%s:%d\n", frame.Function, frame.File, frame.Line) |
| 103 | if !more { |
| 104 | break |
| 105 | } |
| 106 | } |
| 107 | return trace |
| 108 | } |
| 109 | |
| 110 | type loggerKey struct{} |
| 111 |