/*
 * 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.
 */
package br.com.baldaccini.app;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

/**
 *
 * @author baldaccini
 */
public class Log {

    private static File arquivoLog;
    private static FileWriter fw;
    private static PrintWriter pw;
    private static UtilDataHora datahora;

    public static void log(String texto) {
        arquivoLog = new File("logErro.txt");
        datahora = new UtilDataHora();

        if (!arquivoLog.exists() || arquivoLog.isDirectory()) {
            arquivoLog.delete();
            try {
                arquivoLog.createNewFile();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Não foi possível criar o arquivo de log!\n"
                        + "Verifique as permissões das pastas onde o software está instalado!");
            }
        }
        try {
            fw = new FileWriter(arquivoLog, true);
            pw = new PrintWriter(fw);
            pw.append(datahora.dataHora() + ": " + texto);
            pw.append("\n");
            pw.flush();
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException ex) {

        }
    }
}
