package net.exodiusmc.blasteroids;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import net.exodiusmc.blasteroids.enums.LogLevel;

public class Logger {
	private static String filename = "log.txt";
	private static Logger instance;
	private File logfile;
	private BufferedWriter writer;
	
	public static Logger getLogger() {
		if(instance == null) {
			instance = new Logger();
		}
		
		return instance;
	}
	
	private Logger() {
		this.logfile = new File(filename);
		
		if(!logfile.exists()) {
			try {
				logfile.createNewFile();
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
		
		try {
			this.writer = new BufferedWriter(new FileWriter(logfile, true));
		} catch(IOException e) {
			e.printStackTrace();
		}
		
		try {
			this.writer.newLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void setLogDestination(String name) {
		filename = name;
	}
	
	@SuppressWarnings("deprecation")
	public void log(String message, LogLevel... tags) {
		Date d = new Date();
		String msg = "[" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "] ";
		boolean error = false;
		
		for(LogLevel tag : tags) {
			msg += tag.asTag() + " ";
			
			if(tag.equals(LogLevel.ERROR)) error = true;
		}
		
		msg += message;
		
		if(error) {
			System.err.println(msg);
		} else {
			System.out.println(msg);
		}
		
		try {
			writer.write(msg);
			writer.newLine();
			writer.flush();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public void warn(String message) {
		this.log(message, LogLevel.WARNING);
	}
	
	public void error(String message) {
		this.log(message, LogLevel.ERROR);
	}
	
	public void info(String message) {
		this.log(message, LogLevel.INFO);
	}
}

@@ -487,16 +487,50 @@
 e;%0A%09%7D%0A%09%0A
+%09@SuppressWarnings(%22deprecation%22)%0A
 %09private
@@ -778,20 +778,52 @@
 ogfile, 
-true
+logfile.length() %3C 20480 /* 20 KB */
 ));%0A%09%09%7D 
@@ -876,32 +876,244 @@
 %0A%09%09%7D%0A%09%09%0A%09%09try %7B%0A
+%09%09%09Date d = new Date();%0A%09%09%09this.writer.newLine();%0A%09%09%09this.writer.write(%22********** Loading Blasteroids %22 + Main.getVersion() + %22 (%22 + d.getHours() + %22:%22 + d.getMinutes() + %22:%22 + d.getSeconds() + %22) **********%22);%0A
 %09%09%09this.writer.n

Clear log after 20KB