diff --git a/term/windows/ansi_reader.go b/term/windows/ansi_reader.go index 5b91b78..1db920d 100644 --- a/term/windows/ansi_reader.go +++ b/term/windows/ansi_reader.go @@ -28,6 +28,7 @@ type ansiReader struct { } func newAnsiReader(nFile int) *ansiReader { + initLogger() file, fd := winterm.GetStdFile(nFile) return &ansiReader{ file: file, diff --git a/term/windows/ansi_writer.go b/term/windows/ansi_writer.go index 9f3232c..0299a88 100644 --- a/term/windows/ansi_writer.go +++ b/term/windows/ansi_writer.go @@ -3,16 +3,12 @@ package windows import ( - "io/ioutil" "os" ansiterm "github.com/Azure/go-ansiterm" "github.com/Azure/go-ansiterm/winterm" - "github.com/Sirupsen/logrus" ) -var logger *logrus.Logger - // ansiWriter wraps a standard output file (e.g., os.Stdout) providing ANSI sequence translation. type ansiWriter struct { file *os.File @@ -25,18 +21,7 @@ type ansiWriter struct { } func newAnsiWriter(nFile int) *ansiWriter { - logFile := ioutil.Discard - - if isDebugEnv := os.Getenv(ansiterm.LogEnv); isDebugEnv == "1" { - logFile, _ = os.Create("ansiReaderWriter.log") - } - - logger = &logrus.Logger{ - Out: logFile, - Formatter: new(logrus.TextFormatter), - Level: logrus.DebugLevel, - } - + initLogger() file, fd := winterm.GetStdFile(nFile) info, err := winterm.GetConsoleScreenBufferInfo(fd) if err != nil { diff --git a/term/windows/windows.go b/term/windows/windows.go index bf4c7b5..ce4cb59 100644 --- a/term/windows/windows.go +++ b/term/windows/windows.go @@ -3,3 +3,31 @@ // and return pseudo-streams that convert ANSI sequences to / from Windows Console API calls. package windows + +import ( + "io/ioutil" + "os" + "sync" + + ansiterm "github.com/Azure/go-ansiterm" + "github.com/Sirupsen/logrus" +) + +var logger *logrus.Logger +var initOnce sync.Once + +func initLogger() { + initOnce.Do(func() { + logFile := ioutil.Discard + + if isDebugEnv := os.Getenv(ansiterm.LogEnv); isDebugEnv == "1" { + logFile, _ = os.Create("ansiReaderWriter.log") + } + + logger = &logrus.Logger{ + Out: logFile, + Formatter: new(logrus.TextFormatter), + Level: logrus.DebugLevel, + } + }) +}