diff options
Diffstat (limited to 'modules/setting')
-rw-r--r-- | modules/setting/log.go | 68 | ||||
-rw-r--r-- | modules/setting/setting.go | 1 |
2 files changed, 66 insertions, 3 deletions
diff --git a/modules/setting/log.go b/modules/setting/log.go index 5ffb2479dd..35bf021ac2 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -12,6 +12,7 @@ import ( "path" "path/filepath" "strings" + "sync" "code.gitea.io/gitea/modules/log" @@ -20,6 +21,69 @@ import ( var filenameSuffix = "" +var descriptionLock = sync.RWMutex{} +var logDescriptions = make(map[string]*LogDescription) + +// GetLogDescriptions returns a race safe set of descriptions +func GetLogDescriptions() map[string]*LogDescription { + descriptionLock.RLock() + defer descriptionLock.RUnlock() + descs := make(map[string]*LogDescription, len(logDescriptions)) + for k, v := range logDescriptions { + subLogDescriptions := make([]SubLogDescription, len(v.SubLogDescriptions)) + for i, s := range v.SubLogDescriptions { + subLogDescriptions[i] = s + } + descs[k] = &LogDescription{ + Name: v.Name, + SubLogDescriptions: subLogDescriptions, + } + } + return descs +} + +// AddLogDescription adds a set of descriptions to the complete description +func AddLogDescription(key string, description *LogDescription) { + descriptionLock.Lock() + defer descriptionLock.Unlock() + logDescriptions[key] = description +} + +// AddSubLogDescription adds a sub log description +func AddSubLogDescription(key string, subLogDescription SubLogDescription) bool { + descriptionLock.Lock() + defer descriptionLock.Unlock() + desc, ok := logDescriptions[key] + if !ok { + return false + } + for i, sub := range desc.SubLogDescriptions { + if sub.Name == subLogDescription.Name { + desc.SubLogDescriptions[i] = subLogDescription + return true + } + } + desc.SubLogDescriptions = append(desc.SubLogDescriptions, subLogDescription) + return true +} + +// RemoveSubLogDescription removes a sub log description +func RemoveSubLogDescription(key string, name string) bool { + descriptionLock.Lock() + defer descriptionLock.Unlock() + desc, ok := logDescriptions[key] + if !ok { + return false + } + for i, sub := range desc.SubLogDescriptions { + if sub.Name == name { + desc.SubLogDescriptions = append(desc.SubLogDescriptions[:i], desc.SubLogDescriptions[i+1:]...) + return true + } + } + return false +} + type defaultLogOptions struct { levelName string // LogLevel flags string @@ -185,7 +249,7 @@ func generateNamedLogger(key string, options defaultLogOptions) *LogDescription log.Info("%s Log: %s(%s:%s)", strings.Title(key), strings.Title(name), provider, levelName) } - LogDescriptions[key] = &description + AddLogDescription(key, &description) return &description } @@ -279,7 +343,7 @@ func newLogService() { log.Info("Gitea Log Mode: %s(%s:%s)", strings.Title(name), strings.Title(provider), levelName) } - LogDescriptions[log.DEFAULT] = &description + AddLogDescription(log.DEFAULT, &description) // Finally redirect the default golog to here golog.SetFlags(0) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 8efc832f9d..4c2fba8048 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -289,7 +289,6 @@ var ( LogLevel string StacktraceLogLevel string LogRootPath string - LogDescriptions = make(map[string]*LogDescription) RedirectMacaronLog bool DisableRouterLog bool RouterLogLevel log.Level |