1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

log: include caller in debug storage logging

includes the location where Set*Key/Delete*Key was called.
This commit is contained in:
Harvey Tindall 2023-10-12 18:21:47 +01:00
parent 742f5c095a
commit 3143d32b45
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
2 changed files with 13 additions and 8 deletions

View File

@ -28,9 +28,10 @@ type Logger struct {
fatalFunc func(err interface{})
}
func Lshortfile() string {
// Lshortfile is a re-implemented log.Lshortfile with a modifiable call level.
func Lshortfile(level int) string {
// 0 = This function, 1 = Print/Printf/Println, 2 = Caller of Print/Printf/Println
_, file, line, ok := runtime.Caller(2)
_, file, line, ok := runtime.Caller(level)
lineString := strconv.Itoa(line)
if !ok {
return ""
@ -47,6 +48,10 @@ func Lshortfile() string {
return file + ":" + lineString + ":"
}
func lshortfile() string {
return Lshortfile(2)
}
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l *Logger) {
l = &Logger{}
// Use reimplemented Lshortfile since wrapping the log functions messes them up
@ -73,7 +78,7 @@ func (l *Logger) Printf(format string, v ...interface{}) {
}
var out string
if l.shortfile {
out = Lshortfile()
out = lshortfile()
}
out += " " + l.printer.Sprintf(format, v...)
l.logger.Print(out)
@ -85,7 +90,7 @@ func (l *Logger) Print(v ...interface{}) {
}
var out string
if l.shortfile {
out = Lshortfile()
out = lshortfile()
}
out += " " + l.printer.Sprint(v...)
l.logger.Print(out)
@ -97,7 +102,7 @@ func (l *Logger) Println(v ...interface{}) {
}
var out string
if l.shortfile {
out = Lshortfile()
out = lshortfile()
}
out += " " + l.printer.Sprintln(v...)
l.logger.Print(out)
@ -109,7 +114,7 @@ func (l *Logger) Fatal(v ...interface{}) {
}
var out string
if l.shortfile {
out = Lshortfile()
out = lshortfile()
}
out += " " + l.printer.Sprint(v...)
l.logger.Fatal(out)
@ -121,7 +126,7 @@ func (l *Logger) Fatalf(format string, v ...interface{}) {
}
var out string
if l.shortfile {
out = Lshortfile()
out = lshortfile()
}
out += " " + l.printer.Sprintf(format, v...)
if l.fatalFunc != nil {

View File

@ -111,7 +111,7 @@ func (st *Storage) DebugWatch(storeType StoreType, key, mainData string) {
actionString = "DELETE"
}
if logAction == LogAll || mainData == "" {
st.debug.Printf("%s: %s[%s] = \"%s\"\n", actionString, actionKey, key, mainData)
st.debug.Printf("%s @ %s %s[%s] = \"%s\"\n", actionString, logger.Lshortfile(3), actionKey, key, mainData)
}
}