From 94649539243698a3b22ab7a9029d30a25acc13cd Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Tue, 6 Jun 2023 21:42:56 +0200 Subject: [PATCH] Use logger.Interface when resolving auto mode Signed-off-by: Evan Lezar --- cmd/nvidia-container-runtime-hook/main.go | 9 ++++---- internal/info/auto.go | 14 +++++------- internal/logger/lib.go | 26 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/cmd/nvidia-container-runtime-hook/main.go b/cmd/nvidia-container-runtime-hook/main.go index ee8a0397..9e5d4346 100644 --- a/cmd/nvidia-container-runtime-hook/main.go +++ b/cmd/nvidia-container-runtime-hook/main.go @@ -14,6 +14,7 @@ import ( "syscall" "github.com/NVIDIA/nvidia-container-toolkit/internal/info" + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" ) @@ -188,11 +189,11 @@ func main() { } } -// logInterceptor implements the info.Logger interface to allow for logging from this function. -type logInterceptor struct{} +// logInterceptor implements the logger.Interface to allow for logging from executable. +type logInterceptor struct { + logger.NullLogger +} func (l *logInterceptor) Infof(format string, args ...interface{}) { log.Printf(format, args...) } - -func (l *logInterceptor) Debugf(format string, args ...interface{}) {} diff --git a/internal/info/auto.go b/internal/info/auto.go index c7863b97..7ec8bb86 100644 --- a/internal/info/auto.go +++ b/internal/info/auto.go @@ -16,17 +16,13 @@ package info -import "gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/info" - -// Logger is a basic interface for logging to allow these functions to be called -// from code where logrus is not used. -type Logger interface { - Infof(string, ...interface{}) - Debugf(string, ...interface{}) -} +import ( + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" + "gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/info" +) // ResolveAutoMode determines the correct mode for the platform if set to "auto" -func ResolveAutoMode(logger Logger, mode string) (rmode string) { +func ResolveAutoMode(logger logger.Interface, mode string) (rmode string) { if mode != "auto" { return mode } diff --git a/internal/logger/lib.go b/internal/logger/lib.go index f9e46b64..075c6ea3 100644 --- a/internal/logger/lib.go +++ b/internal/logger/lib.go @@ -22,3 +22,29 @@ import "github.com/sirupsen/logrus" func New() Interface { return logrus.StandardLogger() } + +// NullLogger is a logger that does nothing +type NullLogger struct{} + +var _ Interface = (*NullLogger)(nil) + +// Debugf is a no-op for the null logger +func (l *NullLogger) Debugf(string, ...interface{}) {} + +// Errorf is a no-op for the null logger +func (l *NullLogger) Errorf(string, ...interface{}) {} + +// Info is a no-op for the null logger +func (l *NullLogger) Info(...interface{}) {} + +// Infof is a no-op for the null logger +func (l *NullLogger) Infof(string, ...interface{}) {} + +// Warn is a no-op for the null logger +func (l *NullLogger) Warn(...interface{}) {} + +// Warnf is a no-op for the null logger +func (l *NullLogger) Warnf(string, ...interface{}) {} + +// Warningf is a no-op for the null logger +func (l *NullLogger) Warningf(string, ...interface{}) {}