go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/logutil/stack_depth.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package logutil
     9  
    10  import (
    11  	"runtime"
    12  	"strings"
    13  )
    14  
    15  // SDKStackDepth returns the stack depth of the sdk related calls.
    16  func SDKStackDepth() (depth int) {
    17  	const maxDepth = 32
    18  	var pcs [maxDepth]uintptr
    19  
    20  	// skip (1) frame because of this this function (which we don't care about)
    21  	callDepth := runtime.Callers(1, pcs[:])
    22  
    23  	var pc uintptr
    24  	var fn *runtime.Func
    25  	var name string
    26  	for ; depth < callDepth; depth++ {
    27  		pc = pcs[depth] - 1
    28  		fn = runtime.FuncForPC(pc)
    29  		name = fn.Name()
    30  		if strings.HasPrefix(name, "go.charczuk.com/sdk/apputil.") {
    31  			// we break here because apputil stuff is basically
    32  			// service code and we shouldn't treat it like sdk code.
    33  			return
    34  		}
    35  		if !strings.HasPrefix(name, "go.charczuk.com/sdk/") {
    36  			return
    37  		}
    38  	}
    39  	return
    40  }