Post the build tool logs event from the BuildTool

Collect the relevant data in the BuildResult and post the build tool
logs event from a central location rather than a module. Maybe not the
ideal location, but we have multiple modules that need to contribute
to build tool logs, so it must be posted from a common location. It
could be in a module, except that would require modules depending on /
extending other modules, which we don't support right now.

Note that this change includes a fix to LastBuildEvent, without which
this wouldn't work since this change is changing the order of the last
two events posted.

PiperOrigin-RevId: 222061608
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/BuildResult.java b/src/main/java/com/google/devtools/build/lib/buildtool/BuildResult.java
index 5ca0204..20cf11f 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/BuildResult.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/BuildResult.java
@@ -19,10 +19,16 @@
 import com.google.common.base.Preconditions;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection;
+import com.google.devtools.build.lib.buildeventstream.BuildToolLogs;
 import com.google.devtools.build.lib.skyframe.AspectValue;
 import com.google.devtools.build.lib.util.ExitCode;
+import com.google.devtools.build.lib.util.Pair;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.protobuf.ByteString;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import javax.annotation.Nullable;
 
 /**
@@ -45,6 +51,8 @@
   private Collection<ConfiguredTarget> skippedTargets;
   private Collection<AspectValue> successfulAspects;
 
+  private final BuildToolLogCollection buildToolLogCollection = new BuildToolLogCollection();
+
   public BuildResult(long startTimeMillis) {
     this.startTimeMillis = startTimeMillis;
   }
@@ -241,6 +249,15 @@
     return skippedTargets;
   }
 
+  /**
+   * Collection of data for the build tool logs event. This may only be modified until the
+   * BuildCompleteEvent is posted; any changes after that event is handled will not be included in
+   * the build tool logs event.
+   */
+  public BuildToolLogCollection getBuildToolLogCollection() {
+    return buildToolLogCollection;
+  }
+
   /** For debugging. */
   @Override
   public String toString() {
@@ -253,6 +270,53 @@
         .add("actualTargets", actualTargets)
         .add("testTargets", testTargets)
         .add("successfulTargets", successfulTargets)
+        .add("buildToolLogCollection", buildToolLogCollection)
         .toString();
   }
+
+  /** Collection of data for the build tool logs event. */
+  public static final class BuildToolLogCollection {
+    private final List<Pair<String, ByteString>> directValues = new ArrayList<>();
+    private final List<Pair<String, String>> directUris = new ArrayList<>();
+    private final List<Pair<String, Path>> logFiles = new ArrayList<>();
+    private boolean frozen;
+
+    public BuildToolLogCollection freeze() {
+      frozen = true;
+      return this;
+    }
+
+    public BuildToolLogCollection addDirectValue(String name, byte[] data) {
+      Preconditions.checkState(!frozen);
+      this.directValues.add(Pair.of(name, ByteString.copyFrom(data)));
+      return this;
+    }
+
+    public BuildToolLogCollection addUri(String name, String uri) {
+      Preconditions.checkState(!frozen);
+      this.directUris.add(Pair.of(name, uri));
+      return this;
+    }
+
+    public BuildToolLogCollection addLocalFile(String name, Path path) {
+      Preconditions.checkState(!frozen);
+      this.logFiles.add(Pair.of(name, path));
+      return this;
+    }
+
+    public BuildToolLogs toEvent() {
+      Preconditions.checkState(frozen);
+      return new BuildToolLogs(directValues, directUris, logFiles);
+    }
+
+    /** For debugging. */
+    @Override
+    public String toString() {
+      return MoreObjects.toStringHelper(this)
+          .add("directValues", directValues)
+          .add("directUris", directUris)
+          .add("logFiles", logFiles)
+          .toString();
+    }
+  }
 }