aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/buildeventstream/transports
diff options
context:
space:
mode:
authorGravatar Eduardo Colaco <eduardocolaco@google.com>2016-11-03 15:33:47 +0000
committerGravatar Laszlo Csomor <laszlocsomor@google.com>2016-11-03 16:12:39 +0000
commit8d8abe46bd7ab0c87beb31d97c9185760d762d79 (patch)
tree197a26794eddd4b59878b199c1914bc751f5a776 /src/main/java/com/google/devtools/build/lib/buildeventstream/transports
parent38e54ac14e69113e9790ce9a526784e0f18e4b6e (diff)
Writes a machine readable representation of BuildEvents to a file (varint delimited).
Adds --experimental_build_event_binary_file option that enables varint delimited proto loggging to the specified file path Adds varint delimited BuildEventStreamTransport and BuildEventStreamerModule Adds BuildEventStreamerModule for configuring and setting up BuildEventStreamer and its associated BuildEventTransports. Adds BuildEventTransportFactory which creates a Set of transports from command options. Moves BuildEventStreamer configuration from BlazeCommandDispatcher and BuildEventStreamerModule -- Change-Id: If71f2b58654879c2509206da47e6d1a846bf397f Reviewed-on: https://bazel-review.googlesource.com/#/c/7010/ MOS_MIGRATED_REVID=138073726
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/buildeventstream/transports')
-rw-r--r--src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BUILD2
-rw-r--r--src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransport.java47
-rw-r--r--src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventStreamOptions.java48
-rw-r--r--src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventTransportFactory.java74
4 files changed, 171 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BUILD b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BUILD
index 618abca496..11b6c22ccc 100644
--- a/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BUILD
@@ -11,6 +11,8 @@ java_library(
deps = [
"//src/main/java/com/google/devtools/build/lib:buildeventstream",
"//src/main/java/com/google/devtools/build/lib/buildeventstream/proto:build_event_stream_java_proto",
+ "//src/main/java/com/google/devtools/common/options",
+ "//third_party:guava",
"//third_party/protobuf",
],
)
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransport.java b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransport.java
new file mode 100644
index 0000000000..c0f341e2eb
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BinaryFormatFileTransport.java
@@ -0,0 +1,47 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.buildeventstream.transports;
+
+import com.google.devtools.build.lib.buildeventstream.BuildEvent;
+import com.google.devtools.build.lib.buildeventstream.BuildEventTransport;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * A simple {@link BuildEventTransport} that writes varint delimited binary representation of event
+ * {@link BuildEvent} protocol-buffers to a file. Files written by this Transport can be read by
+ * successive calls of {code BuildEvent.Builder#mergeDelimitedFrom(InputStream)} (or the static
+ * method {@code BuildEvent.parseDelimitedFrom(InputStream)}).
+ */
+public final class BinaryFormatFileTransport implements BuildEventTransport {
+ private final BufferedOutputStream out;
+
+ public BinaryFormatFileTransport(String path) throws IOException {
+ this.out = new BufferedOutputStream(new FileOutputStream(new File(path)));
+ }
+
+ @Override
+ public synchronized void sendBuildEvent(BuildEvent event) throws IOException {
+ event.asStreamProto().writeDelimitedTo(out);
+ out.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ out.close();
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventStreamOptions.java b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventStreamOptions.java
new file mode 100644
index 0000000000..c4cb96a36d
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventStreamOptions.java
@@ -0,0 +1,48 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.buildeventstream.transports;
+
+import com.google.devtools.common.options.Option;
+import com.google.devtools.common.options.OptionsBase;
+
+/** Options used to configure BuildEventStreamer and its BuildEventTransports. */
+public class BuildEventStreamOptions extends OptionsBase {
+
+ @Option(
+ name = "experimental_build_event_text_file",
+ defaultValue = "",
+ category = "hidden",
+ help = "If non-empty, write a textual representation of the build event protocol to that file"
+ )
+ public String buildEventTextFile;
+
+ @Option(
+ name = "experimental_build_event_binary_file",
+ defaultValue = "",
+ category = "hidden",
+ help =
+ "If non-empty, write a varint delimited binary representation of representation of the"
+ + " build event protocol to that file."
+ )
+ public String buildEventBinaryFile;
+
+ public String getBuildEventTextFile() {
+ return buildEventTextFile;
+ }
+
+ public String getBuildEventBinaryFile() {
+ return buildEventBinaryFile;
+ }
+}
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventTransportFactory.java b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventTransportFactory.java
new file mode 100644
index 0000000000..f456b189d6
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/buildeventstream/transports/BuildEventTransportFactory.java
@@ -0,0 +1,74 @@
+// Copyright 2016 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.buildeventstream.transports;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
+import com.google.devtools.build.lib.buildeventstream.BuildEventTransport;
+import java.io.IOException;
+
+/** Factory used to create a Set of BuildEventTransports from BuildEventStreamOptions. */
+public enum BuildEventTransportFactory {
+ TEXT_TRANSPORT {
+ @Override
+ protected boolean enabled(BuildEventStreamOptions options) {
+ return !isNullOrEmpty(options.getBuildEventTextFile());
+ }
+
+ @Override
+ protected BuildEventTransport create(BuildEventStreamOptions options) throws IOException {
+ return new TextFormatFileTransport(options.getBuildEventTextFile());
+ }
+ },
+
+ BINARY_TRANSPORT {
+ @Override
+ protected boolean enabled(BuildEventStreamOptions options) {
+ return !isNullOrEmpty(options.getBuildEventBinaryFile());
+ }
+
+ @Override
+ protected BuildEventTransport create(BuildEventStreamOptions options) throws IOException {
+ return new BinaryFormatFileTransport(options.getBuildEventBinaryFile());
+ }
+ };
+
+ /**
+ * Creates a {@link ImmutableSet} of {@link BuildEventTransport} based on the specified {@link
+ * BuildEventStreamOptions}.
+ *
+ * @param options Options used configure and create the returned BuildEventTransports.
+ * @return A {@link ImmutableSet} of BuildEventTransports. This set may be empty.
+ * @throws IOException Exception propagated from a {@link BuildEventTransport} creation failure.
+ */
+ public static ImmutableSet<BuildEventTransport> createFromOptions(BuildEventStreamOptions options)
+ throws IOException {
+ Builder<BuildEventTransport> buildEventTransportsBuilder = ImmutableSet.builder();
+ for (BuildEventTransportFactory transportFactory : BuildEventTransportFactory.values()) {
+ if (transportFactory.enabled(options)) {
+ buildEventTransportsBuilder.add(transportFactory.create(options));
+ }
+ }
+ return buildEventTransportsBuilder.build();
+ }
+
+ /** Returns true if this factory BuildEventTransport is enabled by the specified options. */
+ protected abstract boolean enabled(BuildEventStreamOptions options);
+
+ /** Creates a BuildEventTransport from the specified options. */
+ protected abstract BuildEventTransport create(BuildEventStreamOptions options) throws IOException;
+}