aboutsummaryrefslogtreecommitdiff
path: root/src/log/slog/record.go
diff options
context:
space:
mode:
authorThan McIntosh <thanm@google.com>2023-07-07 16:49:15 -0400
committerThan McIntosh <thanm@google.com>2023-07-07 16:49:15 -0400
commit71aaa8bde1ba983894121987aaf09cb2012ab622 (patch)
tree12c26e401a50654e0d557b98b2136b18b5ee9d97 /src/log/slog/record.go
parent3aba453b66371647dfad4e901fca578d2b564e09 (diff)
parent894d24d617bb72d6e1bed7b143f9f7a0ac16b844 (diff)
downloadgo-71aaa8bde1ba983894121987aaf09cb2012ab622.tar.gz
go-71aaa8bde1ba983894121987aaf09cb2012ab622.zip
[dev.inline] merge with master at 894d24d617dev.inline
Change-Id: I845eec08108c69228ebcba921f8a807a376d3fae
Diffstat (limited to 'src/log/slog/record.go')
-rw-r--r--src/log/slog/record.go24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/log/slog/record.go b/src/log/slog/record.go
index 972552d519..67b76f34e1 100644
--- a/src/log/slog/record.go
+++ b/src/log/slog/record.go
@@ -93,9 +93,17 @@ func (r Record) Attrs(f func(Attr) bool) {
}
// AddAttrs appends the given Attrs to the Record's list of Attrs.
+// It omits empty groups.
func (r *Record) AddAttrs(attrs ...Attr) {
- n := copy(r.front[r.nFront:], attrs)
- r.nFront += n
+ var i int
+ for i = 0; i < len(attrs) && r.nFront < len(r.front); i++ {
+ a := attrs[i]
+ if a.Value.isEmptyGroup() {
+ continue
+ }
+ r.front[r.nFront] = a
+ r.nFront++
+ }
// Check if a copy was modified by slicing past the end
// and seeing if the Attr there is non-zero.
if cap(r.back) > len(r.back) {
@@ -104,15 +112,25 @@ func (r *Record) AddAttrs(attrs ...Attr) {
panic("copies of a slog.Record were both modified")
}
}
- r.back = append(r.back, attrs[n:]...)
+ ne := countEmptyGroups(attrs[i:])
+ r.back = slices.Grow(r.back, len(attrs[i:])-ne)
+ for _, a := range attrs[i:] {
+ if !a.Value.isEmptyGroup() {
+ r.back = append(r.back, a)
+ }
+ }
}
// Add converts the args to Attrs as described in [Logger.Log],
// then appends the Attrs to the Record's list of Attrs.
+// It omits empty groups.
func (r *Record) Add(args ...any) {
var a Attr
for len(args) > 0 {
a, args = argsToAttr(args)
+ if a.Value.isEmptyGroup() {
+ continue
+ }
if r.nFront < len(r.front) {
r.front[r.nFront] = a
r.nFront++