@cryptotaxi247 / kubo / commits / 4bbd9b2da

fix log tail command

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jul 29, 2015 at 09:16 UTC 4bbd9b2da207294bd5f1bf17363a6c60a76f6f6b
1 file changed +6 -57
core/commands/log.go
+6 -57
@@ -3,12 +3,10 @@ package commands
3 import (
4 "fmt"
5 "io"
6 - "strings"
6
7 cmds "github.com/ipfs/go-ipfs/commands"
8 + "github.com/ipfs/go-ipfs/thirdparty/eventlog"
9 u "github.com/ipfs/go-ipfs/util"
10 -
11 - tail "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ActiveState/tail"
10 )
11
12 // Golang os.Args overrides * and replaces the character argument with
@@ -80,61 +78,12 @@ var logTailCmd = &cmds.Command{
78 },
79
80 Run: func(req cmds.Request, res cmds.Response) {
83 - path := fmt.Sprintf("%s/logs/events.log", req.InvocContext().ConfigRoot)
84 -
85 - outChan := make(chan interface{})
86 -
81 + r, w := io.Pipe()
82 + eventlog.WriterGroup.AddWriter(w)
83 go func() {
88 - defer close(outChan)
89 -
90 - t, err := tail.TailFile(path, tail.Config{
91 - Location: &tail.SeekInfo{0, 2},
92 - Follow: true,
93 - MustExist: true,
94 - Logger: tail.DiscardingLogger,
95 - })
96 - if err != nil {
97 - fmt.Println(err.Error())
98 - return
99 - }
100 - defer t.Stop()
101 -
102 - done := req.Context().Done()
103 -
104 - for line := range t.Lines {
105 - // return when context closes
106 - select {
107 - case <-done:
108 - return
109 - default:
110 - }
111 -
112 - if line.Err != nil {
113 - fmt.Println(err.Error())
114 - return
115 - }
116 - // TODO: unpack the line text into a struct and output that
117 - outChan <- &MessageOutput{line.Text}
118 - }
84 + <-req.Context().Done()
85 + w.Close()
86 }()
120 -
121 - res.SetOutput((<-chan interface{})(outChan))
122 - },
123 - Marshalers: cmds.MarshalerMap{
124 - cmds.Text: func(res cmds.Response) (io.Reader, error) {
125 - outChan, ok := res.Output().(<-chan interface{})
126 - if !ok {
127 - return nil, u.ErrCast()
128 - }
129 -
130 - return &cmds.ChannelMarshaler{
131 - Channel: outChan,
132 - Marshaler: func(v interface{}) (io.Reader, error) {
133 - output := v.(*MessageOutput)
134 - return strings.NewReader(output.Message + "\n"), nil
135 - },
136 - }, nil
137 - },
87 + res.SetOutput(r)
88 },
139 - Type: MessageOutput{},
89 }