/*******************************************************************************
 *  Copyright 2017 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.hl7282.utils;

import java.io.File;
import java.util.Locale;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 *  HL7Logger is used to log messages for warning generated.
 *  Logging can be enabled or disabled using the  class. 
 */
public class HL7Logger
{
	private String logFile ;
	/**
	 * HL7 logger Name
	 */
	public static final String HL7_LOGGER_NAME = "HL7_LOGGER"; 
	private static  HL7Logger objHL7Logger = null;
	private static Logger objLogger = null;

	/**
	 * This is the private contructor for DicomLogger.
	 */
	private HL7Logger()
	{
		try
		{
			objLogger = Logger.getLogger(HL7_LOGGER_NAME);

			String tempFileName = StandardMethods.generateUUID() + ".log";
			String strPath = StandardMethods.getTempDirectoryStorage() + "CDAC-SDK/HL7-J/Logs";
			File logDir =  new File(strPath);
			logDir.mkdirs(); 
			logFile = logDir.getPath() +File.separator+ tempFileName;
			{
				// System.out.println("Log file path "+ strLogFile);
			}

			FileHandler objFileHandler = new FileHandler(logFile, true);
			objFileHandler.setFormatter(new SimpleFormatter());
			objLogger.addHandler(objFileHandler);
		}
		catch(Exception e)
		{			
			throw new LoggerException(ResourceBundleWrapper.getValue("exceptions", "LoggerException", new Locale(HL7Config.createInstance().getLocale())));
		}
	}
	
	
	/**
	 * This is the private contructor for HL7Logger.
	 */
	private HL7Logger(String strLogFile)
	{
		try
		{
			objLogger = Logger.getLogger(HL7_LOGGER_NAME);
			this.logFile = strLogFile; 
			FileHandler objFileHandler = new FileHandler(strLogFile, true);
			objFileHandler.setFormatter(new SimpleFormatter());
			objLogger.addHandler(objFileHandler);
		}
		catch(Exception e)
		{
			throw new LoggerException(ResourceBundleWrapper.getValue("exceptions", "LoggerException", new Locale(HL7Config.createInstance().getLocale())));
		}
	}

	/**
	 * Returns empty instance of HL7Logger enabled for logging in the error log file.
	 * @return HL7Logger - instance of HL7Logger.
	 */
	public static HL7Logger getLogger()
	{
		if(objHL7Logger == null)
			objHL7Logger = new HL7Logger();
		return objHL7Logger;
	}
	
	/**
	 * Returns empty instance of HL7Logger enabled for logging in the error log file.
	 * @param strLogFile File in which logging to be done
	 * @return HL7Logger - instance of HL7Logger.
	 */
	public static HL7Logger getLogger(String strLogFile)
	{
		if(objHL7Logger == null)
			objHL7Logger = new HL7Logger(strLogFile);
		return objHL7Logger;
	}

	/**
	 * Logs the given Message with Level specified.
	 * @param objLevel Level to be specified
	 * @param strLogMessage Log message 
	 */
	public void log(Level objLevel,String strLogMessage)
	{
		objLogger.log(objLevel, strLogMessage);
	}

	/**
	 * Logs the given Message with Level Severe.
	 * @param strLogMessage Log message 
	 */
	public void log(String strLogMessage)
	{
		objLogger.log(Level.SEVERE, strLogMessage);
	}
	
	/**
	 * Logs the exception with the level severe. 
	 * @param ide Exception to be logged.
	 */
	public void log(Exception ide)
	{
		//objLogger.log(Level.SEVERE,ide.getMessage(),ide);
		objLogger.log(Level.SEVERE, ide.getMessage());
		Exception ex = (Exception)ide.getCause();
		if(ex != null)
			log(ex);
	}

	/**
	 * Get the log file.
	 * @return String - The log file.
	 */
	public String getLogFile()
	{
		return logFile;
	}
}
