diff --git a/cmd/olcrtc/main.go b/cmd/olcrtc/main.go index aeae0d0..65140d9 100644 --- a/cmd/olcrtc/main.go +++ b/cmd/olcrtc/main.go @@ -72,6 +72,7 @@ func run() error { } func runWithArgs(args []string) error { + logger.DisableNoisyPionLogs() session.RegisterDefaults() if len(args) != 1 || args[0] == "-h" || args[0] == "--help" || args[0] == "-help" { @@ -335,9 +336,9 @@ func (f filteredWriter) Write(p []byte) (int, error) { func configureLogging(debug bool) { log.SetOutput(filteredWriter{w: os.Stderr}) + logger.DisableNoisyPionLogs() if debug { logger.SetVerbose(true) - _ = os.Setenv("PION_LOG_DISABLE", "turnc") return } _ = os.Setenv("PION_LOG_DISABLE", "all") diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 34aac75..795e92f 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -4,6 +4,7 @@ package logger import ( "fmt" "log" + "os" "strings" "sync/atomic" @@ -13,6 +14,72 @@ import ( // verboseEnabled controls whether verbose and debug logging is enabled. var verboseEnabled atomic.Bool //nolint:gochecknoglobals // package-level state intentional +// DisableNoisyPionLogs suppresses Pion scopes that are known to emit +// high-volume non-actionable background noise. +func DisableNoisyPionLogs() { + mergePionLogDisable("turnc") + removePionLogScopes([]string{"turnc"}, "ERROR", "WARN", "INFO", "DEBUG", "TRACE") +} + +func mergePionLogDisable(scopes ...string) { + const envKey = "PION_LOG_DISABLE" + current := strings.TrimSpace(os.Getenv(envKey)) + if strings.EqualFold(current, "all") { + return + } + seen := make(map[string]struct{}) + var merged []string + for _, scope := range strings.Split(current, ",") { + scope = strings.TrimSpace(strings.ToLower(scope)) + if scope == "" { + continue + } + seen[scope] = struct{}{} + merged = append(merged, scope) + } + for _, scope := range scopes { + scope = strings.TrimSpace(strings.ToLower(scope)) + if scope == "" { + continue + } + if _, ok := seen[scope]; ok { + continue + } + seen[scope] = struct{}{} + merged = append(merged, scope) + } + _ = os.Setenv(envKey, strings.Join(merged, ",")) +} + +func removePionLogScopes(scopes []string, levels ...string) { + remove := make(map[string]struct{}, len(scopes)) + for _, scope := range scopes { + scope = strings.TrimSpace(strings.ToLower(scope)) + if scope != "" { + remove[scope] = struct{}{} + } + } + for _, level := range levels { + envKey := "PION_LOG_" + level + current := strings.TrimSpace(os.Getenv(envKey)) + if current == "" || strings.EqualFold(current, "all") { + continue + } + var kept []string + for _, scope := range strings.Split(current, ",") { + scope = strings.TrimSpace(strings.ToLower(scope)) + if scope == "" { + continue + } + if _, drop := remove[scope]; drop { + continue + } + kept = append(kept, scope) + } + _ = os.Setenv(envKey, strings.Join(kept, ",")) + } +} + // SetVerbose enables or disables verbose/debug logging. func SetVerbose(enabled bool) { verboseEnabled.Store(enabled) diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go index ae22d5a..35becf2 100644 --- a/internal/logger/logger_test.go +++ b/internal/logger/logger_test.go @@ -3,6 +3,7 @@ package logger import ( "bytes" "log" + "os" "strings" "testing" ) @@ -89,3 +90,18 @@ func TestPionLoggerDropsTURNRefreshNoise(t *testing.T) { t.Fatalf("expected normal warning to pass through, got %q", got) } } + +func TestDisableNoisyPionLogsMergesTurncScope(t *testing.T) { + t.Setenv("PION_LOG_DISABLE", "ice") + t.Setenv("PION_LOG_ERROR", "turnc,ice") + + DisableNoisyPionLogs() + + got := os.Getenv("PION_LOG_DISABLE") + if !strings.Contains(got, "ice") || !strings.Contains(got, "turnc") { + t.Fatalf("PION_LOG_DISABLE = %q, want ice and turnc", got) + } + if got := os.Getenv("PION_LOG_ERROR"); got != "ice" { + t.Fatalf("PION_LOG_ERROR = %q, want ice", got) + } +}