@@ -77,16 +77,17 @@ public class Log {
7777 private static PrintStream outStream = System .out ;
7878 /** The out stream. */
7979 private static PrintStream errStream = System .err ;
80-
80+ /** The out stream. */
81+ private static PrintStream mirrorStream = System .out ;
8182 /** The use colored prints. */
8283 private boolean useColoredPrints =false ;
8384
8485 private Thread logFileThread = null ;
8586
8687 private File log =null ;
8788
88- private ByteList incoming ;
89-
89+ private ByteList incomingErr ;
90+ private ByteList incomingOut ;
9091
9192 /**
9293 * Instantiates a new log.
@@ -454,22 +455,44 @@ public static void setFile(File logfile) {
454455
455456 instance .log =logfile ;
456457 instance .logFileThread =new Thread (()->{
457- instance .incoming = new ByteList ();
458- OutputStream stream = new OutputStream () {
458+ instance .incomingErr = new ByteList ();
459+
460+ OutputStream streamErr = new OutputStream () {
461+ @ Override
462+ public void write (int b ) throws IOException {
463+ instance .incomingErr .add (b );
464+ }
465+ };
466+ instance .incomingOut = new ByteList ();
467+
468+ OutputStream streamOut = new OutputStream () {
459469 @ Override
460470 public void write (int b ) throws IOException {
461- instance .incoming .add (b );
471+ instance .incomingOut .add (b );
462472 }
463473 };
464- System .setOut (new PrintStream (stream ));
465- System .setErr (new PrintStream (stream ));
466- setOutStream (new PrintStream (stream ));
474+ System .setOut (new PrintStream (streamOut ));
475+ System .setErr (new PrintStream (streamErr ));
476+ setOutStream (new PrintStream (streamErr ));
467477 while (instance .log !=null ) {
468478 ThreadUtil .wait (150 );
469- if (instance .incoming .size () > 0 )
479+ if (instance .incomingOut .size () > 0 )
480+ try {
481+ String text = instance .incomingOut .asString ();
482+ instance .incomingOut .clear ();
483+ if (text != null && text .length () > 0 ){
484+ //Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
485+ Files .write (logfile .toPath (), text .getBytes (StandardCharsets .UTF_8 ), StandardOpenOption .APPEND );
486+ mirrorStream .println (text );
487+ }
488+ text = null ;
489+ } catch (Exception e ) {
490+ e .printStackTrace ();
491+ }
492+ if (instance .incomingErr .size () > 0 )
470493 try {
471- String text = instance .incoming .asString ();
472- instance .incoming .clear ();
494+ String text = instance .incomingErr .asString ();
495+ instance .incomingErr .clear ();
473496 if (text != null && text .length () > 0 ){
474497 //Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
475498 Files .write (logfile .toPath (), text .getBytes (StandardCharsets .UTF_8 ), StandardOpenOption .APPEND );
@@ -488,7 +511,7 @@ public static void flush() {
488511 System .setOut (outStream );
489512 System .setErr (outStream );
490513 instance .log =null ;
491- while (instance .incoming .size () > 0 ) {
514+ while (instance .incomingOut .size () > 0 || instance . incomingErr . size () > 0 ) {
492515 try {
493516 Thread .sleep (10 );
494517 } catch (InterruptedException e ) {
@@ -503,4 +526,12 @@ public static void flush() {
503526 e .printStackTrace ();
504527 }
505528 }
529+
530+ public static PrintStream getMirrorStream () {
531+ return mirrorStream ;
532+ }
533+
534+ public static void setMirrorStream (PrintStream mirrorStream ) {
535+ Log .mirrorStream = mirrorStream ;
536+ }
506537}
0 commit comments