Cleanups and improvements.
This commit is contained in:
parent
0c56a477bc
commit
c8f839de73
39
tee/tee.go
39
tee/tee.go
|
@ -8,15 +8,19 @@ import (
|
|||
// Tee emulates the Unix tee(1) command.
|
||||
type Tee struct {
|
||||
f *os.File
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
func (t *Tee) Write(p []byte) (int64, error) {
|
||||
func (t *Tee) Write(p []byte) (int, error) {
|
||||
n, err := os.Stdout.Write(p)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return file.Write(p)
|
||||
if t.f != nil {
|
||||
return t.f.Write(p)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (t *Tee) Close() error {
|
||||
|
@ -40,9 +44,9 @@ func NewOut(logFile string) (*Tee, error) {
|
|||
|
||||
// Printf formats according to a format specifier and writes to the
|
||||
// tee instance.
|
||||
func (t *Tee) Printf(format string, args ...interface{}) (int64, error) {
|
||||
func (t *Tee) Printf(format string, args ...interface{}) (int, error) {
|
||||
s := fmt.Sprintf(format, args...)
|
||||
n, err := os.Stdout.Write(s)
|
||||
n, err := os.Stdout.WriteString(s)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
@ -54,6 +58,15 @@ func (t *Tee) Printf(format string, args ...interface{}) (int64, error) {
|
|||
return t.f.WriteString(s)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
var globalTee = &Tee{}
|
||||
|
||||
// Open will attempt to open the logFile for the global tee instance.
|
||||
|
@ -63,10 +76,26 @@ func Open(logFile string) error {
|
|||
return err
|
||||
}
|
||||
globalTee.f = f
|
||||
return nil
|
||||
}
|
||||
|
||||
// Printf formats according to a format specifier and writes to the
|
||||
// global tee.
|
||||
func Printf(format string, args ...interface{}) (int64, error) {
|
||||
func Printf(format string, args ...interface{}) (int, error) {
|
||||
return globalTee.Printf(format, args...)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue