From 91685e5b5732064b04293ddb75ccfddfaef91a33 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Tue, 29 Oct 2024 16:07:57 -0600 Subject: [PATCH] internal/witcli, wit/logging: add basic Logger type --- internal/witcli/logger.go | 18 ++++++++ wit/logging/logging.go | 91 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 internal/witcli/logger.go create mode 100644 wit/logging/logging.go diff --git a/internal/witcli/logger.go b/internal/witcli/logger.go new file mode 100644 index 00000000..ce5bf15e --- /dev/null +++ b/internal/witcli/logger.go @@ -0,0 +1,18 @@ +package witcli + +import ( + "os" + + "github.com/bytecodealliance/wasm-tools-go/wit/logging" +) + +// Logger returns a [logging.Logger] that outputs to stdout. +func Logger(verbose, debug bool) logging.Logger { + level := logging.LevelWarn + if debug { + level = logging.LevelDebug + } else if verbose { + level = logging.LevelInfo + } + return logging.NewLogger(os.Stderr, level) +} diff --git a/wit/logging/logging.go b/wit/logging/logging.go new file mode 100644 index 00000000..6ae4857e --- /dev/null +++ b/wit/logging/logging.go @@ -0,0 +1,91 @@ +package logging + +import ( + "io" + "log" + "math" +) + +// Level represents a logging level, identical to [slog.Level]. +type Level int + +const ( + LevelDebug Level = -4 + LevelInfo Level = 0 + LevelWarn Level = 4 + LevelError Level = 8 + LevelNever Level = math.MaxInt +) + +// Logger represents a simple logging interface. +type Logger interface { + // Level returns the current logging level for this Logger. + Level() Level + + // Logf logs a message with a logging level. + Logf(level Level, format string, v ...any) + + // Debugf logs a message with level debug. + Debugf(format string, v ...any) + + // Infof logs a message with level info. + Infof(format string, v ...any) + + // Printf is an alias for Infof. + Printf(format string, v ...any) + + // Warnf logs a message with level warn. + Warnf(format string, v ...any) + + // Errorf logs a message with level error. + Errorf(format string, v ...any) +} + +// DiscardLogger returns a logger that discards all output. +func DiscardLogger() Logger { + return &logger{level: LevelNever} +} + +// NewLogger returns a new leveled logger that writes to out. +func NewLogger(out io.Writer, level Level) Logger { + return &logger{ + level: level, + logger: log.New(out, "", 0), + } +} + +type logger struct { + level Level + logger *log.Logger +} + +func (l *logger) Level() Level { + return l.level +} + +func (l *logger) Logf(level Level, format string, v ...any) { + if l.level > level || l.logger == nil { + return + } + l.logger.Printf(format, v...) +} + +func (l *logger) Debugf(format string, v ...any) { + l.Logf(LevelDebug, format, v...) +} + +func (l *logger) Infof(format string, v ...any) { + l.Logf(LevelInfo, format, v...) +} + +func (l *logger) Printf(format string, v ...any) { + l.Logf(LevelInfo, format, v...) +} + +func (l *logger) Warnf(format string, v ...any) { + l.Logf(LevelWarn, format, v...) +} + +func (l *logger) Errorf(format string, v ...any) { + l.Logf(LevelError, format, v...) +}