Add a unified status reporting UI

This adds a new status package that merges the running of "actions"
(ninja calls them edges) of multiple tools into one view of the current
state, and gives that to a number of different outputs.

For inputs:

Kati's output parser has been rewritten (and moved) to map onto the
StartAction/FinishAction API. A byproduct of this is that the build
servers should be able to extract errors from Kati better, since they
look like the errors that Ninja used to write.

Ninja is no longer directly connected to the terminal, but its output is
read via the protobuf frontend API, so it's just another tool whose
output becomes merged together.

multiproduct_kati loses its custom status routines, and uses the common
one instead.

For outputs:

The primary output is the ui/terminal.Status type, which along with
ui/terminal.Writer now controls everything about the terminal output.
Today, this doesn't really change any behaviors, but having all terminal
output going through here allows a more complicated (multi-line / full
window) status display in the future.

The tracer acts as an output of the status package, tracing all the
action start / finish events. This replaces reading the .ninja_log file,
so it now properly handles multiple output files from a single action.

A new rotated log file (out/error.log, or out/dist/logs/error.log) just
contains a description of all of the errors that happened during the
current build.

Another new compressed and rotated log file (out/verbose.log.gz, or
out/dist/logs/verbose.log.gz) contains the full verbose (showcommands)
log of every execution run by the build. Since this is now written on
every build, the showcommands argument is now ignored -- if you want to
get the commands run, look at the log file after the build.

Test: m
Test: <built-in tests>
Test: NINJA_ARGS="-t list" m
Test: check the build.trace.gz
Test: check the new log files
Change-Id: If1d8994890d43ef68f65aa10ddd8e6e06dc7013a
diff --git a/cmd/multiproduct_kati/main.go b/cmd/multiproduct_kati/main.go
index ab82963..237d384 100644
--- a/cmd/multiproduct_kati/main.go
+++ b/cmd/multiproduct_kati/main.go
@@ -29,6 +29,8 @@
 
 	"android/soong/ui/build"
 	"android/soong/ui/logger"
+	"android/soong/ui/status"
+	"android/soong/ui/terminal"
 	"android/soong/ui/tracer"
 	"android/soong/zip"
 )
@@ -66,98 +68,34 @@
 	ctx     build.Context
 	config  build.Config
 	logFile string
+	action  *status.Action
 }
 
