forked from TRON-US/go-btfs-api
-
Notifications
You must be signed in to change notification settings - Fork 8
/
logger.go
41 lines (35 loc) · 888 Bytes
/
logger.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
package shell
import (
"context"
"encoding/json"
"io"
)
// Logger is used to handle incoming logs from the btfs node
type Logger struct {
resp io.ReadCloser
dec *json.Decoder
}
// Next is used to retrieve the next event from the logging system
func (l Logger) Next() (map[string]interface{}, error) {
var out map[string]interface{}
return out, l.dec.Decode(&out)
}
// Close is used to close our reader
func (l Logger) Close() error {
return l.resp.Close()
}
// GetLogs is used to retrieve a parsable logger object
func (s *Shell) GetLogs(ctx context.Context) (Logger, error) {
resp, err := s.Request("log/tail").Send(ctx)
if err != nil {
return Logger{}, err
}
if resp.Error != nil {
resp.Output.Close()
return Logger{}, resp.Error
}
return newLogger(resp.Output), nil
}
func newLogger(resp io.ReadCloser) Logger {
return Logger{resp, json.NewDecoder(resp)}
}