blob: 4545dec07d8b68ce804caa2da1b4144a1624a6af [file] [log] [blame]
kesavandc71914f2022-03-25 11:19:03 +05301package logrus
2
3import (
4 "bytes"
5 "sync"
6)
7
8var (
9 bufferPool BufferPool
10)
11
12type BufferPool interface {
13 Put(*bytes.Buffer)
14 Get() *bytes.Buffer
15}
16
17type defaultPool struct {
18 pool *sync.Pool
19}
20
21func (p *defaultPool) Put(buf *bytes.Buffer) {
22 p.pool.Put(buf)
23}
24
25func (p *defaultPool) Get() *bytes.Buffer {
26 return p.pool.Get().(*bytes.Buffer)
27}
28
29func getBuffer() *bytes.Buffer {
30 return bufferPool.Get()
31}
32
33func putBuffer(buf *bytes.Buffer) {
34 buf.Reset()
35 bufferPool.Put(buf)
36}
37
38// SetBufferPool allows to replace the default logrus buffer pool
39// to better meets the specific needs of an application.
40func SetBufferPool(bp BufferPool) {
41 bufferPool = bp
42}
43
44func init() {
45 SetBufferPool(&defaultPool{
46 pool: &sync.Pool{
47 New: func() interface{} {
48 return new(bytes.Buffer)
49 },
50 },
51 })
52}