-type Status struct {
-	cur    int
-	total  int
-	failed int
-
-	ctx           build.Context
-	haveBlankLine bool
-	smartTerminal bool
-
-	lock sync.Mutex
-}
-
-func NewStatus(ctx build.Context) *Status {
-	return &Status{
-		ctx:           ctx,
-		haveBlankLine: true,
-		smartTerminal: ctx.IsTerminal(),
-	}
-}
-
-func (s *Status) SetTotal(total int) {
-	s.total = total
-}
-
-func (s *Status) Fail(product string, err error, logFile string) {
-	s.Finish(product)
-
-	s.lock.Lock()
-	defer s.lock.Unlock()
-
-	if s.smartTerminal && !s.haveBlankLine {
-		fmt.Fprintln(s.ctx.Stdout())
-		s.haveBlankLine = true
+func errMsgFromLog(filename string) string {
+	if filename == "" {
+		return ""
 	}
 
-	s.failed++
-	fmt.Fprintln(s.ctx.Stderr(), "FAILED:", product)
-	s.ctx.Verboseln("FAILED:", product)
-
-	if logFile != "" {
-		data, err := ioutil.ReadFile(logFile)
-		if err == nil {
-			lines := strings.Split(strings.TrimSpace(string(data)), "\n")
-			if len(lines) > errorLeadingLines+errorTrailingLines+1 {
-				lines[errorLeadingLines] = fmt.Sprintf("... skipping %d lines ...",
-					len(lines)-errorLeadingLines-errorTrailingLines)
-
-				lines = append(lines[:errorLeadingLines+1],
-					lines[len(lines)-errorTrailingLines:]...)
-			}
-			for _, line := range lines {
-				fmt.Fprintln(s.ctx.Stderr(), "> ", line)
-				s.ctx.Verboseln(line)
-			}
-		}
+	data, err := ioutil.ReadFile(filename)
+	if err != nil {
+		return ""
 	}
 
-	s.ctx.Print(err)
-}
+	lines := strings.Split(strings.TrimSpace(string(data)), "\n")
+	if len(lines) > errorLeadingLines+errorTrailingLines+1 {
+		lines[errorLeadingLines] = fmt.Sprintf("... skipping %d lines ...",
+			len(lines)-errorLeadingLines-errorTrailingLines)
 
-func (s *Status) Finish(product string) {
-	s.lock.Lock()
-	defer s.lock.Unlock()
-
-	s.cur++
-	line := fmt.Sprintf("[%d/%d] %s", s.cur, s.total, product)
-
-	if s.smartTerminal {
-		if max, ok := s.ctx.TermWidth(); ok {
-			if len(line) > max {
-				line = line[:max]
-			}
-		}
-
-		fmt.Fprint(s.ctx.Stdout(), "\r", line, "\x1b[K")
-		s.haveBlankLine = false
-	} else {
-		s.ctx.Println(line)
+		lines = append(lines[:errorLeadingLines+1],
+			lines[len(lines)-errorTrailingLines:]...)
 	}
-}
-
-func (s *Status) Finished() int {
-	s.lock.Lock()
-	defer s.lock.Unlock()
-
-	if !s.haveBlankLine {
-		fmt.Fprintln(s.ctx.Stdout())
-		s.haveBlankLine = true
+	var buf strings.Builder
+	for _, line := range lines {
+		buf.WriteString("> ")
+		buf.WriteString(line)
+		buf.WriteString("\n")
 	}
-	return s.failed
+	return buf.String()
 }
 
 // TODO(b/70370883): This tool uses a lot of open files -- over the default
@@ -194,6 +132,9 @@
 }
 
 func main() {
+	writer := terminal.NewWriter(terminal.StdioImpl{})
+	defer writer.Finish()
+
 	log := logger.New(os.Stderr)
 	defer log.Cleanup()
 
@@ -205,20 +146,24 @@
 	trace := tracer.New(log)
 	defer trace.Close()
 
+	stat := &status.Status{}
+	defer stat.Finish()
+	stat.AddOutput(terminal.NewStatusOutput(writer, ""))
+
 	build.SetupSignals(log, cancel, func() {
 		trace.Close()
 		log.Cleanup()
+		stat.Finish()
 	})
 
 	buildCtx := build.Context{&build.ContextImpl{
-		Context:        ctx,
-		Logger:         log,
-		Tracer:         trace,
-		StdioInterface: build.StdioImpl{},
+		Context: ctx,
+		Logger:  log,
+		Tracer:  trace,
+		Writer:  writer,
+		Status:  stat,
 	}}
 
-	status := NewStatus(buildCtx)
-
 	config := build.NewConfig(buildCtx)
 	if *outDir == "" {
 		name := "multiproduct-" + time.Now().Format("20060102150405")
@@ -303,7 +248,8 @@
 
 	log.Verbose("Got product list: ", products)
 
-	status.SetTotal(len(products))
+	s := buildCtx.Status.StartTool()
+	s.SetTotalActions(len(products))
 
 	var wg sync.WaitGroup
 	productConfigs := make(chan Product, len(products))
@@ -315,8 +261,18 @@
 			var stdLog string
 
 			defer wg.Done()
+
+			action := &status.Action{
+				Description: product,
+				Outputs:     []string{product},
+			}
+			s.StartAction(action)
 			defer logger.Recover(func(err error) {
-				status.Fail(product, err, stdLog)
+				s.FinishAction(status.ActionResult{
+					Action: action,
+					Error:  err,
+					Output: errMsgFromLog(stdLog),
+				})
 			})
 
 			productOutDir := filepath.Join(config.OutDir(), product)
@@ -339,12 +295,14 @@
 			productLog.SetOutput(filepath.Join(productLogDir, "soong.log"))
 
 			productCtx := build.Context{&build.ContextImpl{
-				Context:        ctx,
-				Logger:         productLog,
-				Tracer:         trace,
-				StdioInterface: build.NewCustomStdio(nil, f, f),
-				Thread:         trace.NewThread(product),
+				Context: ctx,
+				Logger:  productLog,
+				Tracer:  trace,
+				Writer:  terminal.NewWriter(terminal.NewCustomStdio(nil, f, f)),
+				Thread:  trace.NewThread(product),
+				Status:  &status.Status{},
 			}}
+			productCtx.Status.AddOutput(terminal.NewStatusOutput(productCtx.Writer, ""))
 
 			productConfig := build.NewConfig(productCtx)
 			productConfig.Environment().Set("OUT_DIR", productOutDir)
@@ -352,7 +310,7 @@
 			productConfig.Lunch(productCtx, product, *buildVariant)
 
 			build.Build(productCtx, productConfig, build.BuildProductConfig)
-			productConfigs <- Product{productCtx, productConfig, stdLog}
+			productConfigs <- Product{productCtx, productConfig, stdLog, action}
 		}(product)
 	}
 	go func() {
@@ -369,7 +327,11 @@
 			for product := range productConfigs {
 				func() {
 					defer logger.Recover(func(err error) {
-						status.Fail(product.config.TargetProduct(), err, product.logFile)
+						s.FinishAction(status.ActionResult{
+							Action: product.action,
+							Error:  err,
+							Output: errMsgFromLog(product.logFile),
+						})
 					})
 
 					defer func() {
@@ -400,7 +362,9 @@
 						}
 					}
 					build.Build(product.ctx, product.config, buildWhat)
-					status.Finish(product.config.TargetProduct())
+					s.FinishAction(status.ActionResult{
+						Action: product.action,
+					})
 				}()
 			}
 		}()
@@ -421,7 +385,5 @@
 		}
 	}
 
-	if count := status.Finished(); count > 0 {
-		log.Fatalln(count, "products failed")
-	}
+	s.Finish()
 }