package io.airlift.log;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.logging.ErrorManager.CLOSE_FAILURE;
import static java.util.logging.ErrorManager.FLUSH_FAILURE;
import static java.util.logging.ErrorManager.FORMAT_FAILURE;
import static java.util.logging.ErrorManager.WRITE_FAILURE;

final class OutputStreamHandler
        extends Handler
{
    private final Writer writer;

    public OutputStreamHandler(OutputStream out)
    {
        writer = new OutputStreamWriter(out, UTF_8);
        setFormatter(new StaticFormatter());
    }

    @Override
    public void publish(LogRecord record)
    {
        if (!isLoggable(record)) {
            return;
        }

        String message;
        try {
            message = getFormatter().format(record);
        }
        catch (Exception e) {
            reportError(null, e, FORMAT_FAILURE);
            return;
        }

        try {
            writer.write(message);
        }
        catch (Exception e) {
            reportError(null, e, WRITE_FAILURE);
        }
    }

    @Override
    public void flush()
    {
        try {
            writer.flush();
        }
        catch (Exception e) {
            reportError(null, e, FLUSH_FAILURE);
        }
    }

    @Override
    public void close()
    {
        try {
            writer.flush();
        }
        catch (Exception e) {
            reportError(null, e, CLOSE_FAILURE);
        }
    }
}

@@ -97,24 +97,29 @@
 java.io.
+Print
 Writer;%0A
 import j
@@ -102,32 +102,105 @@
 io.PrintWriter;%0A
+import java.io.Writer;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A
 import java.util
@@ -313,307 +313,199 @@
 _8;%0A
-import static java.util.logging.ErrorManager.CLOSE_FAILURE;%0Aimport static java.util.logging.ErrorManager.FLUSH_FAILURE;%0Aimport static java.util.logging.ErrorManager.FORMAT_FAILURE;%0Aimport static java.util.logging.ErrorManager.WRITE_FAILURE;%0A%0Afinal class OutputStreamHandler%0A        extends Handler%0A%7B
+%0Afinal class OutputStreamHandler%0A        extends Handler%0A%7B%0A    private final StaticFormatter formatter = new StaticFormatter();%0A    private final AtomicBoolean reported = new AtomicBoolean();
 %0A   
@@ -647,53 +647,8 @@
 8);%0A
-        setFormatter(new StaticFormatter());%0A
     
@@ -782,32 +782,8 @@
  %7D%0A%0A
-        String message;%0A
     
@@ -808,30 +808,30 @@
     
-message = getF
+writer.write(f
 ormatter
 ().f
@@ -826,18 +826,16 @@
 ormatter
-()
 .format(
@@ -841,16 +841,45 @@
 (record)
+);%0A            writer.flush()
 ;%0A      
@@ -928,65 +928,85 @@
     
-reportError(null, e, FORMAT_FAILURE);%0A            return;
+// try to report the first error%0A            if (!reported.getAndSet(true)) %7B
 %0A   
@@ -1006,35 +1006,32 @@
 ) %7B%0A        
-%7D%0A%0A
         
 try %7B%0A      
@@ -1022,46 +1022,50 @@
     
-try %7B%0A            w
+PrintWriter error = new PrintW
 riter
-.
+(
 write
-(message
+r
 );%0A 
@@ -1067,34 +1067,32 @@
 r);%0A        
-%7D%0A
         
 catch (Excep
@@ -1083,76 +1083,38 @@
     
-catch (Exception e) %7B%0A            reportError(null, e, WRITE_FAILURE
+error.print(%22LOGGING FAILED: %22
 );%0A 
@@ -1124,69 +1124,54 @@
     
-%7D%0A
     
-%7D%0A%0A
     
-@Override%0A    public void flush()
+e.printStackTrace(error);
 %0A    
-%7B%0A
         
 try 
@@ -1170,337 +1170,139 @@
     
-try %7B%0A            writer.flush();%0A        %7D%0A        catch (Exception e) %7B%0A            reportError(null, e, FLUSH_FAILURE);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void close()%0A    %7B%0A        try %7B%0A            writer.flush();%0A        %7D%0A        catch (Exception e) %7B%0A            reportError(null, e, CLOSE_FAILURE);%0A        %7D%0A    
+    error.flush();%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void flush() %7B%7D%0A%0A    @Override%0A    public void close() %7B
 %7D%0A%7D%0A

Add flushing for console logging