From 93ebd0f94946e7934d441e394091dd8fb08f85b0 Mon Sep 17 00:00:00 2001 From: Andrew Keesler Date: Tue, 26 Jan 2021 17:34:27 -0500 Subject: [PATCH] internal/plog: add Enabled() Signed-off-by: Andrew Keesler --- internal/plog/klog.go | 34 ++++++++++++++- internal/plog/level.go | 26 +++++------- internal/plog/level_test.go | 85 +++++++++++++++++++++---------------- 3 files changed, 92 insertions(+), 53 deletions(-) diff --git a/internal/plog/klog.go b/internal/plog/klog.go index aac73503..52759814 100644 --- a/internal/plog/klog.go +++ b/internal/plog/klog.go @@ -1,4 +1,4 @@ -// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package plog @@ -7,6 +7,7 @@ import ( "sync" "github.com/spf13/pflag" + "k8s.io/klog/v2" ) //nolint: gochecknoglobals @@ -30,3 +31,34 @@ func RemoveKlogGlobalFlags() { panic("unsupported global klog flag set") } } + +func klogLevelForPlogLevel(plogLevel LogLevel) (klogLevel klog.Level) { + switch plogLevel { + case LevelWarning: + klogLevel = klogLevelWarning // unset means minimal logs (Error and Warning) + case LevelInfo: + klogLevel = klogLevelInfo + case LevelDebug: + klogLevel = klogLevelDebug + case LevelTrace: + klogLevel = klogLevelTrace + case LevelAll: + klogLevel = klogLevelAll + 100 // make all really mean all + default: + klogLevel = -1 + } + + return +} + +func getKlogLevel() klog.Level { + // hack around klog not exposing a Get method + for i := klog.Level(0); i < 256; i++ { + if klog.V(i).Enabled() { + continue + } + return i - 1 + } + + return -1 +} diff --git a/internal/plog/level.go b/internal/plog/level.go index b1a0bf11..20083d68 100644 --- a/internal/plog/level.go +++ b/internal/plog/level.go @@ -1,4 +1,4 @@ -// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package plog @@ -39,26 +39,20 @@ const ( ) func ValidateAndSetLogLevelGlobally(level LogLevel) error { - var klogLogLevel int - - switch level { - case LevelWarning: - klogLogLevel = klogLevelWarning // unset means minimal logs (Error and Warning) - case LevelInfo: - klogLogLevel = klogLevelInfo - case LevelDebug: - klogLogLevel = klogLevelDebug - case LevelTrace: - klogLogLevel = klogLevelTrace - case LevelAll: - klogLogLevel = klogLevelAll + 100 // make all really mean all - default: + klogLevel := klogLevelForPlogLevel(level) + if klogLevel < 0 { return errInvalidLogLevel } - if _, err := logs.GlogSetter(strconv.Itoa(klogLogLevel)); err != nil { + if _, err := logs.GlogSetter(strconv.Itoa(int(klogLevel))); err != nil { panic(err) // programmer error } return nil } + +// Enabled returns whether the provided plog level is enabled, i.e., whether print statements at the +// provided level will show up. +func Enabled(level LogLevel) bool { + return getKlogLevel() >= klogLevelForPlogLevel(level) +} diff --git a/internal/plog/level_test.go b/internal/plog/level_test.go index 2f3ad9d4..a2fde0b6 100644 --- a/internal/plog/level_test.go +++ b/internal/plog/level_test.go @@ -1,4 +1,4 @@ -// Copyright 2020 the Pinniped contributors. All Rights Reserved. +// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package plog @@ -13,42 +13,50 @@ import ( ) func TestValidateAndSetLogLevelGlobally(t *testing.T) { - originalLogLevel := getKlogLevel(t) + originalLogLevel := getKlogLevel() + require.GreaterOrEqual(t, int(originalLogLevel), int(klog.Level(0)), "cannot get klog level") tests := []struct { - name string - level LogLevel - wantLevel klog.Level - wantErr string + name string + level LogLevel + wantLevel klog.Level + wantEnabled []LogLevel + wantErr string }{ { - name: "unset", - wantLevel: 0, + name: "unset", + wantLevel: 0, + wantEnabled: []LogLevel{LevelWarning}, }, { - name: "warning", - level: LevelWarning, - wantLevel: 0, + name: "warning", + level: LevelWarning, + wantLevel: 0, + wantEnabled: []LogLevel{LevelWarning}, }, { - name: "info", - level: LevelInfo, - wantLevel: 2, + name: "info", + level: LevelInfo, + wantLevel: 2, + wantEnabled: []LogLevel{LevelWarning, LevelInfo}, }, { - name: "debug", - level: LevelDebug, - wantLevel: 4, + name: "debug", + level: LevelDebug, + wantLevel: 4, + wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug}, }, { - name: "trace", - level: LevelTrace, - wantLevel: 6, + name: "trace", + level: LevelTrace, + wantLevel: 6, + wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace}, }, { - name: "all", - level: LevelAll, - wantLevel: 108, + name: "all", + level: LevelAll, + wantLevel: 108, + wantEnabled: []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace, LevelAll}, }, { name: "invalid level", @@ -66,26 +74,31 @@ func TestValidateAndSetLogLevelGlobally(t *testing.T) { err := ValidateAndSetLogLevelGlobally(tt.level) require.Equal(t, tt.wantErr, errString(err)) - require.Equal(t, tt.wantLevel, getKlogLevel(t)) + require.Equal(t, tt.wantLevel, getKlogLevel()) + + if tt.wantEnabled != nil { + allLevels := []LogLevel{LevelWarning, LevelInfo, LevelDebug, LevelTrace, LevelAll} + for _, level := range allLevels { + if contains(tt.wantEnabled, level) { + require.Truef(t, Enabled(level), "wanted %q to be enabled", level) + } else { + require.False(t, Enabled(level), "did not want %q to be enabled", level) + } + } + } }) } - require.Equal(t, originalLogLevel, getKlogLevel(t)) + require.Equal(t, originalLogLevel, getKlogLevel()) } -func getKlogLevel(t *testing.T) klog.Level { - t.Helper() - - // hack around klog not exposing a Get method - for i := klog.Level(0); i < 256; i++ { - if klog.V(i).Enabled() { - continue +func contains(haystack []LogLevel, needle LogLevel) bool { + for _, hay := range haystack { + if hay == needle { + return true } - return i - 1 } - - t.Fatal("unknown log level") - return 0 + return false } func errString(err error) string {