package net.thechunk.playpen.utils.process;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileProcessListener implements IProcessListener {
    private Writer writer = null;
    private final ExecutorService service;

    public FileProcessListener(File file) throws IOException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // quick, will probably change later
        Date date = new Date();

        writer = new OutputStreamWriter(Files.newOutputStream(file.toPath(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE));
        service = Executors.newSingleThreadExecutor();
        writer.write("-- FileProcessListener SESSION STARTED " + dateFormat.format(date) + "\r\n");
        writer.flush();
    }

    @Override
    public void onProcessAttach(XProcess proc) {
    }

    @Override
    public void onProcessDetach(XProcess proc) {
    }

    @Override
    public void onProcessOutput(XProcess proc, String out) {
        if (!service.isShutdown()) {
            service.execute(() -> {
                try {
                    writer.write(out);
                    writer.write(System.lineSeparator());
                } catch (IOException e) {
                }
            });
        }
    }

    @Override
    public void onProcessInput(XProcess proc, String in) {
        if (!service.isShutdown()) {
            service.execute(() -> {
                try {
                    writer.write(in);
                    writer.write(System.lineSeparator());
                } catch (IOException e) {
                }
            });
        }
    }

    @Override
    public void onProcessEnd(XProcess proc) {
        service.shutdownNow();
        try {
            writer.write("-- FileProcessListener SESSION ENDED\r\n");
            writer.flush();
        }
        catch(IOException e) {}
        finally {
            try {
                writer.close();
            }
            catch(IOException e) {}
        }
    }
}

@@ -372,16 +372,24 @@
 private 
+Buffered
 Writer w
@@ -402,16 +402,16 @@
 = null;%0A
-
     priv
@@ -677,52 +677,31 @@
 r = 
-new OutputStreamWriter(Files.newOutputStream
+Files.newBufferedWriter
 (fil
@@ -776,17 +776,16 @@
 .CREATE)
-)
 ;%0A      
@@ -1363,32 +1363,68 @@
 neSeparator());%0A
+                    writer.flush();%0A
                 
@@ -1706,16 +1706,16 @@
 te(in);%0A
-
         
@@ -1756,32 +1756,68 @@
 neSeparator());%0A
+                    writer.flush();%0A
                 

Fix logging