41 lines
622 B
Go
41 lines
622 B
Go
package debug
|
|
|
|
import (
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"mini-chat/internal/pkg/core"
|
|
"mini-chat/internal/pkg/trace"
|
|
)
|
|
|
|
type debug struct {
|
|
ctx core.StdContext
|
|
}
|
|
|
|
func WithContext(ctx core.StdContext) *debug {
|
|
return &debug{
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
func (d *debug) Logger(value ...any) {
|
|
debugInfo := new(trace.Debug)
|
|
|
|
defer func() {
|
|
if d.ctx.Trace != nil {
|
|
debugInfo.Stack = fileWithLineNum()
|
|
debugInfo.Value = value
|
|
d.ctx.Trace.AppendDebug(debugInfo)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func fileWithLineNum() string {
|
|
_, file, line, ok := runtime.Caller(3)
|
|
if ok {
|
|
return file + ":" + strconv.FormatInt(int64(line), 10)
|
|
}
|
|
|
|
return ""
|
|
}
|