Skip to content

Commit

Permalink
Fifth cl for verbose workspaces (ability to log certain potentially n…
Browse files Browse the repository at this point in the history
…on-hermetic events that happen as part of repository rules).

* Allow to specify log file rather than dumping to INFO.
* Include a parser that is able to convert that binary file to text, optionally
filtering out specific rules.

In the future:
- Log levels, full or alerts only

RELNOTES: None
PiperOrigin-RevId: 210620591
  • Loading branch information
Googler authored and Copybara-Service committed Aug 28, 2018
1 parent 6d155d7 commit 1950bf9
Show file tree
Hide file tree
Showing 13 changed files with 560 additions and 119 deletions.
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ filegroup(
"//src/test/shell:srcs",
"//src/tools/android/java/com/google/devtools/build/android:srcs",
"//src/tools/execlog:srcs",
"//src/tools/workspacelog:srcs",
"//src/tools/launcher:srcs",
"//src/tools/package_printer/java/com/google/devtools/build/packageprinter:srcs",
"//src/tools/skylark/java/com/google/devtools/skylark/skylint:srcs",
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/google/devtools/build/lib/bazel/debug/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ java_library(
":debugging-options",
":workspace-rule-event",
"//src/main/java/com/google/devtools/build/lib:events",
"//src/main/java/com/google/devtools/build/lib:exitcode-external",
"//src/main/java/com/google/devtools/build/lib:io",
"//src/main/java/com/google/devtools/build/lib:runtime",
"//src/main/java/com/google/devtools/build/lib:util",
"//src/main/java/com/google/devtools/common/options",
"//third_party:guava",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
/** Options for debugging and verbosity tools. */
public final class DebuggingOptions extends OptionsBase {
@Option(
name = "experimental_workspace_rules_logging",
name = "experimental_workspace_rules_log_file",
defaultValue = "null",
category = "verbosity",
documentationCategory = OptionDocumentationCategory.LOGGING,
effectTags = {OptionEffectTag.UNKNOWN},
help = "Log certain Workspace Rules events")
public String workspaceRulesLogging;
help =
"Log certain Workspace Rules events into this file as delimited WorkspaceEvent protos.")
public String workspaceRulesLogFile;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.runtime.BlazeModule;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.util.AbruptExitException;
import com.google.devtools.build.lib.util.ExitCode;
import com.google.devtools.build.lib.util.io.AsynchronousFileOutputStream;
import com.google.devtools.common.options.OptionsBase;
import java.io.IOException;

/** A module for logging workspace rule events */
public final class WorkspaceRuleModule extends BlazeModule {
private Reporter reporter;
private EventBus eventBus;
private AsynchronousFileOutputStream outFileStream;

@Override
public void beforeCommand(CommandEnvironment env) {

reporter = env.getReporter();
eventBus = env.getEventBus();

Expand All @@ -38,18 +42,43 @@ public void beforeCommand(CommandEnvironment env) {
return;
}

if (env.getOptions().getOptions(DebuggingOptions.class).workspaceRulesLogging != null) {
String logFile = env.getOptions().getOptions(DebuggingOptions.class).workspaceRulesLogFile;
if (logFile != null && !logFile.isEmpty()) {
try {
outFileStream = new AsynchronousFileOutputStream(logFile);
} catch (IOException e) {
env.getReporter().handle(Event.error(e.getMessage()));
env.getBlazeModuleEnvironment()
.exit(
new AbruptExitException(
"Error initializing workspace rule log file.", ExitCode.COMMAND_LINE_ERROR));
}
eventBus.register(this);
}
}

@Override
public void afterCommand() {
if (outFileStream != null) {
try {
outFileStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
outFileStream = null;
}
}
}

@Override
public Iterable<Class<? extends OptionsBase>> getCommonCommandOptions() {
return ImmutableList.<Class<? extends OptionsBase>>of(DebuggingOptions.class);
}

@Subscribe
public void workspaceRuleEventReceived(WorkspaceRuleEvent event) {
reporter.handle(Event.info(event.logMessage()));
if (outFileStream != null) {
outFileStream.write(event.getLogEvent());
}
}
}
5 changes: 4 additions & 1 deletion src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ test_suite(
sh_test(
name = "bazel_workspaces_test",
srcs = ["bazel_workspaces_test.sh"],
data = [":test-deps"],
data = [
":test-deps",
"//src/tools/workspacelog:parser",
],
tags = ["no_windows"],
)
Loading

0 comments on commit 1950bf9

Please sign in to comment.