github.com/go-spring/spring-base@v1.1.3/log/internal/caller.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package internal
    18  
    19  import (
    20  	"runtime"
    21  	"sync"
    22  )
    23  
    24  // frameMap 用于缓存调用点,基准测试表明使用缓存大约有 50% 的性能提升。
    25  var frameMap sync.Map
    26  
    27  // Caller 获取调用点的文件及行号信息,fast 为 true 时使用缓存进行加速。
    28  func Caller(skip int, fast bool) (file string, line int, loaded bool) {
    29  
    30  	if !fast {
    31  		_, file, line, loaded = runtime.Caller(skip + 1)
    32  		return
    33  	}
    34  
    35  	rpc := make([]uintptr, 1)
    36  	n := runtime.Callers(skip+2, rpc[:])
    37  	if n < 1 {
    38  		return
    39  	}
    40  	pc := rpc[0]
    41  	if v, ok := frameMap.Load(pc); ok {
    42  		e := v.(*runtime.Frame)
    43  		return e.File, e.Line, true
    44  	}
    45  	frame, _ := runtime.CallersFrames(rpc).Next()
    46  	frameMap.Store(pc, &frame)
    47  	return frame.File, frame.Line, false
    48  }