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,17 @@ 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+ if (isEnabled ) {
533+ installStdOutErrBuffers ();
534+ }
525535 }
526536
527537 private void printProgressEnd () {
528538 linePrinter .a ("]" ).reset ().flush ();
529539 numStageChars ++; // for ]
540+ if (isEnabled ) {
541+ resetStdOutErrBuffers ();
542+ }
530543 }
531544
532545 public void printStageProgress () {
@@ -558,6 +571,9 @@ private void printStageEnd(double totalTime) {
558571 String padding = stringFilledWith (Math .max (0 , CHARACTERS_PER_LINE - numStageChars - suffix .length ()), " " );
559572 linePrinter .a (padding ).dim ().a (suffix ).reset ().flushln (false );
560573 numStageChars = 0 ;
574+ if (isEnabled ) {
575+ flushStdOutErrBuffers ();
576+ }
561577 if (SubstrateOptions .BuildOutputGCWarnings .getValue ()) {
562578 checkForExcessiveGarbageCollection ();
563579 }
@@ -581,14 +597,37 @@ private void checkForExcessiveGarbageCollection() {
581597 lastGCStats = currentGCStats ;
582598 }
583599
584- private static void resetANSIMode () {
585- System .out .print (ANSIColors .RESET );
600+ private void installStdOutErrBuffers () {
601+ System .setOut (new PrintStream (outBuffer ));
602+ System .setErr (new PrintStream (errBuffer ));
603+ }
604+
605+ private void resetStdOutErrBuffers () {
606+ System .setOut (systemOut );
607+ System .setErr (systemErr );
608+ }
609+
610+ private void flushStdOutErrBuffers () {
611+ flushStdIOBuffer (outBuffer , systemOut );
612+ flushStdIOBuffer (errBuffer , systemErr );
613+ }
614+
615+ private static void flushStdIOBuffer (ByteArrayOutputStream buffer , PrintStream target ) {
616+ if (buffer .size () > 0 ) {
617+ target .print (buffer .toString ());
618+ target .flush ();
619+ buffer .reset ();
620+ }
586621 }
587622
588623 /*
589624 * HELPERS
590625 */
591626
627+ private void resetANSIMode () {
628+ systemOut .print (ANSIColors .RESET );
629+ }
630+
592631 private static String stringFilledWith (int size , String fill ) {
593632 return new String (new char [size ]).replace ("\0 " , fill );
594633 }
@@ -786,7 +825,7 @@ private void flush() {
786825 if (printBuffer != null ) {
787826 textBuffer .forEach (printBuffer ::append );
788827 } else {
789- textBuffer .forEach (System . out ::print );
828+ textBuffer .forEach (systemOut ::print );
790829 }
791830 textBuffer .clear ();
792831 }
@@ -798,7 +837,7 @@ private void printRaw(String text) {
798837 if (printBuffer != null ) {
799838 printBuffer .append (text );
800839 } else {
801- System . out .print (text );
840+ systemOut .print (text );
802841 }
803842 }
804843
@@ -811,15 +850,15 @@ private void flushln(boolean useOutputPrefix) {
811850 return ;
812851 }
813852 if (useOutputPrefix ) {
814- System . out .print (outputPrefix );
853+ systemOut .print (outputPrefix );
815854 }
816855 if (printBuffer != null ) {
817- System . out .print (printBuffer .toString ());
856+ systemOut .print (printBuffer .toString ());
818857 printBuffer .setLength (0 ); // Clear buffer.
819858 }
820- textBuffer .forEach (System . out ::print );
859+ textBuffer .forEach (systemOut ::print );
821860 textBuffer .clear ();
822- System . out .println ();
861+ systemOut .println ();
823862 }
824863
825864 private void flushCenteredln () {
0 commit comments