package dataWarehousingTools;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class LogFile 
{
	private FileWriter file;
	private String fileName;
	private Calendar today;
	private DateFormat longDateFormat;
	private DateFormat shortDateFormat;
	private String lineSeparator;
		
	public LogFile(Configuration configuration, String fileNameRoot)
	{
    	today = Calendar.getInstance();
    	shortDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    	longDateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
		
    	fileName = new String
    	(
    		configuration.getLogFileLocation() +
    		fileNameRoot + 
    		shortDateFormat.format(today.getTime()) +
    		".log"
    	);
    	
		try 
		{
			file = new FileWriter(fileName);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		lineSeparator = new String(System.getProperty("line.separator"));

		logMessage("Start");
	}
	
	public void logMessage (String message)
	{
		try 
		{
	    	today = Calendar.getInstance();
			file.write(longDateFormat.format(today.getTime()) + " " + message + lineSeparator);
			file.flush();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void Flush()
	{
		try 
		{
			file.flush();
		} 
		catch (IOException e) 
		{
			System.out.println("Could not flush log file");
			e.printStackTrace();
		}
	}
	
	public void Close()
	{
		try 
		{
			file.close();
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
