-
Notifications
You must be signed in to change notification settings - Fork 69
/
syslogrelay.go
84 lines (75 loc) · 1.38 KB
/
syslogrelay.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package giDevice
import (
"bufio"
"fmt"
"github.com/electricbubble/gidevice/pkg/libimobiledevice"
"io"
"strings"
)
var _ SyslogRelay = (*syslogRelay)(nil)
func newSyslogRelay(client *libimobiledevice.SyslogRelayClient) *syslogRelay {
r := &syslogRelay{
client: client,
stop: make(chan bool),
isReading: false,
}
r.reader = bufio.NewReader(r.client.InnerConn().RawConn())
return r
}
type syslogRelay struct {
client *libimobiledevice.SyslogRelayClient
reader *bufio.Reader
stop chan bool
isReading bool
}
func (r *syslogRelay) Lines() <-chan string {
out := make(chan string)
r.isReading = true
go func() {
defer func() {
close(out)
r.isReading = false
}()
for {
select {
case <-r.stop:
return
default:
bs, err := r.readLine()
if err != nil {
if strings.Contains(err.Error(), io.EOF.Error()) {
return
}
debugLog(fmt.Sprintf("syslog: %s", err))
}
if len(bs) > 1 && bs[0] == 0 {
bs = bs[1:]
}
out <- string(bs)
}
}
}()
return out
}
func (r *syslogRelay) Stop() {
if r.isReading {
r.stop <- true
}
}
func (r *syslogRelay) readLine() ([]byte, error) {
var line []byte
for {
l, more, err := r.reader.ReadLine()
if err != nil {
return nil, err
}
if line == nil && !more {
return l, nil
}
line = append(line, l...)
if !more {
break
}
}
return line, nil
}