bt: filter anacrolix logs to errors so warnings don't corrupt the readout

webseed/peer/tracker warnings (the 403/429 chatter) go through slog, the
rest through the legacy analog logger; both default to Warning, which
interleaves with and corrupts the live progress block. Filter both to
Error and above so only genuine errors reach stderr. quietSlogger is a
small testable helper; anacrolix/log becomes a direct dependency.
This commit is contained in:
2026-06-20 21:11:57 +09:00
parent 3fc29e3a86
commit bb49578892
3 changed files with 46 additions and 1 deletions

View File

@@ -9,12 +9,16 @@ package bt
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
alog "github.com/anacrolix/log"
missinggo "github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
@@ -73,6 +77,14 @@ func ParsePorts(spec string) []int {
return ports
}
// quietSlogger filters anacrolix's structured logging down to Error and above,
// so the routine webseed/peer/tracker warning chatter (the 403/429 noise) no
// longer interleaves with — and corrupts — the live progress display. Genuine
// errors still reach w.
func quietSlogger(w io.Writer) *slog.Logger {
return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{Level: slog.LevelError}))
}
// NewClient builds the shared anacrolix client from the run-wide config. When a
// listen-port spec is given it tries each candidate port in turn, advancing past
// any that is already in use, and falls back to an OS-chosen port if none bind,
@@ -91,6 +103,11 @@ func ParsePorts(spec string) []int {
// prevention must come from a run-wide setting (e.g. --enable-dht=false).
func NewClient(cfg ClientConfig) (*torrent.Client, error) {
c := torrent.NewDefaultClientConfig()
// Keep the library's own logging out of the readout. Webseed/peer warnings go
// through slog, the rest through the legacy analog logger, so filter both to
// Error and above (the analog default is Warning, which is what leaks today).
c.Slogger = quietSlogger(os.Stderr)
c.Logger = alog.Default.FilterLevel(alog.Error)
c.DataDir = cfg.Dir
if c.DataDir == "" {
c.DataDir = "."