Skip to content

Commit

Permalink
use bufio.Reader instead bufio.Scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
strangeman committed Jul 12, 2017
1 parent bca53cd commit 664cb4b
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions api/tasks/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,41 @@ func (t *task) updateStatus() {
}
}

func (t *task) logPipe(scanner *bufio.Scanner) {
for scanner.Scan() {
t.log(scanner.Text())
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix bool = true
err error = nil
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
if err := scanner.Err(); err != nil { // Let's hold on to err...
t.log("Error scanning input!") // and use it here.
panic(err)
return string(ln), err
}

func (t *task) logPipe(reader *bufio.Reader) {

s, e := Readln(reader)
for e == nil {
t.log(s)
s, e = Readln(reader)
}

/*for reader.Scan() {
t.log(reader.Text())
}
if err := reader.Err(); err != nil {
t.log("Error scanning input!")
panic(err)
}*/
}

func (t *task) logCmd(cmd *exec.Cmd) {
stderr, _ := cmd.StderrPipe()
stdout, _ := cmd.StdoutPipe()

go t.logPipe(bufio.NewScanner(stderr))
go t.logPipe(bufio.NewScanner(stdout))
go t.logPipe(bufio.NewReader(stderr))
go t.logPipe(bufio.NewReader(stdout))
}

0 comments on commit 664cb4b

Please sign in to comment.