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:
parent
742f5c095a
commit
3143d32b45
@ -28,9 +28,10 @@ type Logger struct {
|
|||||||
fatalFunc func(err interface{})
|
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
|
// 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)
|
lineString := strconv.Itoa(line)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return ""
|
||||||
@ -47,6 +48,10 @@ func Lshortfile() string {
|
|||||||
return file + ":" + lineString + ":"
|
return file + ":" + lineString + ":"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lshortfile() string {
|
||||||
|
return Lshortfile(2)
|
||||||
|
}
|
||||||
|
|
||||||
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l *Logger) {
|
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l *Logger) {
|
||||||
l = &Logger{}
|
l = &Logger{}
|
||||||
// Use reimplemented Lshortfile since wrapping the log functions messes them up
|
// 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
|
var out string
|
||||||
if l.shortfile {
|
if l.shortfile {
|
||||||
out = Lshortfile()
|
out = lshortfile()
|
||||||
}
|
}
|
||||||
out += " " + l.printer.Sprintf(format, v...)
|
out += " " + l.printer.Sprintf(format, v...)
|
||||||
l.logger.Print(out)
|
l.logger.Print(out)
|
||||||
@ -85,7 +90,7 @@ func (l *Logger) Print(v ...interface{}) {
|
|||||||
}
|
}
|
||||||
var out string
|
var out string
|
||||||
if l.shortfile {
|
if l.shortfile {
|
||||||
out = Lshortfile()
|
out = lshortfile()
|
||||||
}
|
}
|
||||||
out += " " + l.printer.Sprint(v...)
|
out += " " + l.printer.Sprint(v...)
|
||||||
l.logger.Print(out)
|
l.logger.Print(out)
|
||||||
@ -97,7 +102,7 @@ func (l *Logger) Println(v ...interface{}) {
|
|||||||
}
|
}
|
||||||
var out string
|
var out string
|
||||||
if l.shortfile {
|
if l.shortfile {
|
||||||
out = Lshortfile()
|
out = lshortfile()
|
||||||
}
|
}
|
||||||
out += " " + l.printer.Sprintln(v...)
|
out += " " + l.printer.Sprintln(v...)
|
||||||
l.logger.Print(out)
|
l.logger.Print(out)
|
||||||
@ -109,7 +114,7 @@ func (l *Logger) Fatal(v ...interface{}) {
|
|||||||
}
|
}
|
||||||
var out string
|
var out string
|
||||||
if l.shortfile {
|
if l.shortfile {
|
||||||
out = Lshortfile()
|
out = lshortfile()
|
||||||
}
|
}
|
||||||
out += " " + l.printer.Sprint(v...)
|
out += " " + l.printer.Sprint(v...)
|
||||||
l.logger.Fatal(out)
|
l.logger.Fatal(out)
|
||||||
@ -121,7 +126,7 @@ func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|||||||
}
|
}
|
||||||
var out string
|
var out string
|
||||||
if l.shortfile {
|
if l.shortfile {
|
||||||
out = Lshortfile()
|
out = lshortfile()
|
||||||
}
|
}
|
||||||
out += " " + l.printer.Sprintf(format, v...)
|
out += " " + l.printer.Sprintf(format, v...)
|
||||||
if l.fatalFunc != nil {
|
if l.fatalFunc != nil {
|
||||||
|
@ -111,7 +111,7 @@ func (st *Storage) DebugWatch(storeType StoreType, key, mainData string) {
|
|||||||
actionString = "DELETE"
|
actionString = "DELETE"
|
||||||
}
|
}
|
||||||
if logAction == LogAll || mainData == "" {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user