package fr.univ.nantes.logger;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Alexis Giraudet
 * @author Thomas Minier
 */
public class LogWriter {
    private static Map<String, Integer> calls;
    private static PrintWriter fileWriter;

    public static void writeLog() {
        try {
            PrintWriter writer = getWriter();
            if (calls != null && !calls.isEmpty()) {
                for (Map.Entry<String, Integer> entry : calls.entrySet()) {
                    writer.write(entry.getKey() + ": " + entry.getValue() + "\n");
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        if (fileWriter != null) {
            fileWriter.close();
        }
    }

    public static void out(String string, boolean error) {
        try {
            PrintWriter writer = getWriter();

            if (error) {
                writer.write("ERROR: ");
            } else {
                writer.write("INFO: ");
            }
            writer.write(string + "\n");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void call(String method) {
        if (calls == null) {
            calls = new HashMap<String, Integer>();
        }

        Integer count = calls.get(method);
        if (count == null) {
            count = 0;
        } else {
            count++;
        }
        calls.put(method, count);
    }

    protected static PrintWriter getWriter() throws FileNotFoundException {
        if (fileWriter == null) {
            ShutdownHookLog shutdownHook = new ShutdownHookLog();
            Runtime.getRuntime().addShutdownHook(shutdownHook);
            fileWriter = new PrintWriter("log");
        }
        return fileWriter;
    }
}

@@ -272,16 +272,65 @@
  calls;%0A
+    private static ShutdownHookLog shutdownHook;%0A
     priv
@@ -427,39 +427,19 @@
         
-PrintWriter writer = ge
+ini
 tWriter(
@@ -474,28 +474,8 @@
 null
- && !calls.isEmpty()
 ) %7B%0A
@@ -562,33 +562,37 @@
                 
-w
+fileW
 riter.write(entr
@@ -929,31 +929,11 @@
     
-PrintWriter writer = ge
+ini
 tWri
@@ -973,33 +973,37 @@
                 
-w
+fileW
 riter.write(%22ERR
@@ -1039,33 +1039,37 @@
                 
-w
+fileW
 riter.write(%22INF
@@ -1101,17 +1101,21 @@
         
-w
+fileW
 riter.wr
@@ -1269,24 +1269,45 @@
 g method) %7B%0A
+        initHook();%0A%0A
         if (
@@ -1595,60 +1595,23 @@
 tic 
-PrintWriter getWriter() throws FileNotFoundException
+void initHook()
  %7B%0A 
@@ -1621,26 +1621,28 @@
     if (
-fileWriter
+shutdownHook
  == null
@@ -1657,32 +1657,16 @@
         
-ShutdownHookLog 
 shutdown
@@ -1771,41 +1771,54 @@
     
+%7D%0A
     
-fileWriter = new Pr
+%7D%0A%0A    protected static void 
 in
+i
 tWriter(
 %22log
@@ -1817,52 +1817,133 @@
 ter(
-%22log%22);%0A        %7D%0A        return fileWriter;
+) throws FileNotFoundException %7B%0A        if (fileWriter == null) %7B%0A            fileWriter = new PrintWriter(%22log%22);%0A        %7D
 %0A   

fix LogWriter: init hook properly