/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.data.engine.odaconsumer;

import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.HashMap;

/**
 *	A helper to the odaconsumer package to encapsulate
 *  calls to Logger methods.
 */
class LogHelper
{
    static private HashMap sm_loggerMap = new HashMap();
    private Logger m_logger;
    
    /**
     * Returns a log helper for the given logger name.
     * @param loggerName
     * @return
     */
    static LogHelper getInstance( String loggerName )
    {
        LogHelper aLogHelper = (LogHelper) sm_loggerMap.get( loggerName );
        if ( aLogHelper == null )
        {
            aLogHelper = new LogHelper( loggerName );
            sm_loggerMap.put( loggerName, aLogHelper );
        }
        return aLogHelper;
    }
    
    // wrapper to the java.util.logging Logger of the given loggerName
    private LogHelper( String loggerName )
    {
		m_logger = Logger.getLogger( loggerName );       
    }
    
    boolean isLoggable( Level level )
    {
        return m_logger.isLoggable( level );
    }
    
    boolean isLoggingEnterExitLevel()
    {
        return m_logger.isLoggable( Level.FINER );
    }
    
    void entering( String sourceClass, String sourceMethod )
    {
        m_logger.entering( sourceClass, sourceMethod );
    }

    // Encapsulates handling of parameter object(s).
    
    void entering( String sourceClass, String sourceMethod,
            		int intParam )
    {
        if ( ! isLoggingEnterExitLevel() )
            return;
        
        Object param1 = new Integer( intParam );
        m_logger.entering( sourceClass, sourceMethod, param1 );
    }
    
    void entering( String sourceClass, String sourceMethod,
            		Object param1 )
    {
        m_logger.entering( sourceClass, sourceMethod, param1 );
    }
    
    void entering( String sourceClass, String sourceMethod,
            		Object[] params )
    {
        if ( ! isLoggingEnterExitLevel() )
            return;
        
        // Logger does not like a null Object array
 		if ( params == null )		
 	        m_logger.entering( sourceClass, sourceMethod, "<null>" );
        else
            m_logger.entering( sourceClass, sourceMethod, params );		
    }

    void exiting( String sourceClass, String sourceMethod )
    {
        m_logger.exiting( sourceClass, sourceMethod );        
    }
    
    void exiting( String sourceClass, String sourceMethod,
            		int intParam )
    {
        if ( ! isLoggingEnterExitLevel() )
            return;
        
        Object param1 = new Integer( intParam );
        m_logger.exiting( sourceClass, sourceMethod, param1 );
    }

    void exiting( String sourceClass, String sourceMethod,
            		Object result )
    {
        m_logger.exiting( sourceClass, sourceMethod, result );
    }

    void logp( Level level,
        		  String sourceClass, String sourceMethod,
        		  String msg )
    {
        m_logger.logp( level, sourceClass, sourceMethod, msg );
    }

    void logp( Level level,
        		  String sourceClass, String sourceMethod,
        		  String msg,
        		  Object param1 )
    {
        m_logger.logp( level, sourceClass, sourceMethod, msg, param1 );
    }

    void logp( Level level,
        		  String sourceClass, String sourceMethod,
        		  String msg,
        		  Object[] params )
    {
        if ( ! isLoggable( level ) )
            return;
        
 		if ( params == null )
 	        m_logger.logp( level, sourceClass, sourceMethod, msg, "<null>" );
        else
            m_logger.logp( level, sourceClass, sourceMethod, msg, params );
    }

    void logp( Level level,
        		  String sourceClass, String sourceMethod,
        		  String msg,
        		  Throwable ex )
    {
        assert( ex != null );
        m_logger.logp( level, sourceClass, sourceMethod, msg, ex );       
    }

}
