From e68b9d00a6e05b3a941f63ffb696f91e554ac5ec Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 18 Oct 2024 20:33:49 +0200 Subject: Adding upstream version 9.0.3. Signed-off-by: Daniel Baumann --- services/f3/util/logger.go | 97 +++++++++++++++++++++++++++++++++++++++++ services/f3/util/logger_test.go | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 services/f3/util/logger.go create mode 100644 services/f3/util/logger_test.go (limited to 'services/f3/util') diff --git a/services/f3/util/logger.go b/services/f3/util/logger.go new file mode 100644 index 0000000..21d8d6b --- /dev/null +++ b/services/f3/util/logger.go @@ -0,0 +1,97 @@ +// Copyright Earl Warren +// SPDX-License-Identifier: MIT + +package util + +import ( + "fmt" + + forgejo_log "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/migration" + + "code.forgejo.org/f3/gof3/v3/logger" +) + +type f3Logger struct { + m migration.Messenger + l forgejo_log.Logger +} + +func (o *f3Logger) Message(message string, args ...any) { + if o.m != nil { + o.m(message, args...) + } +} + +func (o *f3Logger) SetLevel(level logger.Level) { +} + +func forgejoLevelToF3Level(level forgejo_log.Level) logger.Level { + switch level { + case forgejo_log.TRACE: + return logger.Trace + case forgejo_log.DEBUG: + return logger.Debug + case forgejo_log.INFO: + return logger.Info + case forgejo_log.WARN: + return logger.Warn + case forgejo_log.ERROR: + return logger.Error + case forgejo_log.FATAL: + return logger.Fatal + default: + panic(fmt.Errorf("unexpected level %d", level)) + } +} + +func f3LevelToForgejoLevel(level logger.Level) forgejo_log.Level { + switch level { + case logger.Trace: + return forgejo_log.TRACE + case logger.Debug: + return forgejo_log.DEBUG + case logger.Info: + return forgejo_log.INFO + case logger.Warn: + return forgejo_log.WARN + case logger.Error: + return forgejo_log.ERROR + case logger.Fatal: + return forgejo_log.FATAL + default: + panic(fmt.Errorf("unexpected level %d", level)) + } +} + +func (o *f3Logger) GetLevel() logger.Level { + return forgejoLevelToF3Level(o.l.GetLevel()) +} + +func (o *f3Logger) Log(skip int, level logger.Level, format string, args ...any) { + o.l.Log(skip+1, f3LevelToForgejoLevel(level), format, args...) +} + +func (o *f3Logger) Trace(message string, args ...any) { + o.l.Log(1, forgejo_log.TRACE, message, args...) +} + +func (o *f3Logger) Debug(message string, args ...any) { + o.l.Log(1, forgejo_log.DEBUG, message, args...) +} +func (o *f3Logger) Info(message string, args ...any) { o.l.Log(1, forgejo_log.INFO, message, args...) } +func (o *f3Logger) Warn(message string, args ...any) { o.l.Log(1, forgejo_log.WARN, message, args...) } +func (o *f3Logger) Error(message string, args ...any) { + o.l.Log(1, forgejo_log.ERROR, message, args...) +} + +func (o *f3Logger) Fatal(message string, args ...any) { + o.l.Log(1, forgejo_log.FATAL, message, args...) +} + +func NewF3Logger(messenger migration.Messenger, logger forgejo_log.Logger) logger.Interface { + return &f3Logger{ + m: messenger, + l: logger, + } +} diff --git a/services/f3/util/logger_test.go b/services/f3/util/logger_test.go new file mode 100644 index 0000000..db880aa --- /dev/null +++ b/services/f3/util/logger_test.go @@ -0,0 +1,89 @@ +// Copyright Earl Warren +// SPDX-License-Identifier: MIT + +package util + +import ( + "fmt" + "testing" + "time" + + forgejo_log "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/test" + + "code.forgejo.org/f3/gof3/v3/logger" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestF3UtilMessage(t *testing.T) { + expected := "EXPECTED MESSAGE" + var actual string + logger := NewF3Logger(func(message string, args ...any) { + actual = fmt.Sprintf(message, args...) + }, nil) + logger.Message("EXPECTED %s", "MESSAGE") + assert.EqualValues(t, expected, actual) +} + +func TestF3UtilLogger(t *testing.T) { + for _, testCase := range []struct { + level logger.Level + call func(logger.MessageInterface, string, ...any) + }{ + {level: logger.Trace, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Trace(message, args...) }}, + {level: logger.Debug, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Debug(message, args...) }}, + {level: logger.Info, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Info(message, args...) }}, + {level: logger.Warn, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Warn(message, args...) }}, + {level: logger.Error, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Error(message, args...) }}, + {level: logger.Fatal, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Fatal(message, args...) }}, + } { + t.Run(testCase.level.String(), func(t *testing.T) { + testLoggerCase(t, testCase.level, testCase.call) + }) + } +} + +func testLoggerCase(t *testing.T, level logger.Level, loggerFunc func(logger.MessageInterface, string, ...any)) { + lc, cleanup := test.NewLogChecker(forgejo_log.DEFAULT, f3LevelToForgejoLevel(level)) + defer cleanup() + stopMark := "STOP" + lc.StopMark(stopMark) + filtered := []string{ + "MESSAGE HERE", + } + moreVerbose := logger.MoreVerbose(level) + if moreVerbose != nil { + filtered = append(filtered, "MESSAGE MORE VERBOSE") + } + lessVerbose := logger.LessVerbose(level) + if lessVerbose != nil { + filtered = append(filtered, "MESSAGE LESS VERBOSE") + } + lc.Filter(filtered...) + + logger := NewF3Logger(nil, forgejo_log.GetLogger(forgejo_log.DEFAULT)) + loggerFunc(logger, "MESSAGE %s", "HERE") + if moreVerbose != nil { + logger.Log(1, *moreVerbose, "MESSAGE %s", "MORE VERBOSE") + } + if lessVerbose != nil { + logger.Log(1, *lessVerbose, "MESSAGE %s", "LESS VERBOSE") + } + logger.Fatal(stopMark) + + logFiltered, logStopped := lc.Check(5 * time.Second) + assert.True(t, logStopped) + i := 0 + assert.True(t, logFiltered[i], filtered[i]) + if moreVerbose != nil { + i++ + require.Greater(t, len(logFiltered), i) + assert.False(t, logFiltered[i], filtered[i]) + } + if lessVerbose != nil { + i++ + require.Greater(t, len(logFiltered), i) + assert.True(t, logFiltered[i], filtered[i]) + } +} -- cgit v1.2.3