Skip to content

Commit 5c30541

Browse files
ucwongfjl
andauthored
log: use atomic types (#27763)
Co-authored-by: Felix Lange <[email protected]>
1 parent bb148dd commit 5c30541

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

log/format.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ var locationTrims = []string{
3232
// PrintOrigins sets or unsets log location (file:line) printing for terminal
3333
// format output.
3434
func PrintOrigins(print bool) {
35-
if print {
36-
atomic.StoreUint32(&locationEnabled, 1)
37-
} else {
38-
atomic.StoreUint32(&locationEnabled, 0)
39-
}
35+
locationEnabled.Store(print)
4036
}
4137

4238
// locationEnabled is an atomic flag controlling whether the terminal formatter
4339
// should append the log locations too when printing entries.
44-
var locationEnabled uint32
40+
var locationEnabled atomic.Bool
4541

4642
// locationLength is the maxmimum path length encountered, which all logs are
4743
// padded to to aid in alignment.
48-
var locationLength uint32
44+
var locationLength atomic.Uint32
4945

5046
// fieldPadding is a global map with maximum field value lengths seen until now
5147
// to allow padding log contexts in a bit smarter way.
@@ -109,17 +105,17 @@ func TerminalFormat(usecolor bool) Format {
109105

110106
b := &bytes.Buffer{}
111107
lvl := r.Lvl.AlignedString()
112-
if atomic.LoadUint32(&locationEnabled) != 0 {
108+
if locationEnabled.Load() {
113109
// Log origin printing was requested, format the location path and line number
114110
location := fmt.Sprintf("%+v", r.Call)
115111
for _, prefix := range locationTrims {
116112
location = strings.TrimPrefix(location, prefix)
117113
}
118114
// Maintain the maximum location length for fancyer alignment
119-
align := int(atomic.LoadUint32(&locationLength))
115+
align := int(locationLength.Load())
120116
if align < len(location) {
121117
align = len(location)
122-
atomic.StoreUint32(&locationLength, uint32(align))
118+
locationLength.Store(uint32(align))
123119
}
124120
padding := strings.Repeat(" ", align-len(location))
125121

log/handler_glog.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var errTraceSyntax = errors.New("expect file.go:234")
3939
type GlogHandler struct {
4040
origin Handler // The origin handler this wraps
4141

42-
level uint32 // Current log level, atomically accessible
43-
override uint32 // Flag whether overrides are used, atomically accessible
44-
backtrace uint32 // Flag whether backtrace location is set
42+
level atomic.Uint32 // Current log level, atomically accessible
43+
override atomic.Bool // Flag whether overrides are used, atomically accessible
44+
backtrace atomic.Bool // Flag whether backtrace location is set
4545

4646
patterns []pattern // Current list of patterns to override with
4747
siteCache map[uintptr]Lvl // Cache of callsite pattern evaluations
@@ -72,7 +72,7 @@ type pattern struct {
7272
// Verbosity sets the glog verbosity ceiling. The verbosity of individual packages
7373
// and source files can be raised using Vmodule.
7474
func (h *GlogHandler) Verbosity(level Lvl) {
75-
atomic.StoreUint32(&h.level, uint32(level))
75+
h.level.Store(uint32(level))
7676
}
7777

7878
// Vmodule sets the glog verbosity pattern.
@@ -138,7 +138,7 @@ func (h *GlogHandler) Vmodule(ruleset string) error {
138138

139139
h.patterns = filter
140140
h.siteCache = make(map[uintptr]Lvl)
141-
atomic.StoreUint32(&h.override, uint32(len(filter)))
141+
h.override.Store(len(filter) != 0)
142142

143143
return nil
144144
}
@@ -171,7 +171,7 @@ func (h *GlogHandler) BacktraceAt(location string) error {
171171
defer h.lock.Unlock()
172172

173173
h.location = location
174-
atomic.StoreUint32(&h.backtrace, uint32(len(location)))
174+
h.backtrace.Store(len(location) > 0)
175175

176176
return nil
177177
}
@@ -180,7 +180,7 @@ func (h *GlogHandler) BacktraceAt(location string) error {
180180
// and backtrace filters, finally emitting it if either allow it through.
181181
func (h *GlogHandler) Log(r *Record) error {
182182
// If backtracing is requested, check whether this is the callsite
183-
if atomic.LoadUint32(&h.backtrace) > 0 {
183+
if h.backtrace.Load() {
184184
// Everything below here is slow. Although we could cache the call sites the
185185
// same way as for vmodule, backtracing is so rare it's not worth the extra
186186
// complexity.
@@ -198,11 +198,11 @@ func (h *GlogHandler) Log(r *Record) error {
198198
}
199199
}
200200
// If the global log level allows, fast track logging
201-
if atomic.LoadUint32(&h.level) >= uint32(r.Lvl) {
201+
if h.level.Load() >= uint32(r.Lvl) {
202202
return h.origin.Log(r)
203203
}
204204
// If no local overrides are present, fast track skipping
205-
if atomic.LoadUint32(&h.override) == 0 {
205+
if !h.override.Load() {
206206
return nil
207207
}
208208
// Check callsite cache for previously calculated log levels

0 commit comments

Comments
 (0)