Path: blob/main/vendor/github.com/onsi/gomega/gexec/prefixed_writer.go
2880 views
// untested sections: 112package gexec34import (5"io"6"sync"7)89/*10PrefixedWriter wraps an io.Writer, emitting the passed in prefix at the beginning of each new line.11This can be useful when running multiple gexec.Sessions concurrently - you can prefix the log output of each12session by passing in a PrefixedWriter:1314gexec.Start(cmd, NewPrefixedWriter("[my-cmd] ", GinkgoWriter), NewPrefixedWriter("[my-cmd] ", GinkgoWriter))15*/16type PrefixedWriter struct {17prefix []byte18writer io.Writer19lock *sync.Mutex20atStartOfLine bool21}2223func NewPrefixedWriter(prefix string, writer io.Writer) *PrefixedWriter {24return &PrefixedWriter{25prefix: []byte(prefix),26writer: writer,27lock: &sync.Mutex{},28atStartOfLine: true,29}30}3132func (w *PrefixedWriter) Write(b []byte) (int, error) {33w.lock.Lock()34defer w.lock.Unlock()3536toWrite := []byte{}3738for _, c := range b {39if w.atStartOfLine {40toWrite = append(toWrite, w.prefix...)41}4243toWrite = append(toWrite, c)4445w.atStartOfLine = c == '\n'46}4748_, err := w.writer.Write(toWrite)49if err != nil {50return 0, err51}5253return len(b), nil54}555657