Created
July 17, 2019 13:53
-
-
Save lggomezml/5140d7c94789e1ab11d5f574481286c6 to your computer and use it in GitHub Desktop.
Get a compact stacktrace without the callsites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var newLine = []byte("\n")[0] | |
var goPathPrefix = "\t/go/src/" | |
var goUsersPrefix = "\t/Users/" | |
func dumpStacktrace(skipLines int) []byte { | |
stack := debug.Stack() | |
init, lines := 0, 0 | |
// First pass: seek stack start skiping lines if necessary | |
if skipLines > 0 { | |
for i := 0; i < len(stack); i++ { | |
if stack[i] == newLine { | |
lines = lines + 1 | |
if lines >= skipLines { | |
init = i | |
break | |
} | |
} | |
} | |
} | |
lineBytes := 0 | |
compact := false | |
compactInit, compactStackSize := 0, 0 | |
// Second pass: filter callsite lines while compacting the stack in the process | |
if init < len(stack) { | |
for i := init; i < len(stack); i++ { | |
if stack[i] == newLine { | |
lineBytes = 0 | |
if compact { | |
copied := copy(stack[compactStackSize:], stack[compactInit:i+1]) | |
compactStackSize = compactStackSize + copied | |
compact = false | |
} | |
} else { | |
lineBytes = lineBytes + 1 | |
} | |
if lineBytes == 1 { | |
if (string(stack[i:i+len(goPathPrefix)]) == goPathPrefix) || (string(stack[i:i+len(goUsersPrefix)]) == goUsersPrefix) { | |
compact = true | |
compactInit = i | |
} | |
} | |
} | |
} | |
return stack[0:compactStackSize] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment