all: eliminate usage of global ProxyLogger

So setting up logging for ctrld binary and ctrld packages could be done
more easily, decouple the required setup for interactive vs daemon
running.

This is the first step toward replacing rs/zerolog libary with a
different logging library.
This commit is contained in:
Cuong Manh Le
2025-04-03 21:17:02 +07:00
committed by Cuong Manh Le
parent 47c04bf0f6
commit 0e66697247
39 changed files with 425 additions and 420 deletions
+10 -8
View File
@@ -17,26 +17,27 @@ var (
)
// HasIPv6 reports whether the current network stack has IPv6 available.
func HasIPv6() bool {
func HasIPv6(ctx context.Context) bool {
hasIPv6Once.Do(func() {
ProxyLogger.Load().Debug().Msg("checking for IPv6 availability once")
logger := LoggerFromCtx(ctx)
logger.Debug().Msg("checking for IPv6 availability once")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
val := ctrldnet.IPv6Available(ctx)
ipv6Available.Store(val)
ProxyLogger.Load().Debug().Msgf("ipv6 availability: %v", val)
logger.Debug().Msgf("ipv6 availability: %v", val)
mon, err := netmon.New(func(format string, args ...any) {})
if err != nil {
ProxyLogger.Load().Debug().Err(err).Msg("failed to monitor IPv6 state")
logger.Debug().Err(err).Msg("failed to monitor IPv6 state")
return
}
mon.RegisterChangeCallback(func(delta *netmon.ChangeDelta) {
old := ipv6Available.Load()
cur := delta.Monitor.InterfaceState().HaveV6
if old != cur {
ProxyLogger.Load().Warn().Msgf("ipv6 availability changed, old: %v, new: %v", old, cur)
logger.Warn().Msgf("ipv6 availability changed, old: %v, new: %v", old, cur)
} else {
ProxyLogger.Load().Debug().Msg("ipv6 availability does not changed")
logger.Debug().Msg("ipv6 availability does not changed")
}
ipv6Available.Store(cur)
})
@@ -46,8 +47,9 @@ func HasIPv6() bool {
}
// DisableIPv6 marks IPv6 as unavailable if enabled.
func DisableIPv6() {
func DisableIPv6(ctx context.Context) {
if ipv6Available.CompareAndSwap(true, false) {
ProxyLogger.Load().Debug().Msg("turned off IPv6 availability")
logger := LoggerFromCtx(ctx)
logger.Debug().Msg("turned off IPv6 availability")
}
}