Merge pull request #33 from subicura/master

fix long stdout line read issue
This commit is contained in:
David Dollar
2014-12-16 09:54:07 -05:00
3 changed files with 33 additions and 3 deletions
+2
View File
@@ -0,0 +1,2 @@
stdout1: ruby ./stdout.rb
stdout2: ruby ./stdout.rb
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby
$stdout.sync = true
10.times do |i|
puts "sample log message... " * rand(i*1000) + "ok - #{i}"
sleep 1
end
puts "finish!"
+21 -3
View File
@@ -7,6 +7,7 @@ import (
"io"
"os"
"sync"
"bytes"
)
type OutletFactory struct {
@@ -33,9 +34,26 @@ func (of *OutletFactory) LineReader(wg *sync.WaitGroup, name string, index int,
color := colors[index%len(colors)]
scanner := bufio.NewScanner(r)
for scanner.Scan() {
of.WriteLine(name, scanner.Text(), color, ct.None, isError)
reader := bufio.NewReader(r)
var buffer bytes.Buffer
for {
buf := make([]byte, 1024)
v, _ := reader.Read(buf)
if v == 0 {
return
}
idx := bytes.IndexByte(buf, '\n')
if idx >= 0 {
buffer.Write(buf[0:idx])
of.WriteLine(name, buffer.String(), color, ct.None, isError)
buffer.Reset()
} else {
buffer.Write(buf)
}
}
}