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

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 {
            //pro test na SERVER
            //souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02log.txt", true)); //true - pro dalsi zapis
            //pro TEST na lokalnim PC
            souborLogu = new BufferedWriter(new FileWriter("xdvora02log.txt", true));
        } 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);
        } finally {
            try {
                souborLogu.close();
            } catch (IOException ex) {
                Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    //metoda na smazani logu pri zapnuti programu
    public static void smazSouborLog() {
        try {
            //pro TEST na lokalnim PC
            Writer souborLogu = new BufferedWriter(new FileWriter("xdvora02log.txt"));
            //pro na SERVER
            //Writer souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02log.txt"));
            souborLogu.write("");
        } catch (IOException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void zapisDoLogu(Exception chyba) {
        Writer souborLogu = null;
        try {
            //pro TEST na lokalnim PC
            souborLogu = new BufferedWriter(new FileWriter("xdvora02log.txt", true)); //true - pro dalsi zapis
            //pro test na SERVER
            //souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02log.txt", 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);
        } finally {
            try {
                souborLogu.close();
            } catch (IOException ex) {
                Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}