fix stderr (stdout was wrong too)

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-10 02:46:27 +00:00
parent 954470c34c
commit 59c9bf88ca
3 changed files with 30 additions and 9 deletions

Binary file not shown.

View File

@ -3,24 +3,41 @@ package main
/* /*
import "log" import "log"
import "reflect" import "reflect"
import "os"
*/ */
import "os"
// import "github.com/davecgh/go-spew/spew" // import "github.com/davecgh/go-spew/spew"
import "git.wit.com/jcarr/shell" import "git.wit.com/jcarr/shell"
func main() { func main() {
shell.SpewOn()
shell.Run("ls /tmp") shell.Run("ls /tmp")
shell.Run("ping -c 4 localhost") shell.Run("ping -c 3 localhost")
// slow down the polling to every 2 seconds // slow down the polling to every 2 seconds
shell.SetDelayInMsec(2000) shell.SetDelayInMsec(2000)
shell.Run("ping -c 4 localhost") shell.Run("ping -c 4 localhost")
// capture ping output into a file
fout, _ := os.Create("/tmp/example1.ping.stdout")
ferr, _ := os.Create("/tmp/example1.ping.stderr")
shell.SetStdout(fout)
shell.SetStderr(ferr)
shell.Run("ping -c 5 localhost")
// turn out process exit debugging
shell.SpewOn()
fout, _ = os.Create("/tmp/example1.fail.stdout")
ferr, _ = os.Create("/tmp/example1.fail.stderr")
shell.SetStdout(fout)
shell.SetStderr(ferr)
// TODO: this might not be working // TODO: this might not be working
// check error handling // check error handling
shell.Run("ls /tmpthisisnothere") shell.Run("ls /tmpthisisnothere")

View File

@ -90,8 +90,6 @@ func Run(cmdline string) int {
newreader := bufio.NewReader(pstdout) newreader := bufio.NewReader(pstdout)
nbr := nbreader.NewNBReader(newreader, 1024) nbr := nbreader.NewNBReader(newreader, 1024)
// nbrerr := nbreader.NewNBReader(newerrreader, 1024)
tmp := bufio.NewReader(pstderr) tmp := bufio.NewReader(pstderr)
go NonBlockingReader(tmp, shellStderr) go NonBlockingReader(tmp, shellStderr)
@ -113,15 +111,19 @@ func Run(cmdline string) int {
log.Println("Read() count = ", count, "err = ", err) log.Println("Read() count = ", count, "err = ", err)
oneByte = make([]byte, 1024) oneByte = make([]byte, 1024)
count, err = nbr.Read(oneByte) count, err = nbr.Read(oneByte)
f.Write([]byte(string(oneByte))) log.Println("STDOUT: count = ", count)
f.Write(oneByte[0:count])
f.Flush() f.Flush()
empty = true empty = true
dead = true dead = true
} }
f.Write([]byte(string(oneByte))) // f.Write([]byte(string(oneByte)))
f.Flush()
if (count == 0) { if (count == 0) {
empty = true empty = true
} else {
log.Println("STDOUT: count = ", count)
f.Write(oneByte[0:count])
f.Flush()
} }
} }
@ -180,8 +182,10 @@ func NonBlockingReader(buffReader *bufio.Reader, writeFileHandle *os.File) {
log.Println("STDERR: totalCount = ", totalCount) log.Println("STDERR: totalCount = ", totalCount)
totalCount = 0 totalCount = 0
} }
} else {
log.Println("STDERR: count = ", count)
writeFileHandle.Write(oneByte[0:count])
} }
writeFileHandle.Write([]byte(string(oneByte)))
} }
} }
} }