go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 35e397917bdb4f431812d56eac3a9f8696a22f42
parent b616b16c4668f2135ef5ecd52e78e8a6927b5c1d
Author: lash <dev@holbrook.no>
Date:   Tue, 18 Apr 2023 09:29:54 +0100

Correct caller information in logger

Diffstat:
Mlogging/vanilla.go | 55+++++++++++++++++++++++++++++++++++++------------------
1 file changed, 37 insertions(+), 18 deletions(-)

diff --git a/logging/vanilla.go b/logging/vanilla.go @@ -34,22 +34,23 @@ func(v Vanilla) Printf(level int, msg string, args ...any) { v.Writef(LogWriter, level, msg, args...) } -func(v Vanilla) Writef(w io.Writer, level int, msg string, args ...any) { +func(v Vanilla) writef(w io.Writer, file string, line int, level int, msg string, args ...any) { if level > v.levelFilter { return } - var file string - var line int - _, file, line, _ = runtime.Caller(0) // BUG: needs to be passed from top to aliased private functions - basefile := path.Base(file) argsStr := argsToString(args) if len(msg) > 0 { - fmt.Fprintf(w, "[%s] %s:%s:%v %s\t%s\n", v.AsString(level), v.domain, basefile, line, msg, argsStr) + fmt.Fprintf(w, "[%s] %s:%s:%v %s\t%s\n", v.AsString(level), v.domain, file, line, msg, argsStr) } else { - fmt.Fprintf(w, "[%s] %s:%s:%v %s\n", v.AsString(level), v.domain, basefile, line, argsStr) + fmt.Fprintf(w, "[%s] %s:%s:%v %s\n", v.AsString(level), v.domain, file, line, argsStr) } } +func(v Vanilla) Writef(w io.Writer, level int, msg string, args ...any) { + file, line := getCaller(2) + v.writef(w, file, line, level, msg, args) +} + func argsToString(args []any) string { var s string c := len(args) @@ -79,8 +80,18 @@ func(v Vanilla) WriteCtxf(ctx context.Context, w io.Writer, level int, msg strin v.Writef(w, level, msg, args...) } +func(v Vanilla) printf(level int, msg string, args ...any) { + file, line := getCaller(3) + v.writef(LogWriter, file, line, level, msg, args...) +} + +func(v Vanilla) printCtxf(ctx context.Context, level int, msg string, args ...any) { + file, line := getCaller(3) + v.writef(LogWriter, file, line, level, msg, args...) +} + func(v Vanilla) PrintCtxf(ctx context.Context, level int, msg string, args ...any) { - v.Printf(level, msg, args...) + v.printf(level, msg, args...) } func(v Vanilla) AsString(level int) string { @@ -88,41 +99,49 @@ func(v Vanilla) AsString(level int) string { } func(v Vanilla) Tracef(msg string, args ...any) { - v.Printf(LVL_TRACE, msg, args...) + v.printf(LVL_TRACE, msg, args...) } func(v Vanilla) Debugf(msg string, args ...any) { - v.Printf(LVL_DEBUG, msg, args...) + v.printf(LVL_DEBUG, msg, args...) } func(v Vanilla) Infof(msg string, args ...any) { - v.Printf(LVL_INFO, msg, args...) + v.printf(LVL_INFO, msg, args...) } func(v Vanilla) Warnf(msg string, args ...any) { - v.Printf(LVL_WARN, msg, args...) + v.printf(LVL_WARN, msg, args...) } func(v Vanilla) Errorf(msg string, args ...any) { - v.Printf(LVL_ERROR, msg, args...) + v.printf(LVL_ERROR, msg, args...) } func(v Vanilla) TraceCtxf(ctx context.Context, msg string, args ...any) { - v.PrintCtxf(ctx, LVL_TRACE, msg, args...) + v.printCtxf(ctx, LVL_TRACE, msg, args...) } func(v Vanilla) DebugCtxf(ctx context.Context, msg string, args ...any) { - v.PrintCtxf(ctx, LVL_DEBUG, msg, args...) + v.printCtxf(ctx, LVL_DEBUG, msg, args...) } func(v Vanilla) InfoCtxf(ctx context.Context, msg string, args ...any) { - v.PrintCtxf(ctx, LVL_INFO, msg, args...) + v.printCtxf(ctx, LVL_INFO, msg, args...) } func(v Vanilla) WarnCtxf(ctx context.Context, msg string, args ...any) { - v.PrintCtxf(ctx, LVL_WARN, msg, args...) + v.printCtxf(ctx, LVL_WARN, msg, args...) } func(v Vanilla) ErrorCtxf(ctx context.Context, msg string, args ...any) { - v.PrintCtxf(ctx, LVL_ERROR, msg, args...) + v.printCtxf(ctx, LVL_ERROR, msg, args...) +} + +func getCaller(depth int) (string, int) { + var file string + var line int + _, file, line,_ = runtime.Caller(depth) + baseFile := path.Base(file) + return baseFile, line }