From 09fac44b7ceb4d48ffa647ad24f7e2823030c431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Ollah?= Date: Sat, 25 May 2024 14:56:14 +0200 Subject: [PATCH] Fix #2 --- handler.go | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/handler.go b/handler.go index a6cc70d..9506bd3 100644 --- a/handler.go +++ b/handler.go @@ -70,29 +70,31 @@ func (h *Handler) Handle(_ context.Context, r slog.Record) error { } fmt.Fprint(&bf, " ") - if r.PC != 0 { - f, _ := runtime.CallersFrames([]uintptr{r.PC}).Next() - - var filename string - switch h.opts.SrcFileMode { - case Nop: - break - case ShortFile: - filename = filepath.Base(f.File) - case LongFile: - filename = f.File - } - lineStr := fmt.Sprintf(":%d", f.Line) - formatted := fmt.Sprintf("%s ", filename+lineStr) - if h.opts.SrcFileLength > 0 { - maxFilenameLen := h.opts.SrcFileLength - len(lineStr) - 1 - if len(filename) > maxFilenameLen { - filename = filename[:maxFilenameLen] // Truncate if too long + if h.opts.SrcFileMode != Nop { + if r.PC != 0 { + f, _ := runtime.CallersFrames([]uintptr{r.PC}).Next() + + var filename string + switch h.opts.SrcFileMode { + case Nop: + break + case ShortFile: + filename = filepath.Base(f.File) + case LongFile: + filename = f.File + } + lineStr := fmt.Sprintf(":%d", f.Line) + formatted := fmt.Sprintf("%s ", filename+lineStr) + if h.opts.SrcFileLength > 0 { + maxFilenameLen := h.opts.SrcFileLength - len(lineStr) - 1 + if len(filename) > maxFilenameLen { + filename = filename[:maxFilenameLen] // Truncate if too long + } + lenStr := strconv.Itoa(h.opts.SrcFileLength) + formatted = fmt.Sprintf("%-"+lenStr+"s", filename+lineStr) } - lenStr := strconv.Itoa(h.opts.SrcFileLength) - formatted = fmt.Sprintf("%-"+lenStr+"s", filename+lineStr) + fmt.Fprint(&bf, formatted) } - fmt.Fprint(&bf, formatted) } //we need the attributes here, as we can print a longer string if there are no attributes