package Zyun.Lam.Game.MonsterQuest;

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

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Zyun
 */

//This is just a basic one... Will 
public class SystemLog {
    private static String log = "No Problem";
    static FileWriter logFileOutput = null;
    static BufferedWriter logFileWriter = null;
    //Seceptions that we will need
    public static final int UNKNOWN_EXCEPTION = 0;
    public static final int FILE_NOT_FOUND = 1;
    public static final int PARSING_EXCEPTION = 2;
    public static final int IOEXCEPTION = 3;

    public static void log (String text) {
        //Create Time Stamp
        //Put it into a thread for speed issues. Doesn't affect anything yrt, but it may.
        Runnable thread = () -> {
            Date timeStamp = new Date();
            String content = ("[" + timeStamp + "] " + text);
            try {
                logFileOutput = new FileWriter("system.log", true);
                logFileWriter = new BufferedWriter(logFileOutput);
                logFileWriter.write(content);
                logFileWriter.newLine();
            } catch (IOException e) {
                //No point killing the whole program for the sake of a logging problem...
                e.printStackTrace();
            }
            
            try {
                if (logFileWriter != null)
                    logFileWriter.close();
                if (logFileOutput != null)
                    logFileOutput.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            //Clear these variables, just in case
            logFileOutput = null;
            logFileWriter = null;
            
        };
        Thread print = new Thread (thread);
        print.start();
    }
    
    public static void startNewLog () {
            //Create Time Stamp
            Date timeStamp = new Date();
            String content = (" --- NEW LOG STARTED [" + timeStamp + "] ---");
            try {
                logFileOutput = new FileWriter("system.log", true);
                logFileWriter = new BufferedWriter(logFileOutput);
                logFileWriter.newLine();
                logFileWriter.write(content);
                logFileWriter.newLine();
            } catch (IOException e) {
                //As the file is probally going to fail to do do anything to the file, no writing to file...
                e.printStackTrace();
            } finally {
                try {
                    if (logFileWriter != null)
                        logFileWriter.close();
                    if (logFileOutput != null)
                        logFileOutput.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                //Clear these variables, just in case
                logFileOutput = null;
                logFileWriter = null;
                
            }
    }
        
        static void clearFile () {
            File logFile = new File ("system.log");
            //If log file does not exist, who cares, as we will make it later
            if (logFile.exists()) {
                if (logFile.length() > 102400) { //Size of 100 kb, will delete the file if so
                    if (!logFile.delete()) {
                        //Well, something is wrong, as it should be able to delete. Will there have a exception?
                        //Anyway, report it
                        System.err.println("Unable to delete log file");
                    } else {
                        try {
                            logFile.createNewFile();
                            log("Cleared file of things.");
                            System.out.println("Zyun.Lam.Game.MonsterQuest.SystemLog.clearFile() : Cleared file of things");
                        } catch (IOException ioe) {
                            System.err.println("Unable to open file: " + ioe.getMessage());
                            //Can't write to log files.
                        }
                    }
                }
            }
        }
    public static void log (String text, int errorNumber) {
        Runnable thread = () -> {
            //Create Time Stamp
            Date timeStamp = new Date();
            String content = ("[" + timeStamp + "] (" + errorNumber + "): " + text);
            try {
                logFileOutput = new FileWriter("system.log", true);
                logFileWriter = new BufferedWriter(logFileOutput);
                logFileWriter.write(content);
                logFileWriter.newLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (logFileWriter != null)
                    logFileWriter.close();
                if (logFileOutput != null)
                    logFileOutput.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
        };
        Thread print = new Thread (thread);
        print.start();
    }
}
@@ -508,102 +508,8 @@
 %22;%0D%0A
-    static FileWriter logFileOutput = null;%0D%0A    static BufferedWriter logFileWriter = null;%0D%0A
     
@@ -540,16 +540,16 @@
 l need%0D%0A
+
     publ
@@ -781,24 +781,34 @@
 ng text) %7B%0D%0A
+        %0D%0A
         //Cr
@@ -942,32 +942,128 @@
 read = () -%3E %7B%0D%0A
+            FileWriter logFileOutput = null;%0D%0A            BufferedWriter logFileWriter = null;%0D%0A
             Date
@@ -2148,24 +2148,120 @@
 ewLog () %7B%0D%0A
+            FileWriter logFileOutput = null;%0D%0A            BufferedWriter logFileWriter = null;%0D%0A
             
@@ -4673,32 +4673,128 @@
 read = () -%3E %7B%0D%0A
+            FileWriter logFileOutput = null;%0D%0A            BufferedWriter logFileWriter = null;%0D%0A
             //Cr
@@ -5666,9 +5666,11 @@
 %0A    %7D%0D%0A
-
 %7D
+%0D%0A

Update SystemLog.java