/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vypocetObalky;

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 {
            // nastaveni k cesty k souboru - prvni radek pro TEST na lokalnim PC
            // druhy radek pro testovani na SERVERU
                souborLogu = new BufferedWriter(new FileWriter("xdvora02log.txt", true)); //true - pro dalsi zapis
           //   souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/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);
        } 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 SERVERU
            //Writer souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/xdvora02/xdvora02log.txt"));
            //Pro TEST na lokalnim PC
            Writer souborLogu = new BufferedWriter(new FileWriter("xdvora02log.txt")); //true - pro dalsi zapis
            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 SERVERU
            //souborLogu = new BufferedWriter(new FileWriter("/home/cesnet_vutbr2/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.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);
            }
        }

    }
}