2021-11-03 19:02:27 +00:00
|
|
|
package klog
|
2021-11-01 14:41:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2021-11-02 03:08:23 +00:00
|
|
|
"text/template"
|
2021-11-01 14:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func caller(depth int) string {
|
|
|
|
_, file, line, _ := runtime.Caller(depth)
|
|
|
|
short := file
|
|
|
|
for i := len(file) - 1; i > 0; i-- {
|
|
|
|
if file[i] == '/' {
|
|
|
|
short = file[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d", short, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stack() string {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
if n < len(buf) {
|
|
|
|
return string(buf[:n])
|
|
|
|
}
|
|
|
|
buf = make([]byte, 2*len(buf))
|
|
|
|
}
|
|
|
|
}
|
2021-11-02 03:08:23 +00:00
|
|
|
|
|
|
|
func getAllOffsprings(l *Logger) []*Logger {
|
|
|
|
if l.subs == nil || len(l.subs) == 0 {
|
|
|
|
return []*Logger{l}
|
|
|
|
}
|
|
|
|
loggers := []*Logger{}
|
|
|
|
for i := range l.subs {
|
|
|
|
loggers = append(loggers, getAllOffsprings(l.subs[i])...)
|
|
|
|
}
|
|
|
|
return loggers
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFuncMap(source template.FuncMap) template.FuncMap {
|
|
|
|
dest := template.FuncMap{}
|
|
|
|
for k, v := range source {
|
|
|
|
dest[k] = v
|
|
|
|
}
|
|
|
|
return dest
|
|
|
|
}
|