import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TCLogHandler {

	String strAgentDir;
	static BufferedWriter EventLogFileHandle;
	
	
	void Init(String fname)
	{
		FileWriter fr;
		try {
			fr = new FileWriter(fname);
			String line = new String();
			EventLogFileHandle = new BufferedWriter(fr);
			EventLogFileHandle.write("\"Source\",\"Type\",\"Description\",\"Time\"");
			EventLogFileHandle.newLine();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	void CreateLogEntry(String source, int type, String description) throws IOException
	{
		String line = new String();
		line = "\""+source+"\",";
		switch (type)
		{
			case 0:
				line+= "\"Error\",";
				break;
			case 1:
				line+= "\"Warning\",";
				break;
			default:
				line+= "\"Information\",";
		}
		line+="\""+description+"\"";
		EventLogFileHandle.write(line);
		EventLogFileHandle.newLine();
		EventLogFileHandle.flush();
	}
	
	void CreateLogEntry(Exception e, int type) throws IOException
	{
		String strsourcetrace = new String();
		StackTraceElement[] ste = e.getStackTrace();
		for (int i=0;i<ste.length;i++)
		{
			strsourcetrace+=ste[i].getClassName();
			strsourcetrace+=".";
			strsourcetrace+=ste[i].getMethodName()+"\\";
		}
		CreateLogEntry(strsourcetrace,type,e.getMessage());
	}	
	
	void Close() throws IOException
	{
		EventLogFileHandle.flush();
		EventLogFileHandle.close();
	}
}
