From c5a73e1d1fdd4756c54a1f14f0d0f5e2b57ca78a Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 27 Aug 2021 12:10:47 -0400 Subject: [PATCH 1/2] log: use color.Error in log So that color codes work in windows for log messages. --- log/log.go | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/log/log.go b/log/log.go index 3f4a9354..9a3e85f3 100644 --- a/log/log.go +++ b/log/log.go @@ -2,7 +2,6 @@ package log import ( "fmt" - "os" "github.com/fatih/color" ) @@ -16,15 +15,10 @@ const ( ) var ( - level = WarnLevel - out stringWriter = os.Stderr + level = WarnLevel + out = color.Error ) -// TODO: replace with io.StringWriter once support for go1.11 is dropped. -type stringWriter interface { - WriteString(s string) (n int, err error) -} - // SetLevel for the global logger. func SetLevel(l Level) { level = l @@ -35,9 +29,9 @@ func Warnf(format string, args ...interface{}) { if level < WarnLevel { return } - out.WriteString(color.YellowString("WARN ")) - out.WriteString(fmt.Sprintf(format, args...)) - out.WriteString("\n") + fmt.Fprint(out, color.YellowString("WARN ")) + fmt.Fprintf(out, format, args...) + fmt.Fprint(out, "\n") } // Debugf prints the message to stderr, with no prefix. @@ -45,8 +39,8 @@ func Debugf(format string, args ...interface{}) { if level < DebugLevel { return } - out.WriteString(fmt.Sprintf(format, args...)) - out.WriteString("\n") + fmt.Fprintf(out, format, args...) + fmt.Fprint(out, "\n") } // Errorf prints the message to stderr, with a red ERROR prefix. @@ -54,9 +48,9 @@ func Errorf(format string, args ...interface{}) { if level < ErrorLevel { return } - out.WriteString(color.RedString("ERROR ")) - out.WriteString(fmt.Sprintf(format, args...)) - out.WriteString("\n") + fmt.Fprint(out, color.RedString("ERROR ")) + fmt.Fprintf(out, format, args...) + fmt.Fprint(out, "\n") } // Error prints the message to stderr, with a red ERROR prefix. @@ -64,7 +58,6 @@ func Error(msg string) { if level < ErrorLevel { return } - out.WriteString(color.RedString("ERROR ")) - out.WriteString(msg) - out.WriteString("\n") + fmt.Fprint(out, color.RedString("ERROR ")) + fmt.Fprintln(out, msg) } From 7cadea0534979f4a31a68d2c30ba3c0b5202d4e9 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 27 Aug 2021 12:15:07 -0400 Subject: [PATCH 2/2] Fix color output on windows Use color.Output and color.Error so that color codes are handled properly on windows. --- cmd/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 6b86f90f..ffa08f8d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -48,8 +48,8 @@ func setupFlags(name string) (*pflag.FlagSet, *options) { junitTestCaseClassnameFormat: &junitFieldFormatValue{}, junitTestSuiteNameFormat: &junitFieldFormatValue{}, postRunHookCmd: &commandValue{}, - stdout: os.Stdout, - stderr: os.Stderr, + stdout: color.Output, + stderr: color.Error, } flags := pflag.NewFlagSet(name, pflag.ContinueOnError) flags.SetInterspersed(false)