當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。