Beispiel #1
0
func (l *Logger) output(traceskip int, err error, t LogType, s string) error {
	var stack trace.Stack
	if l.isTraceEnabled(t) {
		stack = trace.TraceN(traceskip+1, 32)
	}

	var b bytes.Buffer
	if t != 0 {
		fmt.Fprint(&b, t, " ", s)
	} else {
		fmt.Fprint(&b, "[LOG] ", s)
	}

	if len(s) == 0 || s[len(s)-1] != '\n' {
		fmt.Fprint(&b, "\n")
	}

	if len(stack) != 0 {
		fmt.Fprint(&b, "[stack]: \n", stack.StringWithIndent(1))
	}
	if err != nil {
		fmt.Fprint(&b, "[error]: ", err.Error(), "\n")
		if stack := errors.ErrorStack(err); stack != nil {
			fmt.Fprint(&b, stack.StringWithIndent(1))
		}
	}
	s = b.String()
	l.mu.Lock()
	defer l.mu.Unlock()
	return l.log.Output(traceskip+2, s)
}
Beispiel #2
0
func Errorf(format string, v ...interface{}) error {
	err := fmt.Errorf(format, v...)
	if !TraceEnabled {
		return err
	}
	return &Error{
		Stack: trace.TraceN(1, 32),
		Cause: err,
	}
}
Beispiel #3
0
func New(s string) error {
	err := errors.New(s)
	if !TraceEnabled {
		return err
	}
	return &Error{
		Stack: trace.TraceN(1, 32),
		Cause: err,
	}
}
Beispiel #4
0
func ErrorIsNil(t *testing.T, err error) {
	if err == nil {
		return
	}
	stack := errors.ErrorStack(err)
	if stack == nil {
		stack = trace.TraceN(1, 32)
	}
	t.Fatalf("%s\n%s", err, stack)
}
Beispiel #5
0
func Trace(err error) error {
	if err == nil || !TraceEnabled {
		return err
	}
	_, ok := err.(*Error)
	if ok {
		return err
	}
	return &Error{
		Stack: trace.TraceN(1, 32),
		Cause: err,
	}
}
Beispiel #6
0
func Must(t *testing.T, b bool) {
	if b {
		return
	}
	t.Fatalf("assertion failed\n%s", trace.TraceN(1, 32))
}
Beispiel #7
0
func Fatalf(t *testing.T, format string, args ...interface{}) {
	t.Fatalf("%s\n%s", fmt.Sprintf(format, args...), trace.TraceN(1, 32))
}