torrent priority and piece state fix

This commit is contained in:
royalcat 2024-07-07 23:09:13 +03:00
parent 13ce2aa07f
commit 199a82ff0c
33 changed files with 2227 additions and 959 deletions
pkg/rlog

View file

@ -34,24 +34,35 @@ func AddHandler(nh slog.Handler) {
}
type Logger struct {
handler slog.Handler
component []string
handler slog.Handler
callNesting int
component []string
}
const functionKey = "function"
const componentKey = "component"
const componentSep = "."
func (l *Logger) log(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
var pcs [1]uintptr
runtime.Callers(3, pcs[:])
runtime.Callers(3+l.callNesting, pcs[:])
pc := pcs[0]
f := runtime.FuncForPC(pc)
attrs = append(attrs, slog.String(functionKey, f.Name()))
if f != nil {
attrs = append(attrs, slog.String(functionKey, f.Name()))
}
if len(l.component) > 0 {
attrs = append(attrs, slog.String(componentKey, strings.Join(l.component, componentSep)))
}
r := slog.NewRecord(time.Now(), level, msg, pc)
r.AddAttrs(attrs...)
if ctx == nil {
ctx = context.Background()
}
_ = l.handler.Handle(ctx, r)
}
@ -71,16 +82,10 @@ func (l *Logger) Error(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, slog.LevelError, msg, attrs...)
}
const componentKey = "component"
const componentSep = "."
func (log *Logger) WithComponent(name string) *Logger {
c := append(log.component, name)
return &Logger{
handler: log.handler.WithAttrs([]slog.Attr{
slog.String(componentKey, strings.Join(c, componentSep)),
}),
component: c,
handler: log.handler,
component: append(log.component, name),
}
}
@ -91,23 +96,20 @@ func (l *Logger) With(attrs ...slog.Attr) *Logger {
}
}
func (l *Logger) Nested(callNesting int) *Logger {
return &Logger{
handler: l.handler,
component: l.component,
callNesting: callNesting,
}
}
// returns a new slog logger with the same attribures as the original logger
// TODO currently not logging function name
func (l *Logger) Slog() *slog.Logger {
return slog.New(l.handler)
}
const endpointKey = "endpoint"
func (l *Logger) WithEndpoint(name string) *Logger {
return &Logger{
handler: l.handler.WithAttrs([]slog.Attr{
slog.String(endpointKey, name),
}),
component: l.component,
}
}
const errKey = "error"
func Error(err error) slog.Attr {