当前位置: 首页>>代码示例>>Golang>>正文


Golang runtime.Callers函数代码示例

本文整理汇总了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)
		}
	}
}
开发者ID:rocky,项目名称:ssa-interp,代码行数:26,代码来源:rtcall.go

示例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
}
开发者ID:DrWrong,项目名称:monica,代码行数:26,代码来源:record.go

示例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
}
开发者ID:skycoin,项目名称:skycoin-exchange,代码行数:27,代码来源:format.go

示例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
}
开发者ID:rohanahata,项目名称:cockroach,代码行数:30,代码来源:clog.go

示例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
}
开发者ID:paulrosania,项目名称:logxi,代码行数:27,代码来源:callstack.go

示例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)
}
开发者ID:voidabhi,项目名称:golang-scripts,代码行数:27,代码来源:panic.go

示例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
}
开发者ID:rainycape,项目名称:command,代码行数:37,代码来源:recover.go

示例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()

}
开发者ID:dailing,项目名称:levlog,代码行数:7,代码来源:levlog.go

示例9: Warn

func Warn(v ...interface{}) {
	if logobj.level <= WARN {
		pc := make([]uintptr, 1)
		runtime.Callers(2, pc)
		logobj.output("WARN", pc[0], v...)
	}
}
开发者ID:SteveWarm,项目名称:gologger,代码行数:7,代码来源:woolog.go

示例10: Info

func Info(v ...interface{}) {
	if logobj.level <= INFO {
		pc := make([]uintptr, 1)
		runtime.Callers(2, pc)
		logobj.output("INFO", pc[0], v...)
	}
}
开发者ID:SteveWarm,项目名称:gologger,代码行数:7,代码来源:woolog.go

示例11: Debug

func Debug(v ...interface{}) {
	if logobj.level <= DEBUG {
		pc := make([]uintptr, 1)
		runtime.Callers(2, pc) // 层次越深 性能越差
		logobj.output("DEBUG", pc[0], v...)
	}
}
开发者ID:SteveWarm,项目名称:gologger,代码行数:7,代码来源:woolog.go

示例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()
}
开发者ID:justinsb,项目名称:slf4g,代码行数:28,代码来源:layout_field.go

示例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())
}
开发者ID:justinsb,项目名称:gova,代码行数:29,代码来源:assert.go

示例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
}
开发者ID:robinivanka,项目名称:blueprint,代码行数:30,代码来源:package_ctx.go

示例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()
}
开发者ID:andradeandrey,项目名称:seriesly,代码行数:7,代码来源:debug.go


注:本文中的runtime.Callers函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。