package game;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by eriks on 15/11/2016.
 */
public class SimpleLogger {
    // Log level constants
    public static final int ALL = 5;
    public static final int DEBUG = 10;
    public static final int INFO = 20;
    public static final int WARN = 30;
    public static final int ERROR = 40;
    public static final int NONE = 100;

    // Log level set for input verification
    private static final Set<Integer> logLevels = new HashSet<Integer>(Arrays.asList(5,10,20,30,40,100));

    private File logPath;
    private int logLevel;

    // Set default log path as /logs/<current date>.log
    private Date date = new Date();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private String strDate = sdf.format(date);
    private String basePath = System.getProperty("user.dir") + "/logs/";
    private String defaultLogFile = strDate + ".log";

    private static SimpleLogger logger = null;

    private SimpleLogger(){
        this.logPath = new File(basePath + defaultLogFile);
        this.logLevel = INFO;
    }

    public static SimpleLogger getLogger() {
        if (logger == null){
            logger = new SimpleLogger();
        }
        return logger;
    }

    public int getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(int logLevel) {
        if (logLevels.contains(logLevel)){
            this.logLevel = logLevel;
        }
    }

    public String getLogFile() {
        return logPath.toString();
    }

    public void setLogFile(String logFile) {
        if(logFile.matches("^[A-Za-z._\\-\\d ]++$")){
            this.logPath = new File(basePath + logFile + "_" + defaultLogFile);
        }
    }

    public void debug(String message){
        if (logLevel <= 10){
            handleWritingFile(logPath, "DEBUG", message);
        }
    }

    public void info(String message){
        if (logLevel <= 20){
            handleWritingFile(logPath, "INFO", message);
        }
    }

    public void warn(String message){
        if (logLevel <= 30){
            handleWritingFile(logPath, "WARNING", message);
        }
    }

    public void error(String message){
        if (logLevel <= 40){
            handleWritingFile(logPath, "ERROR", message);
        }
    }

    private void handleWritingFile(File file, String logLevel ,String input) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String strDate = sdf.format(date);

        BufferedWriter textBuffer = null;
        boolean append = true;

        if (!file.exists()){
            file.getParentFile().mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("Could not create " + file.toString());
                e.printStackTrace();
            }
        }

        try{
            textBuffer = new BufferedWriter(new FileWriter(file,append));
            textBuffer.write(strDate + " " + logLevel + " " + input + "\n");
        } catch (Exception e){
            System.out.println("Could not log to " + file.toString() + ", logging to std out");
            System.out.println(strDate + " " + logLevel + " " + input);
            e.printStackTrace();
        } finally {
            try{
                textBuffer.close();
            } catch (Exception e){
                System.out.println("Could not close " + file.toString());
                e.printStackTrace();
            }
        }
    }
}

@@ -1212,32 +1212,63 @@
  = INFO;%0A    %7D%0A%0A
+    // Singleton instantiating%0A
     public stati
@@ -2721,23 +2721,16 @@
 xtBuffer
- = null
 ;%0A      
@@ -3218,24 +3218,56 @@
 ut + %22%5Cn%22);%0A
+            textBuffer.close();%0A
         %7D ca
@@ -3277,32 +3277,32 @@
  (Exception e)%7B%0A
-
             Syst
@@ -3453,16 +3453,16 @@
 input);%0A
+
         
@@ -3490,241 +3490,8 @@
 ();%0A
-        %7D finally %7B%0A            try%7B%0A                textBuffer.close();%0A            %7D catch (Exception e)%7B%0A                System.out.println(%22Could not close %22 + file.toString());%0A                e.printStackTrace();%0A            %7D%0A
     

Clean up logger file handling