package com.tofly.ghrq_route.common.util.logger;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;

public class LogWriter {
	private static LogWriter mLogWriter;
	private BufferedWriter mWriter;
	private SimpleDateFormat df;
	
	private LogWriter() {
	}
	
	public synchronized static LogWriter getInstance(){
		if(mLogWriter==null)
			mLogWriter=new LogWriter();
		return mLogWriter;
	}
	@SuppressLint("SimpleDateFormat")
//	public static LogWriter open(String file_path) throws IOException {
//		mLogWriter = new LogWriter(file_path);
//		mWriter = new BufferedWriter(new FileWriter(mPath,true), 2048);
//		if(df==null)
//		df = new SimpleDateFormat("[yyyy/MM/dd,HH:mm:ss]");
//		
//		return mLogWriter;
//	}
	
	public boolean open(String filePath){
		try {
			mWriter = new BufferedWriter(new FileWriter(filePath,true), 1024);
			df = new SimpleDateFormat("[yyyy/MM/dd,HH:mm:ss]");
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public void close() throws IOException {
		mWriter.close();
		mWriter=null;
		df=null;
	}

	public void print(String log,String tag) throws IOException {
		StringBuilder stringBuilder=new StringBuilder(df.format(new Date()));
		stringBuilder.append("["+tag+"] ");
		stringBuilder.append(log);
		stringBuilder.append("\n");
		mWriter.write(stringBuilder.toString());
		
		mWriter.flush();
		stringBuilder=null;
		log=null;
		tag=null;
	}

	public void print(String log) throws IOException {
		StringBuilder stringBuilder=new StringBuilder(df.format(new Date()));
		stringBuilder.append(log);
		stringBuilder.append("\n");
		mWriter.write(stringBuilder.toString());
		
		mWriter.flush();
		
		stringBuilder=null;
		log=null;
	}
}

