示例#1
0
func deepStack(depth int, b *testing.B) stack.Trace {
	if depth > 0 {
		return deepStack(depth-1, b)
	}
	b.StartTimer()
	s := stack.Callers()
	b.StopTimer()
	return s
}
示例#2
0
文件: handler.go 项目: sisatech/raft
// CallerStackHandler returns a Handler that adds a stack trace to the context
// with key "stack". The stack trace is formated as a space separated list of
// call sites inside matching []'s. The most recent call site is listed first.
// Each call site is formatted according to format. See the documentation of
// log15/stack.Call.Format for the list of supported formats.
func CallerStackHandler(format string, h Handler) Handler {
	return FuncHandler(func(r *Record) error {
		s := stack.Callers().
			TrimBelow(stack.Call(r.CallPC[0])).
			TrimRuntime()
		if len(s) > 0 {
			buf := &bytes.Buffer{}
			buf.WriteByte('[')
			for i, pc := range s {
				if i > 0 {
					buf.WriteByte(' ')
				}
				fmt.Fprintf(buf, format, pc)
			}
			buf.WriteByte(']')
			r.Ctx = append(r.Ctx, "stack", buf.String())
		}
		return h.Log(r)
	})
}
示例#3
0
func BenchmarkCallers(b *testing.B) {
	for i := 0; i < b.N; i++ {
		stack.Callers()
	}
}