You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
581 B
31 lines
581 B
4 years ago
|
package log
|
||
|
|
||
|
import (
|
||
|
"runtime"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// Valuer is returns a log value.
|
||
|
type Valuer func() interface{}
|
||
|
|
||
|
// Value return the function value.
|
||
|
func Value(v interface{}) interface{} {
|
||
|
if v, ok := v.(Valuer); ok {
|
||
|
return v()
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Caller returns returns a Valuer that returns a pkg/file:line description of the caller.
|
||
|
func Caller(depth int) Valuer {
|
||
|
return func() interface{} {
|
||
|
_, file, line, ok := runtime.Caller(depth)
|
||
|
if !ok {
|
||
|
return nil
|
||
|
}
|
||
|
idx := strings.LastIndexByte(file, '/')
|
||
|
return file[idx+1:] + ":" + strconv.Itoa(line)
|
||
|
}
|
||
|
}
|