goutils/tee/tee.go

103 lines
2.0 KiB
Go
Raw Normal View History

2017-09-12 10:25:12 +00:00
package tee
import (
"fmt"
"os"
)
// Tee emulates the Unix tee(1) command.
type Tee struct {
f *os.File
2017-09-12 10:38:47 +00:00
Verbose bool
2017-09-12 10:25:12 +00:00
}
2017-09-12 10:38:47 +00:00
func (t *Tee) Write(p []byte) (int, error) {
2017-09-12 10:25:12 +00:00
n, err := os.Stdout.Write(p)
if err != nil {
return n, err
}
2017-09-12 10:38:47 +00:00
if t.f != nil {
return t.f.Write(p)
}
return n, nil
2017-09-12 10:25:12 +00:00
}
2017-11-16 16:32:05 +00:00
// Close calls Close on the underlying file.
2017-09-12 10:25:12 +00:00
func (t *Tee) Close() error {
return t.f.Close()
}
// NewOut writes to standard output only. The file is created, not
// appended to.
func NewOut(logFile string) (*Tee, error) {
if logFile == "" {
return &Tee{}, nil
}
f, err := os.Create(logFile)
if err !=nil {
return nil, err
}
return &Tee{f: f}, nil
}
// Printf formats according to a format specifier and writes to the
// tee instance.
2017-09-12 10:38:47 +00:00
func (t *Tee) Printf(format string, args ...interface{}) (int, error) {
2017-09-12 10:25:12 +00:00
s := fmt.Sprintf(format, args...)
2017-09-12 10:38:47 +00:00
n, err := os.Stdout.WriteString(s)
2017-09-12 10:25:12 +00:00
if err != nil {
return n, err
}
if t.f == nil {
return n, err
}
return t.f.WriteString(s)
}
2017-09-12 10:38:47 +00:00
// VPrintf is a variant of Printf that only prints if the Tee's
// Verbose flag is set.
func (t *Tee) VPrintf(format string, args ...interface{}) (int, error) {
if t.Verbose {
return t.Printf(format, args...)
}
return 0, nil
}
2017-09-12 10:25:12 +00:00
var globalTee = &Tee{}
// Open will attempt to open the logFile for the global tee instance.
func Open(logFile string) error {
f, err := os.Create(logFile)
if err !=nil {
return err
}
globalTee.f = f
2017-09-12 10:38:47 +00:00
return nil
2017-09-12 10:25:12 +00:00
}
// Printf formats according to a format specifier and writes to the
// global tee.
2017-09-12 10:38:47 +00:00
func Printf(format string, args ...interface{}) (int, error) {
2017-09-12 10:25:12 +00:00
return globalTee.Printf(format, args...)
}
2017-09-12 10:38:47 +00:00
// VPrintf calls VPrintf on the global tee instance.
func VPrintf(format string, args ...interface{}) (int, error) {
return globalTee.VPrintf(format, args...)
}
// Close calls close on the global tee instance.
func Close() error {
return globalTee.Close()
}
// SetVerbose controls the verbosity of the global tee.
func SetVerbose(verbose bool) {
globalTee.Verbose = verbose
}