From dbce5191f56975ef6bd004d68f8fb9e7b7eaa009 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Thu, 14 Dec 2017 14:50:16 +0000 Subject: [PATCH] Feed the stdout and stderr of the Pilot subprocess into glog Fixes: #166 --- pkg/pilot/elasticsearch/v5/process.go | 2 - pkg/pilot/genericpilot/process/process.go | 48 ++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/pkg/pilot/elasticsearch/v5/process.go b/pkg/pilot/elasticsearch/v5/process.go index b37548b33..acc249b0a 100644 --- a/pkg/pilot/elasticsearch/v5/process.go +++ b/pkg/pilot/elasticsearch/v5/process.go @@ -14,8 +14,6 @@ func (p *Pilot) CmdFunc(pilot *v1alpha1.Pilot) (*exec.Cmd, error) { } cmd := exec.Command("elasticsearch") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr cmd.Env = p.env().Strings() return cmd, nil diff --git a/pkg/pilot/genericpilot/process/process.go b/pkg/pilot/genericpilot/process/process.go index fb37997e1..24134583c 100644 --- a/pkg/pilot/genericpilot/process/process.go +++ b/pkg/pilot/genericpilot/process/process.go @@ -1,9 +1,11 @@ package process import ( + "bufio" "fmt" "os" "os/exec" + "sync" "github.com/golang/glog" ) @@ -33,20 +35,64 @@ type Interface interface { type Adapter struct { Signals Signals Cmd *exec.Cmd + wg sync.WaitGroup } var _ Interface = &Adapter{} +func (p *Adapter) startCommandOutputLoggers() error { + stdout, err := p.Cmd.StdoutPipe() + if err != nil { + return err + } + p.wg.Add(1) + go func() { + defer p.wg.Done() + in := bufio.NewScanner(stdout) + for in.Scan() { + glog.Infoln(in.Text()) + } + err := in.Err() + if err != nil { + glog.Error(err) + } + }() + + stderr, err := p.Cmd.StderrPipe() + if err != nil { + return err + } + p.wg.Add(1) + go func() { + defer p.wg.Done() + in := bufio.NewScanner(stderr) + for in.Scan() { + glog.Errorln(in.Text()) + } + err := in.Err() + if err != nil { + glog.Error(err) + } + }() + return nil +} + func (p *Adapter) Start() error { glog.V(2).Infof("Starting process: %v", p.Cmd.Args) - if err := p.Cmd.Start(); err != nil { + err := p.startCommandOutputLoggers() + if err != nil { + return err + } + + if err = p.Cmd.Start(); err != nil { return fmt.Errorf("error starting process: %s", err.Error()) } return nil } func (p *Adapter) Wait() error { + defer p.wg.Wait() return p.Cmd.Wait() }