/*******************************************************************************
 *  Copyright 2017-2018 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.dcm30_04.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;

import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

//TODO: Auto-generated Javadoc
/**
 * DicomLogger is used to log messages for exceptions generated.
 * Logging can be enabled or disabled using the {@link DicomConfig} class.
 */
public class DicomLogger
{

	private String strLogFile ;

	/**
	 * Specifies name of log file.
	 */
	public static final String DICOM_LOGGER_NAME = "DICOM_LOGGER_V3"; 

	/**
	 * the object dicom logger.
	 */
	private static  DicomLogger objDicomLogger = null;

	/**
	 * the object logger.
	 */
	private static Logger objLogger = null;


	/**
	 * @return String
	 */
	public String getLogFile()
	{
		return strLogFile;
	}

	/**
	 * This is the private constructor for DicomLogger.
	 */
	private DicomLogger()
	{
		try
		{
			objLogger = Logger.getLogger(DICOM_LOGGER_NAME);

			String tempFileName = StandardMethods.generateUUID() + ".log";
			String strPath = StandardMethods.getTempDirectoryStorage() + "CDAC-SDK/DCM-J/Logs";
			File logDir =  new File(strPath);

			logDir.mkdirs();
			strLogFile = logDir.getPath() +"/"+ tempFileName;
			{
				// System.out.println("Log file path "+ strLogFile);
			}

			FileHandler objFileHandler = new FileHandler(strLogFile, true);
			objFileHandler.setFormatter(new SimpleFormatter());
			objLogger.addHandler(objFileHandler);
		}
		catch(Exception e)
		{			
			throw new LoggerException(ResourceBundleWrapper.getValue("exceptions", "LoggingException", new Locale(DicomConfig.createInstance().getLocale())));
		}
	}

	
	/**
	 * This is the private constructor for DicomLogger.
	 */
	private DicomLogger(String strLogFile)
	{
		try
		{
			objLogger = Logger.getLogger(DICOM_LOGGER_NAME);
			this.strLogFile = strLogFile; 
			FileHandler objFileHandler = new FileHandler(strLogFile, true);
			objFileHandler.setFormatter(new SimpleFormatter());
			objLogger.addHandler(objFileHandler);
		}
		catch(Exception e)
		{			
			throw new LoggerException(ResourceBundleWrapper.getValue("exceptions", "LoggingException", new Locale(DicomConfig.createInstance().getLocale())));
		}
	}
	
	
	/**
	 * Retrieves empty instance of DicomLogger enabled for logging in the error log file.
	 * @return DicomLogger - instance of DicomLogger.
	 */
	public static DicomLogger getLogger()
	{
		if(objDicomLogger == null)
			objDicomLogger = new DicomLogger();
		return objDicomLogger;
	}

	
	/**
	 * Retrieves empty instance of DicomLogger enabled for logging in the error log file.
	 * @return DicomLogger - instance of DicomLogger.
	 */
	public static DicomLogger getLogger(String strLogFile)
	{
		if(objDicomLogger == null)
			objDicomLogger = new DicomLogger(strLogFile);
		return objDicomLogger;
	}
	/**
	 * Logs the exception with the level specified. 
	 * @param ide Exception to be logged.
	 */
	public void log(Exception ide)
	{
		if(DicomConfig.createInstance().getLoggingLevel() == EnumLoggingLevel.SEVERE)
			objLogger.log(Level.SEVERE,ide.getMessage(),ide);
		else
			objLogger.log(Level.INFO,ide.getMessage(),ide);
	}

	/**
	 * Logs the error  message with the level specified. 
	 * @param strLogMessage Message to be logged.
	 */
	public void log(String strLogMessage)
	{
		if(DicomConfig.createInstance().getLoggingLevel() == EnumLoggingLevel.SEVERE)
			objLogger.log(Level.SEVERE,strLogMessage);
		else
			objLogger.log(Level.INFO,strLogMessage);
	}
	

	/**
	 * Logs the error  message with the level specified. 
	 * @param sourceClass name of class that issued the logging request.
	 * @param sourceMethod name of method that issued the logging request.
	 * @param msg The string message (or a key in the message catalog).
	 * @param e instance of Exception .
	 */
	public void log(String sourceClass, String sourceMethod,
			String msg,Exception e) 
	{
		if(DicomConfig.createInstance().getLoggingLevel() == EnumLoggingLevel.SEVERE)
			objLogger.logp(Level.SEVERE, sourceClass, sourceMethod,msg,e);
		else
			objLogger.logp(Level.INFO, sourceClass, sourceMethod,msg,e);
	}
}
