...

Source file src/golang.conradwood.net/tests/ctx/tee_writer.go

Documentation: golang.conradwood.net/tests/ctx

     1  package main
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  type tee struct {
     8  	out1    io.Writer
     9  	out2    io.Writer
    10  	prefix2 string
    11  }
    12  
    13  func NewTee(out1, out2 io.Writer, prefix2 string) *tee {
    14  	t := &tee{out1: out1, out2: out2, prefix2: prefix2}
    15  	return t
    16  }
    17  func (t *tee) Write(buf []byte) (int, error) {
    18  	tbuf := t.addPrefix(buf, t.prefix2)
    19  	t.out2.Write(tbuf)
    20  	n, err := t.out1.Write(buf)
    21  	return n, err
    22  }
    23  
    24  // add a prefix to each line
    25  func (t *tee) addPrefix(buf []byte, prefix string) []byte {
    26  	if prefix == "" {
    27  		return buf
    28  	}
    29  	pbuf := []byte(prefix)
    30  	res := append(pbuf, buf...)
    31  	return res
    32  }
    33  

View as plain text