Move the logic to find the server log to the BlazeRuntime.
It makes more sense for this abstract logic to not live within the info
command, and I need to query the server log from elsewhere in an upcoming
change.
RELNOTES: None.
PiperOrigin-RevId: 234001630
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
index 72c3cd8..e6b75db 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java
@@ -67,6 +67,7 @@
import com.google.devtools.build.lib.util.AbruptExitException;
import com.google.devtools.build.lib.util.CustomExitCodePublisher;
import com.google.devtools.build.lib.util.ExitCode;
+import com.google.devtools.build.lib.util.LogHandlerQuerier;
import com.google.devtools.build.lib.util.LoggingUtil;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.Pair;
@@ -579,6 +580,30 @@
return finalCommandResult;
}
+ /**
+ * Returns the path to the Blaze server INFO log.
+ *
+ * @return the path to the log or empty if the log is not yet open
+ * @throws IOException if the log location cannot be determined
+ */
+ public Optional<Path> getServerLogPath() throws IOException {
+ LogHandlerQuerier logHandlerQuerier;
+ try {
+ logHandlerQuerier = LogHandlerQuerier.getConfiguredInstance();
+ } catch (IllegalStateException e) {
+ throw new IOException("Could not find a querier for server log location", e);
+ }
+
+ Optional<java.nio.file.Path> loggerFilePath;
+ try {
+ loggerFilePath = logHandlerQuerier.getLoggerFilePath(logger);
+ } catch (IllegalArgumentException e) {
+ throw new IOException("Could not query server log location", e);
+ }
+
+ return loggerFilePath.map((p) -> fileSystem.getPath(p.toString()));
+ }
+
// Make sure we keep a strong reference to this logger, so that the
// configuration isn't lost when the gc kicks in.
private static Logger templateLogger = Logger.getLogger("com.google.devtools.build");
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/InfoItem.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/InfoItem.java
index 1a3f830..805a1cd 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/InfoItem.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/InfoItem.java
@@ -38,12 +38,12 @@
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.AbruptExitException;
-import com.google.devtools.build.lib.util.LogHandlerQuerier;
import com.google.devtools.build.lib.util.ProcessUtils;
import com.google.devtools.build.lib.util.StringUtilities;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.common.options.OptionsParsingResult;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.management.GarbageCollectorMXBean;
@@ -294,25 +294,13 @@
@Override
public byte[] get(Supplier<BuildConfiguration> configurationSupplier, CommandEnvironment env)
throws AbruptExitException {
- LogHandlerQuerier logHandlerQuerier;
try {
- logHandlerQuerier = LogHandlerQuerier.getConfiguredInstance();
- } catch (IllegalStateException e) {
- // Non-fatal error: we don't want the "info" command to crash.
- logger.log(Level.WARNING, "Could not find a querier for server log location", e);
+ Optional<Path> path = env.getRuntime().getServerLogPath();
+ return print(path.map(Path::toString).orElse(""));
+ } catch (IOException e) {
+ logger.log(Level.WARNING, "Failed to determine server log location", e);
return print("UNKNOWN LOG LOCATION");
}
- Optional<java.nio.file.Path> loggerFilePath;
- try {
- loggerFilePath = logHandlerQuerier.getLoggerFilePath(logger);
- } catch (IllegalArgumentException e) {
- // Non-fatal error: we don't want the "info" command to crash.
- logger.log(Level.WARNING, "Could not query for server log location", e);
- return print("UNKNOWN LOG LOCATION");
- }
- // If loggerFilePath is empty, then no log file is currently open, so an empty string is the
- // correct output.
- return print(loggerFilePath.map(java.nio.file.Path::toString).orElse(""));
}
}