ToText converts back the LogLine to textual format and appends it to the specified buffer. If called on a default constructed LogLine (zero-value), ToText returns nil, which is an useless but syntactically valid buffer.
(buf []byte)
| 150 | // If called on a default constructed LogLine (zero-value), ToText |
| 151 | // returns nil, which is an useless but syntactically valid buffer. |
| 152 | func (l *LogLine) ToText(buf []byte) []byte { |
| 153 | // Fast path: if no fields have been written, we can just copy the |
| 154 | // content of the original buffer and return it. |
| 155 | if l.wcnt == 0 { |
| 156 | blen, bcap, dlen := len(buf), cap(buf), len(l.data) |
| 157 | avail := bcap - blen |
| 158 | if avail < dlen { |
| 159 | // Not enough capacity: create a new buffer big enough to hold the |
| 160 | // previous buffer data, which we copy into, and the log line data. |
| 161 | newbuf := make([]byte, blen+dlen) |
| 162 | copy(newbuf, buf) |
| 163 | buf = newbuf |
| 164 | } else { |
| 165 | // We have the capacity, just reslice to the desired length. |
| 166 | buf = buf[:blen+dlen] |
| 167 | } |
| 168 | copy(buf[blen:], l.data) |
| 169 | return buf |
| 170 | } |
| 171 | |
| 172 | // Get the last setted index in the write array. |
| 173 | var last int |
| 174 | for i := int(LogLineNumFields) - 1; i > 0; i-- { |
| 175 | if l.wmask[i] != 0 { |
| 176 | last = i |
| 177 | break |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Get the last index in the data buffer. |
| 182 | if l.data != nil { |
| 183 | var lastr int |
| 184 | for i := len(l.idx) - 1; i > 0; i-- { |
| 185 | if l.idx[i] != 0 { |
| 186 | lastr = i - 1 |
| 187 | break |
| 188 | } |
| 189 | } |
| 190 | // Update last value. |
| 191 | if last < lastr { |
| 192 | last = lastr |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Compute an estimate of the max capacity required, so only one |
| 197 | // allocation will ever be performed. |
| 198 | var wlen int |
| 199 | for i := uint8(1); i <= l.wcnt; i++ { |
| 200 | wlen += len(l.wdata[i]) |
| 201 | } |
| 202 | wlen += int(l.wcnt) - 1 // Add 1 additional byte per separator. |
| 203 | |
| 204 | blen, bcap, dlen := len(buf), cap(buf), len(l.data) |
| 205 | avail := bcap - blen |
| 206 | if avail < wlen+dlen { |
| 207 | newbuf := make([]byte, blen, blen+dlen+wlen) |
| 208 | copy(newbuf, buf) |
| 209 | buf = newbuf |