package org.bdgp.MMSlide;

import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

import com.j256.ormlite.logger.LocalLog;

public class Logger extends java.util.logging.Logger {
    // Turn off ORMlite logging since it's too inflexible to integrate into
    // our logging system.
    static {
        System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "FATAL");
    }
    
    public static Logger create(String logfile, String source, Level loglevel) {
        try { return new Logger(logfile, source, loglevel); }
        catch (Exception e) {throw new RuntimeException(e);}
    }
    public Logger(String logfile, String source, Level loglevel) {
        super(source, null);
        try { 
            FileHandler fh = new FileHandler(logfile);
            fh.setFormatter(new SimpleFormatter() {
                	public String format(LogRecord record) {
                	    return String.format("%s %s %s%n", new java.util.Date(), record.getLevel(), record.getMessage());
                	}
            });
            this.addHandler(fh); 
        }
        catch (IOException e) {throw new RuntimeException(e);}
        this.setLevel(loglevel);
    }
    public LoggerOutputStream getOutputStream() {
        return new LoggerOutputStream(this, this.getLevel());
    }
    public LoggerOutputStream getOutputStream(Level level) {
        return new LoggerOutputStream(this, level);
    }
    
    public class LoggerOutputStream extends OutputStream {
        private Logger logger;
        private Level level;
        private StringBuilder buffer;
        
        public LoggerOutputStream(Logger logger, Level level) {
            this.logger = logger;
            this.level = level;
            this.buffer = new StringBuilder();
        }
        @Override public void write(int b) throws IOException {
            if (b == '\n') {
                logger.log(level, buffer.toString());
                buffer.setLength(0);
            }
            else {
                buffer.append((char)b);
            }
        }
        @Override public void close() throws IOException {
            flush();
        }
        @Override public void flush() throws IOException {
            if (buffer.length() > 0) {
                write('\n');
            }
        }    
    }
}

@@ -115,16 +115,50 @@
 andler;%0A
+import java.util.logging.Handler;%0A
 import j
@@ -181,16 +181,16 @@
 .Level;%0A
-
 import j
@@ -929,16 +929,36 @@
 (logfile
+, 10000000, 10, true
 );%0A     
@@ -1356,25 +1356,341 @@
 level);%0A    
-%7D
+    %0A        // close handlers on exit%0A        final Logger thisLogger = this;%0A        Runtime.getRuntime().addShutdownHook(new Thread() %7B%0A            public void run() %7B%0A                for (Handler h : thisLogger.getHandlers()) %7B%0A                    h.close();%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A    
 %0A    public 

remove log lockfiles on exit, rolling logfiles