1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 const nAttrsInline = 5
15 // A Record holds information about a log event.
16 // Copies of a Record share state.
17 // Do not modify a Record after handing out a copy to it.
18 // Call [NewRecord] to create a new Record.
19 // Use [Record.Clone] to create a copy with no shared state.
21 // The time at which the output method (Log, Info, etc.) was called.
27 // The level of the event.
30 // The program counter at the time the record was constructed, as determined
31 // by runtime.Callers. If zero, no program counter is available.
33 // The only valid use for this value is as an argument to
34 // [runtime.CallersFrames]. In particular, it must not be passed to
35 // [runtime.FuncForPC].
38 // Allocation optimization: an inline array sized to hold
39 // the majority of log calls (based on examination of open-source
40 // code). It holds the start of the list of Attrs.
41 front [nAttrsInline]Attr
43 // The number of Attrs in front.
46 // The list of Attrs except for those in front.
48 // - len(back) > 0 iff nFront == len(front)
49 // - Unused array elements are zero. Used to detect mistakes.
53 // NewRecord creates a [Record] from the given arguments.
54 // Use [Record.AddAttrs] to add attributes to the Record.
56 // NewRecord is intended for logging APIs that want to support a [Handler] as
58 func NewRecord(t time.Time, level Level, msg string, pc uintptr) Record {
67 // Clone returns a copy of the record with no shared state.
68 // The original record and the clone can both be modified
69 // without interfering with each other.
70 func (r Record) Clone() Record {
71 r.back = slices.Clip(r.back) // prevent append from mutating shared array
75 // NumAttrs returns the number of attributes in the [Record].
76 func (r Record) NumAttrs() int {
77 return r.nFront + len(r.back)
80 // Attrs calls f on each Attr in the [Record].
81 // Iteration stops if f returns false.
82 func (r Record) Attrs(f func(Attr) bool) {
83 for i := 0; i < r.nFront; i++ {
88 for _, a := range r.back {
95 // AddAttrs appends the given Attrs to the [Record]'s list of Attrs.
96 // It omits empty groups.
97 func (r *Record) AddAttrs(attrs ...Attr) {
99 for i = 0; i < len(attrs) && r.nFront < len(r.front); i++ {
101 if a.Value.isEmptyGroup() {
104 r.front[r.nFront] = a
107 // Check if a copy was modified by slicing past the end
108 // and seeing if the Attr there is non-zero.
109 if cap(r.back) > len(r.back) {
110 end := r.back[:len(r.back)+1][len(r.back)]
112 // Don't panic; copy and muddle through.
113 r.back = slices.Clip(r.back)
114 r.back = append(r.back, String("!BUG", "AddAttrs unsafely called on copy of Record made without using Record.Clone"))
117 ne := countEmptyGroups(attrs[i:])
118 r.back = slices.Grow(r.back, len(attrs[i:])-ne)
119 for _, a := range attrs[i:] {
120 if !a.Value.isEmptyGroup() {
121 r.back = append(r.back, a)
126 // Add converts the args to Attrs as described in [Logger.Log],
127 // then appends the Attrs to the [Record]'s list of Attrs.
128 // It omits empty groups.
129 func (r *Record) Add(args ...any) {
132 a, args = argsToAttr(args)
133 if a.Value.isEmptyGroup() {
136 if r.nFront < len(r.front) {
137 r.front[r.nFront] = a
141 r.back = make([]Attr, 0, countAttrs(args)+1)
143 r.back = append(r.back, a)
148 // countAttrs returns the number of Attrs that would be created from args.
149 func countAttrs(args []any) int {
151 for i := 0; i < len(args); i++ {
153 if _, ok := args[i].(string); ok {
160 const badKey = "!BADKEY"
162 // argsToAttr turns a prefix of the nonempty args slice into an Attr
163 // and returns the unconsumed portion of the slice.
164 // If args[0] is an Attr, it returns it.
165 // If args[0] is a string, it treats the first two elements as
167 // Otherwise, it treats args[0] as a value with a missing key.
168 func argsToAttr(args []any) (Attr, []any) {
169 switch x := args[0].(type) {
172 return String(badKey, x), nil
174 return Any(x, args[1]), args[2:]
180 return Any(badKey, x), args[1:]
184 // Source describes the location of a line of source code.
186 // Function is the package path-qualified function name containing the
187 // source line. If non-empty, this string uniquely identifies a single
188 // function in the program. This may be the empty string if not known.
189 Function string `json:"function"`
190 // File and Line are the file name and line number (1-based) of the source
191 // line. These may be the empty string and zero, respectively, if not known.
192 File string `json:"file"`
193 Line int `json:"line"`
196 // attrs returns the non-zero fields of s as a slice of attrs.
197 // It is similar to a LogValue method, but we don't want Source
198 // to implement LogValuer because it would be resolved before
199 // the ReplaceAttr function was called.
200 func (s *Source) group() Value {
202 if s.Function != "" {
203 as = append(as, String("function", s.Function))
206 as = append(as, String("file", s.File))
209 as = append(as, Int("line", s.Line))
211 return GroupValue(as...)
214 // source returns a Source for the log event.
215 // If the Record was created without the necessary information,
216 // or if the location is unavailable, it returns a non-nil *Source
218 func (r Record) source() *Source {
219 fs := runtime.CallersFrames([]uintptr{r.PC})
222 Function: f.Function,