buffer.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package logger
  2. import (
  3. "strconv"
  4. "sync"
  5. "unicode/utf8"
  6. )
  7. var bufPool = sync.Pool{
  8. New: func() interface{} {
  9. // Reserve some capacity to not re-allocate on short logs.
  10. buf := make(buffer, 0, 1024)
  11. return &buf
  12. },
  13. }
  14. // buffer is a slice of bytes with some additional convenience methods.
  15. type buffer []byte
  16. // newBuffer creates a new buffer from the pool.
  17. func newBuffer() *buffer {
  18. return bufPool.Get().(*buffer)
  19. }
  20. // free truncates the buffer and returns it to the pool.
  21. func (b *buffer) free() {
  22. // Don't keep large buffers around.
  23. if len(*b) > 16*1024 {
  24. return
  25. }
  26. *b = (*b)[:0]
  27. bufPool.Put(b)
  28. }
  29. // Write writes data to the buffer.
  30. func (b *buffer) Write(p []byte) (n int, err error) {
  31. b.append(p...)
  32. return len(p), nil
  33. }
  34. // String returns the contents of the buffer as a string.
  35. func (b *buffer) String() string {
  36. return string(*b)
  37. }
  38. // len returns the number of bytes written to the buffer.
  39. func (b *buffer) len() int {
  40. return len(*b)
  41. }
  42. // append appends data to the buffer.
  43. func (b *buffer) append(data ...byte) {
  44. *b = append(*b, data...)
  45. }
  46. // appendString appends a string value to the buffer.
  47. // If the string does not require escaping, it is appended directly.
  48. // Otherwise, it is escaped and quoted.
  49. func (b *buffer) appendString(data string) {
  50. if b.isStringQuoteSafe(data) {
  51. b.appendStringRaw(data)
  52. } else {
  53. b.appendStringQuoted(data)
  54. }
  55. }
  56. // appendStringRaw appends a string value to the buffer without escaping.
  57. func (b *buffer) appendStringRaw(data string) {
  58. *b = append(*b, data...)
  59. }
  60. // appendStringQuoted appends a string value to the buffer, escaping and quoting it as necessary.
  61. func (b *buffer) appendStringQuoted(data string) {
  62. *b = strconv.AppendQuote(*b, data)
  63. }
  64. // appendInt appends an integer value to the buffer.
  65. func (b *buffer) appendInt(data int64) {
  66. *b = strconv.AppendInt(*b, data, 10)
  67. }
  68. // appendUint appends an unsigned integer value to the buffer.
  69. func (b *buffer) appendUint(data uint64) {
  70. *b = strconv.AppendUint(*b, data, 10)
  71. }
  72. // appendFloat appends a float value to the buffer.
  73. func (b *buffer) appendFloat(data float64) {
  74. *b = strconv.AppendFloat(*b, data, 'g', -1, 64)
  75. }
  76. // appendBool appends a boolean value to the buffer.
  77. func (b *buffer) appendBool(data bool) {
  78. *b = strconv.AppendBool(*b, data)
  79. }
  80. // remove removes the last n bytes from the buffer.
  81. func (b *buffer) remove(n int) {
  82. n = max(0, n)
  83. trimTo := max(0, len(*b)-n)
  84. *b = (*b)[:trimTo]
  85. }
  86. // removeNewline removes the trailing newline character from the buffer, if present.
  87. func (b *buffer) removeNewline() {
  88. if len(*b) > 0 && (*b)[len(*b)-1] == '\n' {
  89. *b = (*b)[:len(*b)-1]
  90. }
  91. }
  92. // isStringQuoteSafe checks if a string is safe to append without quoting.
  93. func (b *buffer) isStringQuoteSafe(val string) bool {
  94. for i := 0; i < len(val); i++ {
  95. if b := val[i]; b >= utf8.RuneSelf || !quoteSafeSet[b] {
  96. return false
  97. }
  98. }
  99. return true
  100. }
  101. // quoteSafeSet is a set of runes that are safe to append without quoting.
  102. // Some runes here are explicitly marked as unsafe for clarity.
  103. // The unlisted runes are considered unsafe by default.
  104. // Shamesly stolen from https://github.com/golang/go/blob/master/src/encoding/json/tables.go
  105. // and tuned for our needs.
  106. var quoteSafeSet = [utf8.RuneSelf]bool{
  107. ' ': false,
  108. '!': true,
  109. '"': false,
  110. '#': true,
  111. '$': true,
  112. '%': true,
  113. '&': true,
  114. '\'': false,
  115. '(': true,
  116. ')': true,
  117. '*': true,
  118. '+': true,
  119. ',': true,
  120. '-': true,
  121. '.': true,
  122. '/': true,
  123. '0': true,
  124. '1': true,
  125. '2': true,
  126. '3': true,
  127. '4': true,
  128. '5': true,
  129. '6': true,
  130. '7': true,
  131. '8': true,
  132. '9': true,
  133. ':': false,
  134. ';': true,
  135. '<': true,
  136. '=': false,
  137. '>': true,
  138. '?': true,
  139. '@': true,
  140. 'A': true,
  141. 'B': true,
  142. 'C': true,
  143. 'D': true,
  144. 'E': true,
  145. 'F': true,
  146. 'G': true,
  147. 'H': true,
  148. 'I': true,
  149. 'J': true,
  150. 'K': true,
  151. 'L': true,
  152. 'M': true,
  153. 'N': true,
  154. 'O': true,
  155. 'P': true,
  156. 'Q': true,
  157. 'R': true,
  158. 'S': true,
  159. 'T': true,
  160. 'U': true,
  161. 'V': true,
  162. 'W': true,
  163. 'X': true,
  164. 'Y': true,
  165. 'Z': true,
  166. '[': true,
  167. '\\': false,
  168. ']': true,
  169. '^': true,
  170. '_': true,
  171. '`': false,
  172. 'a': true,
  173. 'b': true,
  174. 'c': true,
  175. 'd': true,
  176. 'e': true,
  177. 'f': true,
  178. 'g': true,
  179. 'h': true,
  180. 'i': true,
  181. 'j': true,
  182. 'k': true,
  183. 'l': true,
  184. 'm': true,
  185. 'n': true,
  186. 'o': true,
  187. 'p': true,
  188. 'q': true,
  189. 'r': true,
  190. 's': true,
  191. 't': true,
  192. 'u': true,
  193. 'v': true,
  194. 'w': true,
  195. 'x': true,
  196. 'y': true,
  197. 'z': true,
  198. '{': false,
  199. '|': true,
  200. '}': false,
  201. '~': true,
  202. }