calltrace
parent
ac46db3f16
commit
c0015e89c0
|
@ -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
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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()?
|
||||
|
|
Loading…
Reference in New Issue