diff options
author | 2017-06-14 10:19:34 +0200 | |
---|---|---|
committer | 2017-06-14 13:17:23 +0200 | |
commit | 45820abe0e4da93b8f1ce97d4296e7d42f0d617e (patch) | |
tree | 71da361a3063af546c8b0c27302bd8ca4d50d676 /src/main/java/com/google/devtools/build/lib/events | |
parent | 3cc84f2f43b93ff2d909358e5cb32bff16968232 (diff) |
Use EventHandler even before we have a Reporter instance
...instead of relying on all the methods to call printErrLn with exactly the
right format string.
PiperOrigin-RevId: 158951236
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/events')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/events/PrintingEventHandler.java | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/events/PrintingEventHandler.java b/src/main/java/com/google/devtools/build/lib/events/PrintingEventHandler.java index cad3439710..fc16475694 100644 --- a/src/main/java/com/google/devtools/build/lib/events/PrintingEventHandler.java +++ b/src/main/java/com/google/devtools/build/lib/events/PrintingEventHandler.java @@ -14,9 +14,8 @@ package com.google.devtools.build.lib.events; import com.google.devtools.build.lib.util.io.OutErr; - import java.io.IOException; -import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.Set; /** @@ -26,13 +25,6 @@ import java.util.Set; * ERROR: /home/jrluser/src/workspace/x/BUILD:23:1: syntax error. * </pre> * This syntax is parseable by Emacs's compile.el. - * - * <p> - * By default, the output will go to SYSTEM_OUT_ERR, - * but this can be changed by calling the setOut() method. - * - * <p> - * This class is used only for tests. */ public class PrintingEventHandler extends AbstractEventHandler implements EventHandler { @@ -55,15 +47,22 @@ public class PrintingEventHandler extends AbstractEventHandler public static final PrintingEventHandler ERRORS_TO_STDERR = new PrintingEventHandler(EventKind.ERRORS_AND_OUTPUT); - private OutErr outErr = OutErr.SYSTEM_OUT_ERR; + private OutErr outErr; /** - * Setup a printing event handler that will handle events matching the mask. - * Events will be printed to the original System.out and System.err - * unless/until redirected by a call to setOutErr(). + * Setup a printing event handler that prints events matching the mask. */ - public PrintingEventHandler(Set<EventKind> mask) { + public PrintingEventHandler(OutErr outErr, Set<EventKind> mask) { super(mask); + this.outErr = outErr; + } + + /** + * Setup a printing event handler that prints events matching the mask. Events are printed to the + * System.out and System.err unless/until redirected by a call to setOutErr(). + */ + public PrintingEventHandler(Set<EventKind> mask) { + this(OutErr.SYSTEM_OUT_ERR, mask); } /** @@ -96,15 +95,14 @@ public class PrintingEventHandler extends AbstractEventHandler outErr.getErrorStream().flush(); break; default: - PrintWriter err = new PrintWriter(outErr.getErrorStream()); - err.print(event.getKind()); - err.print(": "); + StringBuilder builder = new StringBuilder(); + builder.append(event.getKind()).append(": "); if (event.getLocation() != null) { - err.print(event.getLocation().print()); - err.print(": "); + builder.append(event.getLocation().print()).append(": "); } - err.println(event.getMessage()); - err.flush(); + builder.append(event.getMessage()).append("\n"); + outErr.getErrorStream().write(builder.toString().getBytes(StandardCharsets.UTF_8)); + outErr.getErrorStream().flush(); } } catch (IOException e) { /* |