Add environment variable to force keeping ANSI codes

If SOONG_UI_ANSI_OUTPUT is set to a true value force the simple status
output to keep ANSI codes.  This will allow buildbots to collect logs
with ANSI codes in them.  Smart status output is not affected as it
always keeps ANSI codes.

Bug: 147310922
Test: manual
Change-Id: I0b78ceebb65125b8e8dafb4787816fb679d3eb3e
This commit is contained in:
Colin Cross 2021-02-10 13:11:18 -08:00
parent 72a28a6a13
commit 3c0fe0edc0
6 changed files with 22 additions and 11 deletions

View file

@ -218,10 +218,16 @@ func distDir(outDir string) string {
}
}
func forceAnsiOutput() bool {
value := os.Getenv("SOONG_UI_ANSI_OUTPUT")
return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true"
}
func main() {
stdio := terminal.StdioImpl{}
output := terminal.NewStatusOutput(stdio.Stdout(), "", false, false)
output := terminal.NewStatusOutput(stdio.Stdout(), "", false, false,
forceAnsiOutput())
log := logger.New(output)
defer log.Cleanup()

View file

@ -164,7 +164,8 @@ func main() {
// Create a terminal output that mimics Ninja's.
output := terminal.NewStatusOutput(c.stdio().Stdout(), os.Getenv("NINJA_STATUS"), c.simpleOutput,
build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD"))
build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD"),
build.OsEnvironment().IsEnvTrue("SOONG_UI_ANSI_OUTPUT"))
// Attach a new logger instance to the terminal output.
log := logger.New(output)

View file

@ -48,7 +48,7 @@ type TestResults struct {
// Run runs a single build command. It emulates the "m" command line by calling into Soong UI directly.
func (t *Test) Run(logsDir string) {
output := terminal.NewStatusOutput(os.Stdout, "", false, false)
output := terminal.NewStatusOutput(os.Stdout, "", false, false, false)
log := logger.New(output)
defer log.Cleanup()

View file

@ -24,15 +24,17 @@ import (
type simpleStatusOutput struct {
writer io.Writer
formatter formatter
keepANSI bool
}
// NewSimpleStatusOutput returns a StatusOutput that represents the
// current build status similarly to Ninja's built-in terminal
// output.
func NewSimpleStatusOutput(w io.Writer, formatter formatter) status.StatusOutput {
func NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool) status.StatusOutput {
return &simpleStatusOutput{
writer: w,
formatter: formatter,
keepANSI: keepANSI,
}
}
@ -54,7 +56,9 @@ func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts sta
progress := s.formatter.progress(counts) + str
output := s.formatter.result(result)
if !s.keepANSI {
output = string(stripAnsiEscapes([]byte(output)))
}
if output != "" {
fmt.Fprint(s.writer, progress, "\n", output)

View file

@ -26,12 +26,12 @@ import (
//
// statusFormat takes nearly all the same options as NINJA_STATUS.
// %c is currently unsupported.
func NewStatusOutput(w io.Writer, statusFormat string, forceSimpleOutput, quietBuild bool) status.StatusOutput {
func NewStatusOutput(w io.Writer, statusFormat string, forceSimpleOutput, quietBuild, forceKeepANSI bool) status.StatusOutput {
formatter := newFormatter(statusFormat, quietBuild)
if !forceSimpleOutput && isSmartTerminal(w) {
return NewSmartStatusOutput(w, formatter)
} else {
return NewSimpleStatusOutput(w, formatter)
return NewSimpleStatusOutput(w, formatter, forceKeepANSI)
}
}

View file

@ -94,7 +94,7 @@ func TestStatusOutput(t *testing.T) {
t.Run("smart", func(t *testing.T) {
smart := &fakeSmartTerminal{termWidth: 40}
stat := NewStatusOutput(smart, "", false, false)
stat := NewStatusOutput(smart, "", false, false, false)
tt.calls(stat)
stat.Flush()
@ -105,7 +105,7 @@ func TestStatusOutput(t *testing.T) {
t.Run("simple", func(t *testing.T) {
simple := &bytes.Buffer{}
stat := NewStatusOutput(simple, "", false, false)
stat := NewStatusOutput(simple, "", false, false, false)
tt.calls(stat)
stat.Flush()
@ -116,7 +116,7 @@ func TestStatusOutput(t *testing.T) {
t.Run("force simple", func(t *testing.T) {
smart := &fakeSmartTerminal{termWidth: 40}
stat := NewStatusOutput(smart, "", true, false)
stat := NewStatusOutput(smart, "", true, false, false)
tt.calls(stat)
stat.Flush()
@ -269,7 +269,7 @@ func TestSmartStatusOutputWidthChange(t *testing.T) {
os.Setenv(tableHeightEnVar, "")
smart := &fakeSmartTerminal{termWidth: 40}
stat := NewStatusOutput(smart, "", false, false)
stat := NewStatusOutput(smart, "", false, false, false)
smartStat := stat.(*smartStatusOutput)
smartStat.sigwinchHandled = make(chan bool)