summaryrefslogtreecommitdiff
path: root/error_buffer.go
blob: 27f35e784b643f7e48af0db66d0de9a65de2d234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package roadrunner

import (
	"bytes"
	"sync"
)

// EventStderrOutput - is triggered when worker sends data into stderr. The context is output data in []bytes form.
const EventStderrOutput = 1900

// thread safe errBuffer
type errBuffer struct {
	mu     sync.Mutex
	buffer *bytes.Buffer
	lsn    func(event int, ctx interface{})
}

// Listen attaches error stream even listener.
func (b *errBuffer) Listen(l func(event int, ctx interface{})) {
	b.mu.Lock()
	defer b.mu.Unlock()

	b.lsn = l
}

// Len returns the number of bytes of the unread portion of the errBuffer;
// b.Len() == len(b.Bytes()).
func (b *errBuffer) Len() int {
	b.mu.Lock()
	defer b.mu.Unlock()

	return b.buffer.Len()
}

// Write appends the contents of p to the errBuffer, growing the errBuffer as
// needed. The return value n is the length of p; err is always nil. If the
// errBuffer becomes too large, Write will panic with ErrTooLarge.
func (b *errBuffer) Write(p []byte) (n int, err error) {
	b.mu.Lock()
	defer b.mu.Unlock()

	if b.lsn != nil {
		b.lsn(EventStderrOutput, p)
	}

	return b.buffer.Write(p)
}

// Strings fetches all errBuffer data into string.
func (b *errBuffer) String() string {
	b.mu.Lock()
	defer b.mu.Unlock()

	return b.buffer.String()
}