Skip to content

Commit ced4709

Browse files
committed
adding a log flush
1 parent b33bce6 commit ced4709

File tree

1 file changed

+27
-8
lines changed
  • src/main/java/com/neuronrobotics/sdk/common

1 file changed

+27
-8
lines changed

src/main/java/com/neuronrobotics/sdk/common/Log.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public class Log {
8484
private Thread logFileThread = null;
8585

8686
private File log=null;
87+
88+
private ByteList incoming;
8789

8890

8991
/**
@@ -169,8 +171,7 @@ private void add(String message, int importance) {
169171

170172
if( systemprint) {
171173
outStream.println(m.toString());
172-
if(outStream != System.out)
173-
errStream.println(m);
174+
errStream.println(m);
174175
}
175176

176177

@@ -453,22 +454,22 @@ public static void setFile(File logfile) {
453454

454455
instance.log=logfile;
455456
instance.logFileThread=new Thread(()->{
456-
ByteList incoming = new ByteList();
457+
instance.incoming = new ByteList();
457458
OutputStream stream = new OutputStream() {
458459
@Override
459460
public void write(int b) throws IOException {
460-
incoming.add(b);
461+
instance.incoming.add(b);
461462
}
462463
};
463464
System.setOut(new PrintStream(stream));
464465
System.setErr(new PrintStream(stream));
465466
setOutStream(new PrintStream(stream));
466467
while (instance.log!=null) {
467468
ThreadUtil.wait(150);
468-
if (incoming.size() > 0)
469+
if (instance.incoming.size() > 0)
469470
try {
470-
String text = incoming.asString();
471-
incoming.clear();
471+
String text = instance.incoming.asString();
472+
instance.incoming.clear();
472473
if (text != null && text.length() > 0){
473474
Files.writeString(instance.log.toPath(), text, StandardCharsets.UTF_8,
474475
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
@@ -481,5 +482,23 @@ public void write(int b) throws IOException {
481482
});
482483
instance.logFileThread.start();
483484
}
484-
485+
public static void flush() {
486+
setOutStream(outStream);
487+
System.setOut(outStream);
488+
System.setErr(outStream);
489+
while(instance.incoming.size() > 0) {
490+
try {
491+
Thread.sleep(10);
492+
} catch (InterruptedException e) {
493+
// TODO Auto-generated catch block
494+
e.printStackTrace();
495+
}
496+
}
497+
try {
498+
instance.logFileThread.join();
499+
} catch (InterruptedException e) {
500+
// TODO Auto-generated catch block
501+
e.printStackTrace();
502+
}
503+
}
485504
}

0 commit comments

Comments
 (0)