package zlog import ( "log/slog" "os" "runtime" "strings" "gitlab.bareksa.com/backend/zen/core/zcaller" ) // Caller represents the caller of a function. // // It gives helper methods to get the caller's information. type Caller uintptr func (ca Caller) LogValue() slog.Value { if ca == 0 { return slog.AnyValue(nil) } f := frame{ca.Frame()} return slog.GroupValue( slog.String("file", f.ShortFile()), slog.Int("line", f.Line), slog.String("function", f.ShortFunction()), ) } func (ca Caller) Frame() runtime.Frame { return zcaller.Frame(uintptr(ca)) } type frame struct { runtime.Frame } func (f frame) ShortFile() string { wd, err := os.Getwd() if err != nil { return f.File } if after, found := strings.CutPrefix(f.File, wd); found { return strings.TrimPrefix(after, string(os.PathSeparator)) } return f.File } func (f frame) ShortFunction() string { split := strings.Split(f.Function, string(os.PathSeparator)) return split[len(split)-1] }