diff options
author | 2017-07-07 07:36:31 -0400 | |
---|---|---|
committer | 2017-07-07 13:37:23 -0400 | |
commit | 70f84252a0f1669564ef3c92c4adeda9b5a22f50 (patch) | |
tree | 62f6270d47bfb2551796dff18d4243ed72d7c759 /src/main/java/com/google/devtools/build | |
parent | ab10165414cc1b5614d97d368ca338fd7c4ac3e5 (diff) |
Avoid relying on System.out/err going to the terminal
Use an appropriate EventHandler instead.
PiperOrigin-RevId: 161189061
Diffstat (limited to 'src/main/java/com/google/devtools/build')
3 files changed, 37 insertions, 34 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkExecutionResult.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkExecutionResult.java index 5f0bffd54c..87102ad8a8 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkExecutionResult.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkExecutionResult.java @@ -107,7 +107,7 @@ final class SkylarkExecutionResult { private final Map<String, String> envBuilder = Maps.newLinkedHashMap(); private long timeout = -1; private boolean executed = false; - private boolean quiet; + private OutErr outErr; private Builder(Map<String, String> environment) { envBuilder.putAll(environment); @@ -162,8 +162,8 @@ final class SkylarkExecutionResult { return this; } - Builder setQuiet(boolean quiet) { - this.quiet = quiet; + Builder setOutErr(OutErr outErr) { + this.outErr = outErr; return this; } @@ -183,8 +183,8 @@ final class SkylarkExecutionResult { // Bazel will crash. Maybe we should use custom output streams that throw an appropriate // exception when reaching a specific size. delegator.addSink(recorder); - if (!quiet) { - delegator.addSink(OutErr.create(System.out, System.err)); + if (outErr != null) { + delegator.addSink(outErr); } try { String[] argsArray = new String[args.size()]; diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java index 08dc59b527..51e48c1d13 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java @@ -25,6 +25,7 @@ import com.google.devtools.build.lib.bazel.repository.downloader.HttpDownloader; import com.google.devtools.build.lib.bazel.repository.downloader.HttpUtils; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.NativeClassObjectConstructor; import com.google.devtools.build.lib.packages.Rule; @@ -406,7 +407,7 @@ public class SkylarkRepositoryContext { .setDirectory(outputDirectory.getPathFile()) .addEnvironmentVariables(environment) .setTimeout(timeout.longValue() * 1000) - .setQuiet(quiet) + .setOutErr(quiet ? null : Reporter.outErrForReporter(env.getListener())) .execute(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java index d384928737..3559fffde8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java @@ -64,6 +64,8 @@ import com.google.devtools.build.skyframe.SkyFunction; import com.google.devtools.build.skyframe.SkyKey; import com.google.devtools.build.skyframe.SkyValue; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -874,40 +876,40 @@ public class CppCompileAction extends AbstractAction } } if (VALIDATION_DEBUG_WARN) { - synchronized (System.err) { - if (VALIDATION_DEBUG >= 2 || errors.hasProblems() || warnings.hasProblems()) { - if (errors.hasProblems()) { - System.err.println("ERROR: Include(s) were not in declared srcs:"); - } else if (warnings.hasProblems()) { - System.err.println("WARN: Include(s) were not in declared srcs:"); - } else { - System.err.println("INFO: Include(s) were OK for '" + getSourceFile() - + "', declared srcs:"); - } - for (Artifact a : context.getDeclaredIncludeSrcs()) { - System.err.println(" '" + a.toDetailString() + "'"); - } - System.err.println(" or under declared dirs:"); - for (PathFragment f : Sets.newTreeSet(context.getDeclaredIncludeDirs())) { - System.err.println(" '" + f + "'"); - } - System.err.println(" or under declared warn dirs:"); - for (PathFragment f : Sets.newTreeSet(context.getDeclaredIncludeWarnDirs())) { - System.err.println(" '" + f + "'"); - } - System.err.println(" with prefixes:"); - for (PathFragment dirpath : context.getQuoteIncludeDirs()) { - System.err.println(" '" + dirpath + "'"); - } + if (VALIDATION_DEBUG >= 2 || errors.hasProblems() || warnings.hasProblems()) { + StringWriter buffer = new StringWriter(); + PrintWriter out = new PrintWriter(buffer); + if (errors.hasProblems()) { + out.println("ERROR: Include(s) were not in declared srcs:"); + } else if (warnings.hasProblems()) { + out.println("WARN: Include(s) were not in declared srcs:"); + } else { + out.println("INFO: Include(s) were OK for '" + getSourceFile() + + "', declared srcs:"); + } + for (Artifact a : context.getDeclaredIncludeSrcs()) { + out.println(" '" + a.toDetailString() + "'"); + } + out.println(" or under declared dirs:"); + for (PathFragment f : Sets.newTreeSet(context.getDeclaredIncludeDirs())) { + out.println(" '" + f + "'"); + } + out.println(" or under declared warn dirs:"); + for (PathFragment f : Sets.newTreeSet(context.getDeclaredIncludeWarnDirs())) { + out.println(" '" + f + "'"); + } + out.println(" with prefixes:"); + for (PathFragment dirpath : context.getQuoteIncludeDirs()) { + out.println(" '" + dirpath + "'"); } + eventHandler.handle( + Event.warn(buffer.toString()).withTag(Label.print(getOwner().getLabel()))); } } if (warnings.hasProblems()) { eventHandler.handle( - Event.warn( - getOwner().getLocation(), - warnings.getMessage(this, getSourceFile())) + Event.warn(getOwner().getLocation(), warnings.getMessage(this, getSourceFile())) .withTag(Label.print(getOwner().getLabel()))); } errors.assertProblemFree(this, getSourceFile()); |