Cleanup stdout/stderr log initialization

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-03 15:56:22 +01:00
parent 892e19c019
commit 2e79f67e93
5 changed files with 17 additions and 26 deletions

View File

@ -2,7 +2,6 @@ package commands
import (
"fmt"
"os"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/check"
@ -12,7 +11,7 @@ import (
func cmdConfig(c CommandLine, api libmachine.API) error {
// Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr)
log.RedirectStdOutToStdErr()
if len(c.Args()) != 1 {
return ErrExpectedOneMachine

View File

@ -50,7 +50,7 @@ func cmdEnv(c CommandLine, api libmachine.API) error {
// Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr)
log.RedirectStdOutToStdErr()
if c.Bool("unset") {
shellCfg, err = shellCfgUnset(c, api)

View File

@ -82,13 +82,13 @@ func TestExecServer(t *testing.T) {
logReader, logWriter := io.Pipe()
log.SetOutWriter(logWriter)
log.SetErrWriter(logWriter)
log.GetStandardLogger().OutWriter = logWriter
log.GetStandardLogger().ErrWriter = logWriter
defer func() {
log.IsDebug = false
log.SetOutWriter(os.Stdout)
log.SetErrWriter(os.Stderr)
log.GetStandardLogger().OutWriter = os.Stdout
log.GetStandardLogger().ErrWriter = os.Stderr
}()
stdoutReader, stdoutWriter := io.Pipe()

View File

@ -4,17 +4,17 @@ package main
import (
"encoding/json"
"fmt"
"os"
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/log"
"os"
)
func main() {
log.IsDebug = true
log.SetOutWriter(os.Stdout)
log.SetErrWriter(os.Stderr)
log.GetStandardLogger().OutWriter = os.Stdout
log.GetStandardLogger().ErrWriter = os.Stderr
client := libmachine.NewClient("/tmp/automatic")

View File

@ -1,33 +1,24 @@
package log
import (
"io"
"os"
"sync"
)
var (
l = StandardLogger{
mu: &sync.Mutex{},
OutWriter: os.Stdout,
ErrWriter: os.Stderr,
mu: &sync.Mutex{},
}
IsDebug = false
)
type Fields map[string]interface{}
func init() {
// TODO: Is this really the best approach? I worry that it will create
// implicit behavior which may be problmatic for users of the lib.
SetOutWriter(os.Stdout)
SetErrWriter(os.Stderr)
}
func SetOutWriter(w io.Writer) {
l.OutWriter = w
}
func SetErrWriter(w io.Writer) {
l.ErrWriter = w
// RedirectStdOutToStdErr prevents any log from corrupting the output
func RedirectStdOutToStdErr() {
l.OutWriter = l.ErrWriter
}
func Debug(args ...interface{}) {
@ -54,7 +45,6 @@ func Infof(fmtString string, args ...interface{}) {
l.Infof(fmtString, args...)
}
func Fatal(args ...interface{}) {
l.Fatal(args...)
}
@ -79,4 +69,6 @@ func WithField(fieldName string, field interface{}) Logger {
func WithFields(fields Fields) Logger {
return l.WithFields(fields)
func GetStandardLogger() *StandardLogger {
return &l
}