Adding logging and some common functions.

This commit is contained in:
Kyle
2015-09-22 03:27:14 -07:00
parent 49444c3318
commit 0750c235b6
6 changed files with 496 additions and 0 deletions

42
mwc/mwc.go Normal file
View File

@@ -0,0 +1,42 @@
// Package mwc implements MultiWriteClosers.
package mwc
import "io"
type mwc struct {
wcs []io.WriteCloser
}
// Write implements the Writer interface.
func (t *mwc) Write(p []byte) (n int, err error) {
for _, w := range t.wcs {
n, err = w.Write(p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
}
}
return len(p), nil
}
// Close implements the Closer interface.
func (t *mwc) Close() error {
for _, wc := range t.wcs {
err := wc.Close()
if err != nil {
return err
}
}
return nil
}
// MultiWriteClose creates a WriteCloser that duplicates its writes to
// all the provided writers, similar to the Unix tee(1) command.
func MultiWriteCloser(wc ...io.WriteCloser) io.WriteCloser {
wcs := make([]io.WriteCloser, len(wc))
copy(wcs, wc)
return &mwc{wcs}
}