-
Notifications
You must be signed in to change notification settings - Fork 771
Map generator -verbose and -performance flags #2721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tidwell
wants to merge
29
commits into
openfrontio:main
Choose a base branch
from
Tidwell:map-generator-verbose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+326
−40
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c72354f
Add -verbose and -performance flags to map generator
Tidwell 997861a
shuffle location of logger type and abstract logger creation fn
Tidwell ff7744f
rename fn arg so it does not shadow type name
Tidwell 05f931c
update nitpick comments in map generator logging
Tidwell dd4b69c
unexport and rename LogArgs to loggerArgs type
Tidwell 7ee6a8d
working custom slog formater
Tidwell 315de9e
abstract to slog logger and clean up log level logic
Tidwell 798fffc
clean up readme and comments
Tidwell da4c70b
update docs
Tidwell ecba038
switch to context-based passing for logger
Tidwell 60a193d
further isolate logger related logic
Tidwell 6866335
fix operator prcedence, group prefix, and 0-tile error in map-generat…
Tidwell 9f1541e
Merge branch 'main' into map-generator-verbose
Tidwell a9ed7a0
fix comment
Tidwell 8cd4526
fix const block
Tidwell 75a3645
ensure logger calls with tags use Debug
Tidwell 02556b4
Merge branch 'main' into map-generator-verbose
Tidwell d995069
Merge branch 'main' into map-generator-verbose
Tidwell 2c53f51
Merge branch 'main' into map-generator-verbose
Tidwell fc75d37
Merge branch 'main' into map-generator-verbose
Tidwell 239da1d
Merge branch 'main' into map-generator-verbose
Tidwell d9292ac
Update map-generator/main.go
Tidwell 27d8a00
fix coderabbits bad commit
Tidwell 2fc2dfc
Merge branch 'main' into map-generator-verbose
Tidwell 05f3132
Merge branch 'main' into map-generator-verbose
Tidwell 844391b
Merge branch 'main' into map-generator-verbose
Tidwell 74cb9e3
Merge branch 'main' into map-generator-verbose
Tidwell e4ae4c0
Merge branch 'main' into map-generator-verbose
Tidwell fcbeb96
Merge branch 'main' into map-generator-verbose
Tidwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| // This is the custom logger providing the multi-level and flag-based logging for | ||
| // the map-generator. It uses slog. | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
| type LogFlags struct { | ||
| logLevel string // The log-level (most -> least wordy): ALL, DEBUG, INFO (default), WARN, ERROR | ||
| verbose bool // sets log-level=DEBUG | ||
| performance bool // opts-in to performance checks and sets log-level=DEBUG | ||
| removal bool // opts-in to island/lake removal logging and sets log-level=DEBUG | ||
| } | ||
|
|
||
| // LevelAll is a custom log Level that outputs all messages, regardless of other passed flags | ||
| const LevelAll = slog.Level(-8) | ||
|
|
||
| // PerformanceLogTag is a slog attribute used to tag performance-related log messages. | ||
| var PerformanceLogTag = slog.String("tag", "performance") | ||
|
|
||
| // RemovalLogTag is a slog attribute used to tag land/water removal-related log messages. | ||
| var RemovalLogTag = slog.String("tag", "removal") | ||
|
|
||
| // DetermineLogLevel determines the log level based on the LogFlags | ||
| // It prioritizes the log level flag over the default, and switches to debug if performance or removal flags are set. | ||
| func DetermineLogLevel( | ||
| logFlags LogFlags) slog.Level { | ||
|
|
||
| var level = slog.LevelInfo | ||
| if logFlags.verbose { | ||
| level = slog.LevelDebug | ||
| } | ||
|
|
||
| // switch to debug if any of the optional flags is enabled | ||
| if logFlags.performance || logFlags.removal { | ||
| level = slog.LevelDebug | ||
| } | ||
|
|
||
| // parse the log-level input string to the slog.Level type | ||
| if logFlags.logLevel != "" { | ||
| switch strings.ToLower(logFlags.logLevel) { | ||
| case "all": | ||
| level = LevelAll | ||
| case "debug": | ||
| level = slog.LevelDebug | ||
| case "info": | ||
| level = slog.LevelInfo | ||
| case "warn": | ||
| level = slog.LevelWarn | ||
| case "error": | ||
| level = slog.LevelError | ||
| default: | ||
| fmt.Printf("invalid log level: %s, defaulting to info\n", logFlags.logLevel) | ||
| level = slog.LevelInfo | ||
| } | ||
| } | ||
| return level | ||
| } | ||
|
|
||
| // GeneratorLogger is a custom slog.Handler that outputs logs based on log level and additional LogFlags. | ||
| type GeneratorLogger struct { | ||
| opts slog.HandlerOptions | ||
| w io.Writer | ||
| mu *sync.Mutex | ||
| attrs []slog.Attr | ||
| prefix string | ||
| flags LogFlags | ||
| } | ||
|
|
||
| // NewGeneratorLogger creates a new GeneratorLogger. | ||
| // It initializes a handler with specific output, options, and flags | ||
| func NewGeneratorLogger( | ||
| out io.Writer, | ||
| opts *slog.HandlerOptions, | ||
| flags LogFlags) *GeneratorLogger { | ||
|
|
||
| h := &GeneratorLogger{ | ||
| w: out, | ||
| mu: &sync.Mutex{}, | ||
| flags: flags, | ||
| } | ||
| if opts != nil { | ||
| h.opts = *opts | ||
| } | ||
| if h.opts.Level == nil { | ||
| h.opts.Level = slog.LevelInfo | ||
| } | ||
| return h | ||
| } | ||
|
|
||
| // Enabled checks if a given log level is enabled for this handler. | ||
| func (h *GeneratorLogger) Enabled(_ context.Context, level slog.Level) bool { | ||
| return level >= h.opts.Level.Level() | ||
| } | ||
|
|
||
| // Handle processes a log record. | ||
| // It decides whether to output each record based on log level, flags, and if the map is a test map | ||
| // On output, it formats the log message with any extra formatting | ||
| func (h *GeneratorLogger) Handle(_ context.Context, r slog.Record) error { | ||
| isPerformanceLog := false | ||
| isRemovalLog := false | ||
| isTestMap := false | ||
|
|
||
| var mapName string | ||
|
|
||
| findAttrs := func(a slog.Attr) { | ||
| if a.Equal(PerformanceLogTag) { | ||
| isPerformanceLog = true | ||
| } | ||
| if a.Equal(RemovalLogTag) { | ||
| isRemovalLog = true | ||
| } | ||
| if a.Key == "map" { | ||
| mapName = a.Value.String() | ||
| } | ||
| if a.Key == "isTest" { | ||
| isTestMap = a.Value.Bool() | ||
| } | ||
| } | ||
|
|
||
| // Check record attributes for performance tag and map name | ||
| r.Attrs(func(a slog.Attr) bool { | ||
| findAttrs(a) | ||
| return true | ||
| }) | ||
|
|
||
| // Check handler's own attributes for performance tag and map name | ||
| for _, a := range h.attrs { | ||
| findAttrs(a) | ||
| } | ||
|
|
||
| // Don't log messages if the flags are not set | ||
| // If the log level is set to LevelAll, disregard | ||
| if h.opts.Level != LevelAll && isPerformanceLog && !h.flags.performance { | ||
| return nil | ||
| } | ||
| if h.opts.Level != LevelAll && (isRemovalLog && !h.flags.removal) { | ||
| return nil | ||
| } | ||
|
|
||
| // dont log performance messages for test maps | ||
| if isPerformanceLog && isTestMap { | ||
| return nil | ||
| } | ||
|
|
||
| buf := &bytes.Buffer{} | ||
|
|
||
| // Add map name as a prefix in log Level DEBUG and ALL | ||
| if (h.opts.Level == slog.LevelDebug || h.opts.Level == LevelAll) && mapName != "" { | ||
| mapName = strings.Trim(mapName, `"`) | ||
| fmt.Fprintf(buf, "[%s] ", mapName) | ||
| } | ||
|
|
||
| // Add prefix for performance messages | ||
| if isPerformanceLog { | ||
| fmt.Fprintf(buf, "[PERF] ") | ||
| } | ||
|
|
||
| if h.prefix != "" { | ||
| fmt.Fprintf(buf, "%s ", h.prefix) | ||
| } | ||
|
|
||
| fmt.Fprintln(buf, r.Message) | ||
|
|
||
| h.mu.Lock() | ||
| defer h.mu.Unlock() | ||
| _, err := h.w.Write(buf.Bytes()) | ||
| return err | ||
| } | ||
|
|
||
| // WithAttrs returns a new handler with the given attributes added. | ||
| func (h *GeneratorLogger) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
| newHandler := *h | ||
| newHandler.attrs = append(newHandler.attrs, attrs...) | ||
| return &newHandler | ||
| } | ||
|
|
||
| // WithGroup returns a new handler with the given group name. | ||
| // The group name is added as a prefix to subsequent log messages. | ||
| func (h *GeneratorLogger) WithGroup(name string) slog.Handler { | ||
| if name == "" { | ||
| return h | ||
| } | ||
| newHandler := *h | ||
| if newHandler.prefix != "" { | ||
| newHandler.prefix += "." | ||
| } | ||
| newHandler.prefix += name | ||
| return &newHandler | ||
| } | ||
|
|
||
| type loggerKey struct{} | ||
|
|
||
| // LoggerFromContext retrieves the logger from the context. | ||
| // If no logger is found, it returns the default logger. | ||
| func LoggerFromContext(ctx context.Context) *slog.Logger { | ||
| if logger, ok := ctx.Value(loggerKey{}).(*slog.Logger); ok { | ||
| return logger | ||
| } | ||
| return slog.Default() | ||
| } | ||
|
|
||
| // ContextWithLogger returns a new context with the provided logger. | ||
| func ContextWithLogger(ctx context.Context, logger *slog.Logger) context.Context { | ||
| return context.WithValue(ctx, loggerKey{}, logger) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.