/*
Vytvori logovaci soubory a do nich zaznamena mimoradne udalosti
 */
package tcpserver;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Filip Dvorak
 */
public class Log {

    //zapise do logu
    public static void zapisDoLogu(String chyba) {
        Writer souborLogu = null;
        try {
            
            souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02logTCPServer.txt", true)); //true - pro dalsi zapis
            //souborLogu = new BufferedWriter(new FileWriter("/var/www/html/bachsoft/public/methods/xdvora02/xdvora02log.txt",true)); //true - pro dalsi zapis
        } catch (IOException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            souborLogu.write(chyba + "\n -------------------------------------------------------------- \n");
        } catch (IOException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
            Log.zapisDoLogu(ex);
        } finally {
            try {
                souborLogu.close();
            } catch (IOException ex) {
                Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
                Log.zapisDoLogu(ex);
            }
        }

    }

    //metoda na smazani logu pri yapnuti programu
    public static void smazSouborLog() {
        try {
           Writer souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02logTCPServer.txt"));
          //  Writer souborLogu = new BufferedWriter(new FileWriter("/var/www/html/bachsoft/public/methods/xdvora02/xdvora02logTCP",true));
            souborLogu.write("");
        } catch (IOException ex) {
             Log.zapisDoLogu("Nepodarilo se smazat log soubor");
             Log.zapisDoLogu(ex);
        }
    }

    public static void zapisDoLogu(Exception chyba) {
        Writer souborLogu = null;
        try {
            souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02logTCPServer.txt",true)); //true - pro dalsi zapis
           //souborLogu = new BufferedWriter(new FileWriter("/var/www/html/bachsoft/public/methods/xdvora02/xdvora02logTCP",true));
        } catch (IOException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            souborLogu.write(chyba.getMessage() + "\n");
            for (StackTraceElement prvek : chyba.getStackTrace()) {
                souborLogu.write(prvek.toString() + "\n");
            }
            souborLogu.write("\n" + "--------------------------------------------------------------" + "\n");
        } catch (IOException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
            Log.zapisDoLogu(chyba);
        } finally {
            try {
                souborLogu.close();
            } catch (IOException ex) {
                Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
                Log.zapisDoLogu(chyba);
            }
        }

    }
}