Skip to content
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

CC-1181: Add secondary prefix to logger #35

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,52 @@ type Logger struct {
// IsQuiet is used to determine whether to emit non-critical logs.
IsQuiet bool

// prefix is the prefix to be used for all logs.
prefix string

// secondaryPrefix is a secondary prefix, that can be dynamically appended to the prefix.
secondaryPrefix string

logger log.Logger
}

// GetLogger Returns a logger.
func GetLogger(isDebug bool, prefix string) *Logger {
color.NoColor = false

prefix = yellowColorize(prefix)[0]
coloredPrefix := yellowColorize(prefix)[0]
return &Logger{
logger: *log.New(os.Stdout, prefix, 0),
logger: *log.New(os.Stdout, coloredPrefix, 0),
IsDebug: isDebug,
prefix: prefix,
}
}

func (l *Logger) UpdateSecondaryPrefix(prefix string) {
l.secondaryPrefix = prefix
if prefix == "" {
// Reset the prefix to the original one.
l.logger.SetPrefix(yellowColorize(l.prefix)[0])
} else {
// Append the secondary prefix to the original one.
l.logger.SetPrefix(yellowColorize(l.prefix + fmt.Sprintf("[%s] ", prefix))[0])
}
}

func (l *Logger) ResetSecondaryPrefix() {
l.UpdateSecondaryPrefix("")
}

// GetQuietLogger Returns a logger that only emits critical logs. Useful for anti-cheat stages.
func GetQuietLogger(prefix string) *Logger {
color.NoColor = false

prefix = yellowColorize(prefix)[0]
coloredPrefix := yellowColorize(prefix)[0]
return &Logger{
logger: *log.New(os.Stdout, prefix, 0),
logger: *log.New(os.Stdout, coloredPrefix, 0),
IsDebug: false,
IsQuiet: true,
prefix: prefix,
}
}

Expand Down
Loading