<commit_before>/**
 * Copyright (c) 2010 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.sonatype.guice.bean.reflect;

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

/**
 * Utility methods for dealing with logs.
 */
public final class Logs
{
    // ----------------------------------------------------------------------
    // Constants
    // ----------------------------------------------------------------------

    private static final boolean DEBUG_ENABLED;

    private static final boolean SLF4J_ENABLED;

    private static final String ANCHOR = "{}";

    static
    {
        boolean debugEnabled = false;
        boolean slf4jEnabled = true;
        try
        {
            debugEnabled = org.slf4j.LoggerFactory.getLogger( Logs.class ).isDebugEnabled();
        }
        catch ( final Throwable e )
        {
            debugEnabled = Logger.getLogger( Logs.class.getName() ).isLoggable( Level.FINE );
            slf4jEnabled = false;
        }
        DEBUG_ENABLED = debugEnabled;
        SLF4J_ENABLED = slf4jEnabled;
    }

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    private Logs()
    {
        // static utility class, not allowed to create instances
    }

    static
    {
        new Logs(); // keep Cobertura coverage happy
    }

    // ----------------------------------------------------------------------
    // Utility methods
    // ----------------------------------------------------------------------

    public static void debug( final Class<?> clazz, final String fmt, final Object o1, final Object o2 )
    {
        if ( DEBUG_ENABLED )
        {
            if ( SLF4J_ENABLED )
            {
                org.slf4j.LoggerFactory.getLogger( clazz ).debug( fmt, o1, o2 );
            }
            else
            {
                Logger.getLogger( clazz.getName() ).fine( format( format( fmt, o1 ), o2 ) );
            }
        }
    }

    public static void warn( final Class<?> clazz, final String msg, final Throwable e )
    {
        if ( SLF4J_ENABLED )
        {
            org.slf4j.LoggerFactory.getLogger( clazz ).warn( msg, e );
        }
        else
        {
            Logger.getLogger( clazz.getName() ).log( Level.WARNING, msg, e );
        }
    }

    // ----------------------------------------------------------------------
    // Implementation methods
    // ----------------------------------------------------------------------

    private static String format( final String fmt, final Object o )
    {
        return fmt.replaceFirst( ANCHOR, String.valueOf( o ) );
    }<commit_msg>Javadoc<commit_after>/**
 * Copyright (c) 2010 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.sonatype.guice.bean.reflect;

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

/**
 * Utility methods for dealing with internal container logs.
 */
public final class Logs
{
    // ----------------------------------------------------------------------
    // Constants
    // ----------------------------------------------------------------------

    private static final boolean DEBUG_ENABLED;

    private static final boolean SLF4J_ENABLED;

    private static final String ANCHOR = "{}";

    static
    {
        boolean debugEnabled = false;
        boolean slf4jEnabled = true;
        try
        {
            debugEnabled = org.slf4j.LoggerFactory.getLogger( Logs.class ).isDebugEnabled();
        }
        catch ( final Throwable e )
        {
            debugEnabled = Logger.getLogger( Logs.class.getName() ).isLoggable( Level.FINE );
            slf4jEnabled = false;
        }
        DEBUG_ENABLED = debugEnabled;
        SLF4J_ENABLED = slf4jEnabled;
    }

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    private Logs()
    {
        // static utility class, not allowed to create instances
    }

    static
    {
        new Logs(); // keep Cobertura coverage happy
    }

    // ----------------------------------------------------------------------
    // Utility methods
    // ----------------------------------------------------------------------

    /**
     * Formats and logs the given debug message under the given context class
     * 
     * @param clazz The context class
     * @param format The debug message format
     * @param arg1 First object to format
     * @param arg2 Second object to format
     */
    public static void debug( final Class<?> clazz, final String format, final Object arg1, final Object arg2 )
    {
        if ( DEBUG_ENABLED )
        {
            if ( SLF4J_ENABLED )
            {
                org.slf4j.LoggerFactory.getLogger( clazz ).debug( format, arg1, arg2 );
            }
            else
            {
                Logger.getLogger( clazz.getName() ).fine( format( format( format, arg1 ), arg2 ) );
            }
        }
    }

    /**
     * Logs the given warning message and cause under the given context class
     * 
     * @param clazz The context class
     * @param message The warning message
     * @param cause The cause
     */
    public static void warn( final Class<?> clazz, final String message, final Throwable cause )
    {
        if ( SLF4J_ENABLED )
        {
            org.slf4j.LoggerFactory.getLogger( clazz ).warn( message, cause );
        }
        else
        {
            Logger.getLogger( clazz.getName() ).log( Level.WARNING, message, cause );
        }
    }

    // ----------------------------------------------------------------------
    // Implementation methods
    // ----------------------------------------------------------------------

    /**
     * Replaces the first available formatting anchor with the given object.
     * 
     * @param format The format string
     * @param arg The object to format
     */
    private static String format( final String format, final Object arg )
    {
        int cursor = format.indexOf( ANCHOR );
        if ( cursor < 0 )
        {
            return format;
        }
        final StringBuilder buf = new StringBuilder();
        if ( cursor > 0 )
        {
            buf.append( format.substring( 0, cursor ) );
        }
        buf.append( arg );
        cursor += ANCHOR.length();
        if ( cursor < format.length() )
        {
            buf.append( format.substring( cursor, format.length() ) );
        }
        return buf.toString();
    }
{'fraction_non_alphanumeric': 0.21892888945426792, 'fraction_numerical': 0.008777509222745198, 'mean_word_length': 1.3021961932650072, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6546361', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1856, 'n_words': 633}