From 7ed119d38065ad09b5dde2d706b2597891707761 Mon Sep 17 00:00:00 2001 From: ucwong Date: Fri, 21 Jul 2023 20:00:23 +0800 Subject: [PATCH 1/4] log: atomic types used in log --- log/format.go | 14 +++++++------- log/handler_glog.go | 18 +++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/log/format.go b/log/format.go index fb476b73e322..bbbd08c52008 100644 --- a/log/format.go +++ b/log/format.go @@ -33,19 +33,19 @@ var locationTrims = []string{ // format output. func PrintOrigins(print bool) { if print { - atomic.StoreUint32(&locationEnabled, 1) + locationEnabled.Store(1) } else { - atomic.StoreUint32(&locationEnabled, 0) + locationEnabled.Store(0) } } // locationEnabled is an atomic flag controlling whether the terminal formatter // should append the log locations too when printing entries. -var locationEnabled uint32 +var locationEnabled atomic.Uint32 // locationLength is the maxmimum path length encountered, which all logs are // padded to to aid in alignment. -var locationLength uint32 +var locationLength atomic.Uint32 // fieldPadding is a global map with maximum field value lengths seen until now // to allow padding log contexts in a bit smarter way. @@ -109,17 +109,17 @@ func TerminalFormat(usecolor bool) Format { b := &bytes.Buffer{} lvl := r.Lvl.AlignedString() - if atomic.LoadUint32(&locationEnabled) != 0 { + if locationEnabled.Load() != 0 { // Log origin printing was requested, format the location path and line number location := fmt.Sprintf("%+v", r.Call) for _, prefix := range locationTrims { location = strings.TrimPrefix(location, prefix) } // Maintain the maximum location length for fancyer alignment - align := int(atomic.LoadUint32(&locationLength)) + align := int(locationLength.Load()) if align < len(location) { align = len(location) - atomic.StoreUint32(&locationLength, uint32(align)) + locationLength.Store(uint32(align)) } padding := strings.Repeat(" ", align-len(location)) diff --git a/log/handler_glog.go b/log/handler_glog.go index b5186d4b27ec..4e7b54d08b21 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -39,9 +39,9 @@ var errTraceSyntax = errors.New("expect file.go:234") type GlogHandler struct { origin Handler // The origin handler this wraps - level uint32 // Current log level, atomically accessible - override uint32 // Flag whether overrides are used, atomically accessible - backtrace uint32 // Flag whether backtrace location is set + level atomic.Uint32 // Current log level, atomically accessible + override atomic.Uint32 // Flag whether overrides are used, atomically accessible + backtrace atomic.Uint32 // Flag whether backtrace location is set patterns []pattern // Current list of patterns to override with siteCache map[uintptr]Lvl // Cache of callsite pattern evaluations @@ -72,7 +72,7 @@ type pattern struct { // Verbosity sets the glog verbosity ceiling. The verbosity of individual packages // and source files can be raised using Vmodule. func (h *GlogHandler) Verbosity(level Lvl) { - atomic.StoreUint32(&h.level, uint32(level)) + h.level.Store(uint32(level)) } // Vmodule sets the glog verbosity pattern. @@ -138,7 +138,7 @@ func (h *GlogHandler) Vmodule(ruleset string) error { h.patterns = filter h.siteCache = make(map[uintptr]Lvl) - atomic.StoreUint32(&h.override, uint32(len(filter))) + h.override.Store(uint32(len(filter))) return nil } @@ -171,7 +171,7 @@ func (h *GlogHandler) BacktraceAt(location string) error { defer h.lock.Unlock() h.location = location - atomic.StoreUint32(&h.backtrace, uint32(len(location))) + h.backtrace.Store(uint32(len(location))) return nil } @@ -180,7 +180,7 @@ func (h *GlogHandler) BacktraceAt(location string) error { // and backtrace filters, finally emitting it if either allow it through. func (h *GlogHandler) Log(r *Record) error { // If backtracing is requested, check whether this is the callsite - if atomic.LoadUint32(&h.backtrace) > 0 { + if h.backtrace.Load() > 0 { // Everything below here is slow. Although we could cache the call sites the // same way as for vmodule, backtracing is so rare it's not worth the extra // complexity. @@ -198,11 +198,11 @@ func (h *GlogHandler) Log(r *Record) error { } } // If the global log level allows, fast track logging - if atomic.LoadUint32(&h.level) >= uint32(r.Lvl) { + if h.level.Load() >= uint32(r.Lvl) { return h.origin.Log(r) } // If no local overrides are present, fast track skipping - if atomic.LoadUint32(&h.override) == 0 { + if h.override.Load() == 0 { return nil } // Check callsite cache for previously calculated log levels From 0d9e6fd2118b91794d5b584dfe075c85eda31a42 Mon Sep 17 00:00:00 2001 From: ucwong Date: Tue, 1 Aug 2023 21:20:06 +0800 Subject: [PATCH 2/4] use atomic.Bool --- log/format.go | 8 ++++---- log/handler_glog.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/log/format.go b/log/format.go index bbbd08c52008..529788f05017 100644 --- a/log/format.go +++ b/log/format.go @@ -33,15 +33,15 @@ var locationTrims = []string{ // format output. func PrintOrigins(print bool) { if print { - locationEnabled.Store(1) + locationEnabled.Store(true) } else { - locationEnabled.Store(0) + locationEnabled.Store(false) } } // locationEnabled is an atomic flag controlling whether the terminal formatter // should append the log locations too when printing entries. -var locationEnabled atomic.Uint32 +var locationEnabled atomic.Bool // locationLength is the maxmimum path length encountered, which all logs are // padded to to aid in alignment. @@ -109,7 +109,7 @@ func TerminalFormat(usecolor bool) Format { b := &bytes.Buffer{} lvl := r.Lvl.AlignedString() - if locationEnabled.Load() != 0 { + if locationEnabled.Load() { // Log origin printing was requested, format the location path and line number location := fmt.Sprintf("%+v", r.Call) for _, prefix := range locationTrims { diff --git a/log/handler_glog.go b/log/handler_glog.go index 4e7b54d08b21..2a66679fb177 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -40,7 +40,7 @@ type GlogHandler struct { origin Handler // The origin handler this wraps level atomic.Uint32 // Current log level, atomically accessible - override atomic.Uint32 // Flag whether overrides are used, atomically accessible + override atomic.Bool // Flag whether overrides are used, atomically accessible backtrace atomic.Uint32 // Flag whether backtrace location is set patterns []pattern // Current list of patterns to override with @@ -138,7 +138,7 @@ func (h *GlogHandler) Vmodule(ruleset string) error { h.patterns = filter h.siteCache = make(map[uintptr]Lvl) - h.override.Store(uint32(len(filter))) + h.override.Store(len(filter) != 0) return nil } @@ -202,7 +202,7 @@ func (h *GlogHandler) Log(r *Record) error { return h.origin.Log(r) } // If no local overrides are present, fast track skipping - if h.override.Load() == 0 { + if h.override.Load() { return nil } // Check callsite cache for previously calculated log levels From 5c55deb1300c38f315f1c45daeb4f21b005129a8 Mon Sep 17 00:00:00 2001 From: ucwong Date: Tue, 1 Aug 2023 21:36:55 +0800 Subject: [PATCH 3/4] atomic.Bool use --- log/handler_glog.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/log/handler_glog.go b/log/handler_glog.go index 2a66679fb177..6db5f1a4c9ba 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -41,7 +41,7 @@ type GlogHandler struct { level atomic.Uint32 // Current log level, atomically accessible override atomic.Bool // Flag whether overrides are used, atomically accessible - backtrace atomic.Uint32 // Flag whether backtrace location is set + backtrace atomic.Bool // Flag whether backtrace location is set patterns []pattern // Current list of patterns to override with siteCache map[uintptr]Lvl // Cache of callsite pattern evaluations @@ -171,7 +171,7 @@ func (h *GlogHandler) BacktraceAt(location string) error { defer h.lock.Unlock() h.location = location - h.backtrace.Store(uint32(len(location))) + h.backtrace.Store(len(location) > 0) return nil } @@ -180,7 +180,7 @@ func (h *GlogHandler) BacktraceAt(location string) error { // and backtrace filters, finally emitting it if either allow it through. func (h *GlogHandler) Log(r *Record) error { // If backtracing is requested, check whether this is the callsite - if h.backtrace.Load() > 0 { + if h.backtrace.Load() { // Everything below here is slow. Although we could cache the call sites the // same way as for vmodule, backtracing is so rare it's not worth the extra // complexity. @@ -202,7 +202,7 @@ func (h *GlogHandler) Log(r *Record) error { return h.origin.Log(r) } // If no local overrides are present, fast track skipping - if h.override.Load() { + if !h.override.Load() { return nil } // Check callsite cache for previously calculated log levels From 5682693bf67e4f879a0dd8ce685b3704e46ef2a6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 5 Aug 2023 00:14:57 +0200 Subject: [PATCH 4/4] Update format.go --- log/format.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/log/format.go b/log/format.go index 529788f05017..6a03013b816c 100644 --- a/log/format.go +++ b/log/format.go @@ -32,11 +32,7 @@ var locationTrims = []string{ // PrintOrigins sets or unsets log location (file:line) printing for terminal // format output. func PrintOrigins(print bool) { - if print { - locationEnabled.Store(true) - } else { - locationEnabled.Store(false) - } + locationEnabled.Store(print) } // locationEnabled is an atomic flag controlling whether the terminal formatter