139 lines
3.4 KiB
Go
139 lines
3.4 KiB
Go
package starlog
|
|
|
|
import "strings"
|
|
|
|
type KeywordPreset string
|
|
|
|
const (
|
|
KeywordPresetMobaLite KeywordPreset = "moba-lite"
|
|
KeywordPresetMobaFull KeywordPreset = "moba-full"
|
|
)
|
|
|
|
var keywordPresetDefs = map[KeywordPreset]map[string][]Attr{
|
|
KeywordPresetMobaLite: buildKeywordPresetMap(false),
|
|
KeywordPresetMobaFull: buildKeywordPresetMap(true),
|
|
}
|
|
|
|
func normalizeKeywordPreset(preset KeywordPreset) KeywordPreset {
|
|
name := strings.TrimSpace(strings.ToLower(string(preset)))
|
|
switch name {
|
|
case "moba-lite", "lite", "moba":
|
|
return KeywordPresetMobaLite
|
|
case "moba-full", "full", "mobaxterm":
|
|
return KeywordPresetMobaFull
|
|
default:
|
|
return KeywordPreset(name)
|
|
}
|
|
}
|
|
|
|
func keywordPresetMap(preset KeywordPreset) (map[string][]Attr, bool) {
|
|
normalized := normalizeKeywordPreset(preset)
|
|
mapping, ok := keywordPresetDefs[normalized]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return mapping, true
|
|
}
|
|
|
|
func GetKeywordPreset(preset KeywordPreset) map[string][]Attr {
|
|
mapping, ok := keywordPresetMap(preset)
|
|
if !ok {
|
|
return map[string][]Attr{}
|
|
}
|
|
return cloneColorMap(mapping)
|
|
}
|
|
|
|
func (logger *StarLogger) ApplyKeywordPreset(preset KeywordPreset) {
|
|
if logger == nil {
|
|
return
|
|
}
|
|
mapping, ok := keywordPresetMap(preset)
|
|
if !ok {
|
|
return
|
|
}
|
|
logger.SetKeywordColors(mapping)
|
|
}
|
|
|
|
func (logger *StarLogger) MergeKeywordPreset(preset KeywordPreset) {
|
|
if logger == nil {
|
|
return
|
|
}
|
|
mapping, ok := keywordPresetMap(preset)
|
|
if !ok {
|
|
return
|
|
}
|
|
logger.logcore.mu.Lock()
|
|
defer logger.logcore.mu.Unlock()
|
|
merged := cloneColorMap(mapping)
|
|
for keyword, attrs := range logger.logcore.keywordColors {
|
|
merged[keyword] = cloneAttrs(attrs)
|
|
}
|
|
logger.logcore.keywordColors = merged
|
|
logger.logcore.rebuildKeywordCachesLocked()
|
|
}
|
|
|
|
func buildKeywordPresetMap(full bool) map[string][]Attr {
|
|
colors := make(map[string][]Attr)
|
|
|
|
putKeywordGroup(colors, []string{
|
|
"error", "failed", "fatal", "panic", "critical", "timeout",
|
|
"denied", "refused", "invalid", "exception",
|
|
}, []Attr{FgHiRed, Bold})
|
|
putKeywordGroup(colors, []string{
|
|
"warn", "warning", "retry", "slow",
|
|
}, []Attr{FgHiYellow, Bold})
|
|
putKeywordGroup(colors, []string{
|
|
"ok", "success", "passed", "done", "connected", "ready",
|
|
}, []Attr{FgHiGreen, Bold})
|
|
putKeywordGroup(colors, []string{
|
|
"true", "yes", "enabled", "on",
|
|
}, []Attr{FgGreen, Bold})
|
|
putKeywordGroup(colors, []string{
|
|
"false", "no", "disabled", "off",
|
|
}, []Attr{FgHiMagenta, Bold})
|
|
putKeywordGroup(colors, []string{
|
|
"info", "notice", "debug",
|
|
}, []Attr{FgHiCyan})
|
|
|
|
if full {
|
|
putKeywordGroup(colors, []string{
|
|
"closed", "disconnect", "cancel", "drop", "overload",
|
|
}, []Attr{FgMagenta})
|
|
putKeywordGroup(colors, []string{
|
|
"start", "started", "ready", "healthy", "up",
|
|
}, []Attr{FgGreen})
|
|
putKeywordGroup(colors, []string{
|
|
"stop", "stopped", "down", "degraded",
|
|
}, []Attr{FgYellow})
|
|
}
|
|
|
|
return colors
|
|
}
|
|
|
|
func putKeywordGroup(colors map[string][]Attr, keywords []string, attrs []Attr) {
|
|
for _, keyword := range keywords {
|
|
for _, variant := range buildKeywordVariants(keyword) {
|
|
colors[variant] = cloneAttrs(attrs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func buildKeywordVariants(keyword string) []string {
|
|
keyword = strings.TrimSpace(keyword)
|
|
if keyword == "" {
|
|
return nil
|
|
}
|
|
lower := strings.ToLower(keyword)
|
|
upper := strings.ToUpper(keyword)
|
|
title := strings.ToUpper(lower[:1]) + lower[1:]
|
|
|
|
variants := []string{lower}
|
|
if upper != lower {
|
|
variants = append(variants, upper)
|
|
}
|
|
if title != lower && title != upper {
|
|
variants = append(variants, title)
|
|
}
|
|
return variants
|
|
}
|