Esempio n. 1
0
func TestCaller(t *testing.T) {
	f := stack.Caller(0)
	const expected = "stack_test.go:83 TestCaller"
	if !strings.HasSuffix(f.String(), expected) {
		t.Fatalf(`expected suffix "%s" got "%s"`, expected, f)
	}
}
Esempio n. 2
0
func TestCaller(t *testing.T) {
	f := stack.Caller(0)
	const expected = "github.com/facebookgo/stack/stack_test.go:83 TestCaller"
	if f.String() != expected {
		t.Fatalf(`expected "%s" got "%s"`, expected, f)
	}
}
Esempio n. 3
0
func (formatter) Format(e *logrus.Entry) ([]byte, error) {
	var t logrus.Formatter

	frame := stack.Caller(7)
	frame.File = stack.StripGOPATH(frame.File)
	e.Data["function"] = frame.Name
	e.Data["file"] = fmt.Sprintf("%s:%d", frame.File, frame.Line)

	if os.Getenv("LOG_FORMAT") == "json" {
		t = &logrus.JSONFormatter{}
	} else {
		t = &logrus.TextFormatter{
			FullTimestamp:   true,
			TimestampFormat: time.Stamp,
		}
	}

	return t.Format(e)
}
Esempio n. 4
0
func (formatter) Format(e *logrus.Entry) ([]byte, error) {
	var f logrus.Formatter

	frame := stack.Caller(7)

	if os.Getenv("LOG_FORMAT") == "json" {
		e.Data["func"] = frame.Name
		e.Data["line"] = frame.Line
		e.Data["file"] = frame.File

		f = json
	} else {
		e.Data["func"] = fmt.Sprintf("%s:%d", frame.Name, frame.Line)

		f = text
	}

	return f.Format(e)
}
Esempio n. 5
0
// WrapSkip may wrap the error and return an augmented error depending on the
// configuration in the context. The defaults result in the error being
// returned as is. It also handles nils correctly, returning a nil in that
// case. If the given error is already wrapped, it returns it as-is.
func WrapSkip(ctx context.Context, err error, skip int) error {
	switch err := err.(type) {
	case nil:
		return nil
	case *singleFrameError:
		return err
	case *singleStackError:
		return err
	case *multiStackError:
		err.multiStack.AddCallers(skip + 1)
		return err
	}

	config := ContextConfig(ctx)
	switch config.StackMode {
	case StackModeSingleFrame:
		return &singleFrameError{
			config:     config,
			underlying: err,
			frame:      stack.Caller(skip + 1),
		}
	case StackModeSingleStack:
		return &singleStackError{
			config:     config,
			underlying: err,
			stack:      stack.Callers(skip + 1),
		}
	case StackModeMultiStack:
		return &multiStackError{
			config:     config,
			underlying: err,
			multiStack: stack.CallersMulti(skip + 1),
		}
	}
	return err
}
Esempio n. 6
0
func BenchmarkCaller(b *testing.B) {
	for i := 0; i < b.N; i++ {
		stack.Caller(0)
	}
}