From c0015e89c0749aa26ba71824e85904a2e0e85c9e Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 11 Jul 2019 21:27:37 -0700 Subject: [PATCH] calltrace --- calltrace.go | 62 +++++++++++++++++++++++++++++++++++++++ default_writing_router.go | 4 +++ pretty_writing_router.go | 9 ++++++ 3 files changed, 75 insertions(+) create mode 100644 calltrace.go diff --git a/calltrace.go b/calltrace.go new file mode 100644 index 0000000..0451887 --- /dev/null +++ b/calltrace.go @@ -0,0 +1,62 @@ +package flog + +import ( + "fmt" + "runtime" + "strings" +) + +func calltrace() []string { + + const max = 10 + + var frames *runtime.Frames + { + var buffer [max]uintptr + + var pc []uintptr = buffer[:] + + n := runtime.Callers(0, pc) + if 0 >= n { + return nil + } + pc = pc[:n] + frames = runtime.CallersFrames(pc) + } + + var trace []string + { + var buffer [max]string + trace = buffer[:0] + + var more bool = true + for more { + var frame runtime.Frame + + frame, more = frames.Next() + switch frame.Function { + case "runtime.Callers": + continue + default: + if strings.HasPrefix(frame.Function, "github.com/reiver/go-flog.") { + continue + } + } + + var filename string = frame.File + if index := strings.LastIndex(filename, "/"); 0 <= index { + filename = filename[1+index:] + } + + s := fmt.Sprintf("%s():%s:%d", frame.Function, filename, frame.Line) + + trace = append(trace, s) + + if !more { + break + } + } + } + + return trace +} diff --git a/default_writing_router.go b/default_writing_router.go index 46d9125..6254270 100644 --- a/default_writing_router.go +++ b/default_writing_router.go @@ -63,6 +63,10 @@ func (router *DefaultWritingRouter) Route(message string, context map[string]int p = dotquote.AppendString(p, message, "text") p = append(p, ' ') p = dotquote.AppendString(p, time.Now().String(), "when") + if trace := calltrace(); nil != trace { + p = append(p, ' ') + p = dotquote.AppendStrings(p, trace, "calltrace") + } // If we have an error, then get the error.Error() into the log too. if errorFieldValue, ok := context["~error"]; ok { diff --git a/pretty_writing_router.go b/pretty_writing_router.go index 37b03e9..1df9615 100644 --- a/pretty_writing_router.go +++ b/pretty_writing_router.go @@ -41,6 +41,7 @@ func (router *PrettyWritingRouter) Route(message string, context map[string]inte const STYLE_MESSAGE = "\x1b[44;37;1m" // BG BLUE, FG WHITE, BOLD const STYLE_DEFAULT = "\033[95m" // HEADER const STYLE_RESET = "\033[0m" // RESET + const STYLE_CALLTRACE = "\033[40;36;1m" // BG BLACK, FG CYAN, BOLD str := "" @@ -101,6 +102,14 @@ func (router *PrettyWritingRouter) Route(message string, context map[string]inte str = fmt.Sprintf("%s\t%s%s%s=%s%#v%s", str, style, key, STYLE_RESET, style, value, STYLE_RESET) } + if trace := calltrace(); nil != trace { + str = fmt.Sprintf("%s\t⟨⟨", str) + for _, s := range trace { + str = fmt.Sprintf("%s %s%s%s,", str, STYLE_CALLTRACE, s, STYLE_RESET) + } + str = fmt.Sprintf("%s ⟩⟩", str) + } + fmt.Fprintln(router.writer, str) //@TODO: Should this be checking for errors from fmt.Fprintln()?