Sha256: 408b00a10b3577e4c9932ea24fdd42e94de35d41fe67431bbbb675e0f5cf59af
Contents?: true
Size: 1.46 KB
Versions: 16
Compression:
Stored size: 1.46 KB
Contents
//go:build go1.18 // +build go1.18 // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. package diag import ( "fmt" "runtime" "strings" ) // Caller returns the file and line number of a frame on the caller's stack. // If the funtion fails an empty string is returned. // skipFrames - the number of frames to skip when determining the caller. // Passing a value of 0 will return the immediate caller of this function. func Caller(skipFrames int) string { if pc, file, line, ok := runtime.Caller(skipFrames + 1); ok { // the skipFrames + 1 is to skip ourselves frame := runtime.FuncForPC(pc) return fmt.Sprintf("%s()\n\t%s:%d", frame.Name(), file, line) } return "" } // StackTrace returns a formatted stack trace string. // If the funtion fails an empty string is returned. // skipFrames - the number of stack frames to skip before composing the trace string. // totalFrames - the maximum number of stack frames to include in the trace string. func StackTrace(skipFrames, totalFrames int) string { pcCallers := make([]uintptr, totalFrames) if frames := runtime.Callers(skipFrames, pcCallers); frames == 0 { return "" } frames := runtime.CallersFrames(pcCallers) sb := strings.Builder{} for { frame, more := frames.Next() sb.WriteString(frame.Function) sb.WriteString("()\n\t") sb.WriteString(frame.File) sb.WriteRune(':') sb.WriteString(fmt.Sprintf("%d\n", frame.Line)) if !more { break } } return sb.String() }
Version data entries
16 entries across 16 versions & 1 rubygems