本文整理汇总了Golang中runtime.Callers函数的典型用法代码示例。如果您正苦于以下问题:Golang Callers函数的具体用法?Golang Callers怎么用?Golang Callers使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Callers函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: caller
func caller() {
//FIXME add a test to make sure file, pc from Callers matches
// the equivalent from Caller.
for i := 0; i < 2; i++ {
_, _, _, ok := runtime.Caller(i)
if !ok {
panic(i)
}
}
_, _, _, ok := runtime.Caller(20)
if ok {
panic(3)
}
pcA := make([]uintptr, 6, 6)
count := runtime.Callers(0, pcA)
pcB := make([]uintptr, 6, 6)
countM1 := runtime.Callers(1, pcB)
if count-1 != countM1 {
panic(5)
}
for i := 1; i < countM1-1; i++ {
if pcA[i+1] != pcB[i] {
panic(100 + i)
}
}
}
示例2: NewRecord
func NewRecord(level Level, message string) *Record {
record := &Record{
Level: level,
Message: message,
}
record.Time = time.Now()
var pcs [2]uintptr
numStack := runtime.Callers(4, pcs[:])
for i := 0; i < numStack; i += 1 {
function := runtime.FuncForPC(pcs[i])
if function == nil {
break
}
filename, line := function.FileLine(pcs[i])
if filename == srcFile {
continue
} else {
record.FileName = filename
record.LineNo = line
record.FuncName = function.Name()
break
}
}
return record
}
示例3: formatCallpath
func formatCallpath(calldepth int) string {
v := ""
callers := make([]uintptr, 64)
n := runtime.Callers(calldepth+2, callers)
oldPc := callers[n-1]
recursiveCall := false
for i := n - 3; i >= 0; i-- {
pc := callers[i]
if oldPc == pc {
recursiveCall = true
continue
}
oldPc = pc
if recursiveCall {
recursiveCall = false
v += ".."
}
if i < n-3 {
v += "."
}
if f := runtime.FuncForPC(pc); f != nil {
v += formatFuncName(fmtVerbShortfunc, f.Name())
}
}
return v
}
示例4: VDepth
// VDepth reports whether verbosity at the call site is at least the requested
// level.
func VDepth(level level, depth int) bool {
// This function tries hard to be cheap unless there's work to do.
// The fast path is two atomic loads and compares.
// Here is a cheap but safe test to see if V logging is enabled globally.
if logging.verbosity.get() >= level {
return true
}
// It's off globally but it vmodule may still be set.
// Here is another cheap but safe test to see if vmodule is enabled.
if atomic.LoadInt32(&logging.filterLength) > 0 {
// Now we need a proper lock to use the logging structure. The pcs field
// is shared so we must lock before accessing it. This is fairly expensive,
// but if V logging is enabled we're slow anyway.
logging.mu.Lock()
defer logging.mu.Unlock()
if runtime.Callers(2+depth, logging.pcs[:]) == 0 {
return false
}
v, ok := logging.vmap[logging.pcs[0]]
if !ok {
v = logging.setV(logging.pcs[0])
}
return v >= level
}
return false
}
示例5: stackFrames
// Generates a stack from runtime.Callers()
func stackFrames(skip int, ignoreRuntime bool) []*frameInfo {
frames := []*frameInfo{}
size := 20
pcs := make([]uintptr, size)
// always skip the first frame, since it's runtime.Callers itself
pcs = pcs[:runtime.Callers(1+skip, pcs)]
for _, pc := range pcs {
fn := runtime.FuncForPC(pc)
name := fn.Name()
file, line := fn.FileLine(pc - 1)
if ignoreRuntime && strings.Contains(file, filepath.Join("src", "runtime")) {
break
}
ci := &frameInfo{
filename: file,
lineno: line,
method: name,
pc: pc,
}
frames = append(frames, ci)
}
return frames
}
示例6: identifyPanic
func identifyPanic() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
switch {
case name != "":
return fmt.Sprintf("%v:%v", name, line)
case file != "":
return fmt.Sprintf("%v:%v", file, line)
}
return fmt.Sprintf("pc:%x", pc)
}
示例7: getPanic
// getPanic returns the number of frames to skip and the PC
// for the uppermost panic in the call stack (there might be
// multiple panics when a recover() catches a panic and then
// panics again). The second value indicates how many stack frames
// should be skipped in the stacktrace (they might not always match).
// The last return value indicates a frame could be found.
func getPanic() (int, int, uintptr, bool) {
skip := 0
callers := make([]uintptr, 10)
for {
calls := callers[:runtime.Callers(skip, callers)]
c := len(calls)
if c == 0 {
break
}
for ii := c - 1; ii >= 0; ii-- {
f := runtime.FuncForPC(calls[ii])
if f != nil {
name := f.Name()
if strings.HasPrefix(name, "runtime.") && strings.Contains(name, "panic") {
pcSkip := skip + ii - 1
stackSkip := pcSkip
switch name {
case "runtime.panic":
case "runtime.sigpanic":
stackSkip -= 2
default:
stackSkip--
}
return pcSkip, stackSkip, calls[ii], true
}
}
}
skip += c
}
return 0, 0, 0, false
}
示例8: getCaller
func getCaller() string {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(3, pc)
f := runtime.FuncForPC(pc[0])
return f.Name()
}
示例9: Warn
func Warn(v ...interface{}) {
if logobj.level <= WARN {
pc := make([]uintptr, 1)
runtime.Callers(2, pc)
logobj.output("WARN", pc[0], v...)
}
}
示例10: Info
func Info(v ...interface{}) {
if logobj.level <= INFO {
pc := make([]uintptr, 1)
runtime.Callers(2, pc)
logobj.output("INFO", pc[0], v...)
}
}
示例11: Debug
func Debug(v ...interface{}) {
if logobj.level <= DEBUG {
pc := make([]uintptr, 1)
runtime.Callers(2, pc) // 层次越深 性能越差
logobj.output("DEBUG", pc[0], v...)
}
}
示例12: format
func (self *ErrorField) format(e *Event) string {
if e.Error == nil {
return ""
}
var buffer bytes.Buffer
callers := make([]uintptr, 20)
n := runtime.Callers(3, callers) // starts in (*Logger).Log or similar
callers = callers[:n]
buffer.WriteString("\n")
errorType := reflect.TypeOf(e.Error)
buffer.WriteString(errorType.String() + ": ")
buffer.WriteString(e.Error.Error())
for _, pc := range callers {
f := runtime.FuncForPC(pc)
if !strings.Contains(f.Name(), "/slf4g/") {
pathname, lineno := f.FileLine(pc)
filename := path.Base(pathname)
s := fmt.Sprintf("\n at %s (%s:%d)", f.Name(), filename, lineno)
buffer.WriteString(s)
}
}
return buffer.String()
}
示例13: Fail
func Fail(message string) {
log.Error("Failed assertion: %v", message)
var buffer bytes.Buffer
callers := make([]uintptr, 20)
skip := 1
n := runtime.Callers(skip, callers)
callers = callers[:n]
buffer.WriteString("Assertion failed")
buffer.WriteString("\n")
for _, pc := range callers {
f := runtime.FuncForPC(pc)
//if !strings.Contains(f.Name(), "/gova/log/")
{
pathname, lineno := f.FileLine(pc)
filename := path.Base(pathname)
s := fmt.Sprintf("\n at %s (%s:%d)", f.Name(), filename, lineno)
buffer.WriteString(s)
}
}
log.Error(buffer.String())
// TODO: Should we panic?
panic("Assertion failed: " + buffer.String())
}
示例14: callerName
// callerName returns the package path and function name of the calling
// function. The skip argument has the same meaning as the skip argument of
// runtime.Callers.
func callerName(skip int) (pkgPath, funcName string, ok bool) {
var pc [1]uintptr
n := runtime.Callers(skip+1, pc[:])
if n != 1 {
return "", "", false
}
f := runtime.FuncForPC(pc[0])
fullName := f.Name()
lastDotIndex := strings.LastIndex(fullName, ".")
if lastDotIndex == -1 {
panic("unable to distinguish function name from package")
}
if fullName[lastDotIndex-1] == ')' {
// The caller is a method on some type, so it's name looks like
// "pkg/path.(type).method". We need to go back one dot farther to get
// to the package name.
lastDotIndex = strings.LastIndex(fullName[:lastDotIndex], ".")
}
pkgPath = fullName[:lastDotIndex]
funcName = fullName[lastDotIndex+1:]
ok = true
return
}
示例15: recordDBConn
func recordDBConn(path string, db *gouchstore.Gouchstore) {
callers := make([]uintptr, 32)
n := runtime.Callers(2, callers)
openConnLock.Lock()
openConns[db] = dbOpenState{path, frameSnap(callers[:n-1])}
openConnLock.Unlock()
}