Пример #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
// CallerFileHandler returns a Handler that adds the line number and file of
// the calling function to the context with key "caller".
//
// Skips skip number of lines from the top of the stack.
func CallerFileHandler(skip int, h log15.Handler) log15.Handler {
	return log15.FuncHandler(func(r *log15.Record) error {
		call := stack.Call(r.CallPC[0])
		if skip > 0 {
			callers := stack.Callers()
			if len(callers) > skip {
				call = callers[skip]
			} else {
				call = callers[len(callers)-1]
			}
		}
		r.Ctx = append(r.Ctx, "caller", fmt.Sprint(call))
		return h.Log(r)
	})
}
Пример #3
0
// 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)
	})
}
Пример #4
0
// closes the connection
func (c conn) Close() error {
	debug("Close connection", "conn", c, "strace", stack.Callers())
	err := c.cx.Close()
	c.cx = nil
	return err
}
Пример #5
0
func BenchmarkCallers(b *testing.B) {
	for i := 0; i < b.N; i++ {
		stack.Callers()
	}
}