Blame view
logger/tracing.go
1.19 KB
28a6cc60c Integrated logger... |
1 |
package logger |
e2880d3fb integraded golloger |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import ( "fmt" "math" "runtime" "strings" "time" ) // getCallStack func getCallStack() (stack []string) { const ( maxStackDepth = 10000 skipRuntime = 2 // skip runtime and startup skipCallers = 2 |
28a6cc60c Integrated logger... |
17 |
thisPackage = "logger" |
e2880d3fb integraded golloger |
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
) callers := make([]uintptr, maxStackDepth) // min 1 stackDepth := runtime.Callers(skipCallers, callers) frames := runtime.CallersFrames(callers) for i := 0; i < skipRuntime; i++ { frames.Next() } for i := 0; i < stackDepth-skipRuntime-skipCallers; i++ { frame, next := frames.Next() if !strings.Contains(frame.File, thisPackage) { stack = append(stack, fmt.Sprintf("[%s %d]", frame.File, frame.Line)) } if !next { break } } reverseStack(stack) return stack } // in place func reverseStack(stack []string) { middle := int(math.Floor(float64(len(stack)) / 2.0)) lastIndex := len(stack) - 1 for i := 0; i < middle; i++ { stack[i], stack[lastIndex] = stack[lastIndex], stack[i] lastIndex-- } } func getTrace(format string, v ...interface{}) (t string) { stack := getCallStack() t = time.Now().Format(dateTimeFormat) + ": " for _, s := range stack { t += s + " " } t += fmt.Sprintf(format, v...) + " " return t } |