2424 */
2525package com .oracle .svm .hosted .reporting ;
2626
27+ import java .io .ByteArrayOutputStream ;
2728import java .io .File ;
29+ import java .io .PrintStream ;
2830import java .io .PrintWriter ;
2931import java .lang .management .GarbageCollectorMXBean ;
3032import java .lang .management .ManagementFactory ;
@@ -95,6 +97,11 @@ public class ProgressReporter {
9597 private final boolean showLinks ;
9698 private final ScheduledExecutorService executor = Executors .newSingleThreadScheduledExecutor ();
9799
100+ private final PrintStream systemOut = System .out ;
101+ private final PrintStream systemErr = System .err ;
102+ private final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream ();
103+ private final ByteArrayOutputStream errBuffer = new ByteArrayOutputStream ();
104+
98105 private ScheduledFuture <?> periodicPrintingTask ;
99106 private int periodicPrintingTaskPeriodSeconds = 1 ;
100107
@@ -150,7 +157,7 @@ public ProgressReporter(OptionValues options) {
150157 linePrinter = new ColorfulLinePrinter (enableProgress );
151158 /* Add a shutdown hook to reset the ANSI mode. */
152159 try {
153- Runtime .getRuntime ().addShutdownHook (new Thread (ProgressReporter ::resetANSIMode ));
160+ Runtime .getRuntime ().addShutdownHook (new Thread (this ::resetANSIMode ));
154161 } catch (IllegalStateException e ) {
155162 /* If the VM is already shutting down, we do not need to register shutdownHook. */
156163 }
@@ -522,11 +529,13 @@ private void printProgressStart() {
522529 linePrinter .a (stringFilledWith (PROGRESS_BAR_START - numStageChars , " " )).dim ().a ("[" );
523530 numStageChars = PROGRESS_BAR_START + 1 ; /* +1 for [ */
524531 linePrinter .flush ();
532+ installStdOutErrBuffers ();
525533 }
526534
527535 private void printProgressEnd () {
528536 linePrinter .a ("]" ).reset ().flush ();
529537 numStageChars ++; // for ]
538+ resetStdOutErrBuffers ();
530539 }
531540
532541 public void printStageProgress () {
@@ -558,6 +567,7 @@ private void printStageEnd(double totalTime) {
558567 String padding = stringFilledWith (Math .max (0 , CHARACTERS_PER_LINE - numStageChars - suffix .length ()), " " );
559568 linePrinter .a (padding ).dim ().a (suffix ).reset ().flushln (false );
560569 numStageChars = 0 ;
570+ flushStdOutErrBuffers ();
561571 if (SubstrateOptions .BuildOutputGCWarnings .getValue ()) {
562572 checkForExcessiveGarbageCollection ();
563573 }
@@ -581,14 +591,37 @@ private void checkForExcessiveGarbageCollection() {
581591 lastGCStats = currentGCStats ;
582592 }
583593
584- private static void resetANSIMode () {
585- System .out .print (ANSIColors .RESET );
594+ private void installStdOutErrBuffers () {
595+ System .setOut (new PrintStream (outBuffer ));
596+ System .setErr (new PrintStream (errBuffer ));
597+ }
598+
599+ private void resetStdOutErrBuffers () {
600+ System .setOut (systemOut );
601+ System .setErr (systemErr );
602+ }
603+
604+ private void flushStdOutErrBuffers () {
605+ flushStdIOBuffer (outBuffer , systemOut );
606+ flushStdIOBuffer (errBuffer , systemErr );
607+ }
608+
609+ private static void flushStdIOBuffer (ByteArrayOutputStream buffer , PrintStream target ) {
610+ if (buffer .size () > 0 ) {
611+ target .print (buffer .toString ());
612+ target .flush ();
613+ buffer .reset ();
614+ }
586615 }
587616
588617 /*
589618 * HELPERS
590619 */
591620
621+ private void resetANSIMode () {
622+ systemOut .print (ANSIColors .RESET );
623+ }
624+
592625 private static String stringFilledWith (int size , String fill ) {
593626 return new String (new char [size ]).replace ("\0 " , fill );
594627 }
@@ -786,7 +819,7 @@ private void flush() {
786819 if (printBuffer != null ) {
787820 textBuffer .forEach (printBuffer ::append );
788821 } else {
789- textBuffer .forEach (System . out ::print );
822+ textBuffer .forEach (systemOut ::print );
790823 }
791824 textBuffer .clear ();
792825 }
@@ -798,7 +831,7 @@ private void printRaw(String text) {
798831 if (printBuffer != null ) {
799832 printBuffer .append (text );
800833 } else {
801- System . out .print (text );
834+ systemOut .print (text );
802835 }
803836 }
804837
@@ -811,15 +844,15 @@ private void flushln(boolean useOutputPrefix) {
811844 return ;
812845 }
813846 if (useOutputPrefix ) {
814- System . out .print (outputPrefix );
847+ systemOut .print (outputPrefix );
815848 }
816849 if (printBuffer != null ) {
817- System . out .print (printBuffer .toString ());
850+ systemOut .print (printBuffer .toString ());
818851 printBuffer .setLength (0 ); // Clear buffer.
819852 }
820- textBuffer .forEach (System . out ::print );
853+ textBuffer .forEach (systemOut ::print );
821854 textBuffer .clear ();
822- System . out .println ();
855+ systemOut .println ();
823856 }
824857
825858 private void flushCenteredln () {
0 commit comments