package org.bdgp.MMSlide;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
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 LogFileHandler(logfile);
            this.addHandler(fh); 
        }
        catch (IOException e) {throw new RuntimeException(e);}
        this.setLevel(loglevel);
        
        // close handlers on exit
        final Logger thisLogger = this;
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                for (Handler h : thisLogger.getHandlers()) {
                    h.close();
                }
            }
        });
    }
    
    public LoggerOutputStream getOutputStream() {
        return new LoggerOutputStream(this, this.getLevel());
    }
    public LoggerOutputStream getOutputStream(Level level) {
        return new LoggerOutputStream(this, level);
    }
    
    public static class LogFileHandler extends FileHandler {
    	public LogFileHandler(String pattern) 
            throws IOException, SecurityException 
        {
    		super(pattern, 10000000, 10, true);
    		setFormatter();
    	}
		public LogFileHandler(String pattern, int limit, int count, boolean append) 
            throws IOException, SecurityException 
        {
			super(pattern, limit, count, append);
    		setFormatter();
		}
		public void setFormatter() {
			setFormatter(new SimpleFormatter() {
				public String format(LogRecord record) {
					return String.format("[%s:%s:%s] %s%n", 
							record.getLoggerName(),
							new Date(record.getMillis()), 
							record.getLevel(), 
							record.getMessage());
				}
			});
		}
    }
    
    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');
            }
        }    
    }
}

@@ -881,24 +881,59 @@
 rce, null);%0A
+        if (logfile != null) %7B%0A    
         try 
@@ -935,16 +935,20 @@
  try %7B %0A
+    
         
@@ -1005,24 +1005,28 @@
             
+    
 this.addHand
@@ -1035,34 +1035,42 @@
 r(fh); %0A        
+   
+ 
 %7D%0A
+    
         catch (I
@@ -1112,24 +1112,34 @@
 eption(e);%7D%0A
+        %7D%0A
         this

allow logger which doesn’t log to file