Applied-Energistics-2-tiler.../src/main/java/appeng/core/AELog.java

389 lines
9.7 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-09-24 02:26:27 +02:00
package appeng.core;
import javax.annotation.Nonnull;
2015-06-16 02:44:59 +02:00
import org.apache.logging.log4j.Level;
2015-09-29 15:47:55 +02:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
2015-09-29 15:47:55 +02:00
import net.minecraft.util.math.BlockPos;
2014-12-29 21:59:05 +01:00
2014-09-24 02:26:27 +02:00
import appeng.core.features.AEFeature;
import appeng.tile.AEBaseTile;
import appeng.util.Platform;
2015-03-26 12:25:44 +01:00
public final class AELog
2014-09-24 02:26:27 +02:00
{
private static final String LOGGER_PREFIX = "AE2:";
private static final String SERVER_SUFFIX = "S";
private static final String CLIENT_SUFFIX = "C";
private static final Logger SERVER = LogManager.getFormatterLogger( LOGGER_PREFIX + SERVER_SUFFIX );
private static final Logger CLIENT = LogManager.getFormatterLogger( LOGGER_PREFIX + CLIENT_SUFFIX );
private static final String BLOCK_UPDATE = "Block Update of %s @ ( %s )";
private static final String DEFAULT_EXCEPTION_MESSAGE = "Exception: ";
2014-09-24 02:26:27 +02:00
private AELog()
2014-09-24 02:26:27 +02:00
{
}
/**
* Returns a {@link Logger} logger suitable for the effective side (client/server).
*
* @return a suitable logger instance
*/
private static Logger getLogger()
2014-09-24 02:26:27 +02:00
{
return Platform.isServer() ? SERVER : CLIENT;
2014-09-24 02:26:27 +02:00
}
/**
* Indicates of the global log is enabled or disabled.
*
* By default it is enabled.
*
* @return true when the log is enabled.
*/
public static boolean isLogEnabled()
2014-09-24 02:26:27 +02:00
{
return AEConfig.instance == null || AEConfig.instance.isFeatureEnabled( AEFeature.Logging );
}
/**
* Logs a formatted message with a specific log level.
*
* This uses {@link String#format(String, Object...)} as opposed to the {@link ParameterizedMessage} to allow a more
* flexible formatting.
*
* The output can be globally disabled via the configuration file.
*
* @param level the intended level.
* @param message the message to be formatted.
* @param params the parameters used for {@link String#format(String, Object...)}.
*/
public static void log( @Nonnull final Level level, @Nonnull final String message, final Object... params )
{
if( AELog.isLogEnabled() )
{
final String formattedMessage = String.format( message, params );
final Logger logger = getLogger();
logger.log( level, formattedMessage );
}
2014-09-24 02:26:27 +02:00
}
/**
* Log an exception with a custom message formated via {@link String#format(String, Object...)}
*
* Similar to {@link AELog#log(Level, String, Object...)}.
*
* @see AELog#log(Level, String, Object...)
*
* @param level the intended level.
* @param exception
* @param message the message to be formatted.
* @param params the parameters used for {@link String#format(String, Object...)}.
*/
public static void log( @Nonnull final Level level, @Nonnull final Throwable exception, @Nonnull String message, final Object... params )
2014-09-24 02:26:27 +02:00
{
if( AELog.isLogEnabled() )
{
final String formattedMessage = String.format( message, params );
final Logger logger = getLogger();
logger.log( level, formattedMessage, exception );
}
2014-09-24 02:26:27 +02:00
}
/**
* @see AELog#log(Level, String, Object...)
* @param format
* @param params
*/
public static void info( @Nonnull final String format, final Object... params )
2014-09-24 02:26:27 +02:00
{
log( Level.INFO, format, params );
}
/**
* Log exception as {@link Level#INFO}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
*/
public static void info( @Nonnull final Throwable exception )
{
log( Level.INFO, exception, DEFAULT_EXCEPTION_MESSAGE );
}
/**
* Log exception as {@link Level#INFO}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
* @param message
*/
public static void info( @Nonnull final Throwable exception, @Nonnull final String message )
{
log( Level.INFO, exception, message );
}
/**
* @see AELog#log(Level, String, Object...)
* @param format
* @param params
*/
public static void warn( @Nonnull final String format, final Object... params )
{
log( Level.WARN, format, params );
}
/**
* Log exception as {@link Level#WARN}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
*/
public static void warn( @Nonnull final Throwable exception )
{
log( Level.WARN, exception, DEFAULT_EXCEPTION_MESSAGE );
}
/**
* Log exception as {@link Level#WARN}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
* @param message
*/
public static void warn( @Nonnull final Throwable exception, @Nonnull final String message )
{
log( Level.WARN, exception, message );
}
/**
* @see AELog#log(Level, String, Object...)
* @param format
* @param params
*/
public static void error( @Nonnull final String format, final Object... params )
{
log( Level.ERROR, format, params );
}
/**
* Log exception as {@link Level#ERROR}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
*/
public static void error( @Nonnull final Throwable exception )
{
log( Level.ERROR, exception, DEFAULT_EXCEPTION_MESSAGE );
}
/**
* Log exception as {@link Level#ERROR}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
* @param message
*/
public static void error( @Nonnull final Throwable exception, @Nonnull final String message )
{
log( Level.ERROR, exception, message );
}
/**
* Log message as {@link Level#DEBUG}
*
* @see AELog#log(Level, String, Object...)
* @param format
* @param data
*/
public static void debug( @Nonnull final String format, final Object... data )
{
if( AELog.isDebugLogEnabled() )
{
log( Level.DEBUG, format, data );
}
}
/**
* Log exception as {@link Level#DEBUG}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
*/
public static void debug( @Nonnull final Throwable exception )
{
if( AELog.isDebugLogEnabled() )
{
log( Level.DEBUG, exception, DEFAULT_EXCEPTION_MESSAGE );
}
}
/**
* Log exception as {@link Level#DEBUG}
*
* @see AELog#log(Level, Throwable, String, Object...)
*
* @param exception
* @param message
*/
public static void debug( @Nonnull final Throwable exception, @Nonnull final String message )
{
if( AELog.isDebugLogEnabled() )
2014-09-24 02:26:27 +02:00
{
log( Level.DEBUG, exception, message );
2014-09-24 02:26:27 +02:00
}
}
/**
* Use to check for an enabled debug log.
*
* Can be used to prevent the execution of debug logic.
*
* @return true when the debug log is enabled.
*/
public static boolean isDebugLogEnabled()
{
return AEConfig.instance.isFeatureEnabled( AEFeature.DebugLogging );
}
//
// Specialized handlers
//
/**
* A specialized logging for grinder recipes, can be disabled inside configuration file.
*
* @param message String to be logged
*/
public static void grinder( @Nonnull final String message )
2014-09-24 02:26:27 +02:00
{
if( AEConfig.instance.isFeatureEnabled( AEFeature.GrinderLogging ) )
2014-09-24 02:26:27 +02:00
{
log( Level.DEBUG, "grinder: " + message );
2014-09-24 02:26:27 +02:00
}
}
/**
* A specialized logging for mod integration errors, can be disabled inside configuration file.
*
* @param exception
*/
public static void integration( @Nonnull final Throwable exception )
2014-09-24 02:26:27 +02:00
{
if( AEConfig.instance.isFeatureEnabled( AEFeature.IntegrationLogging ) )
{
debug( exception );
}
2014-09-24 02:26:27 +02:00
}
/**
* Logging of block updates.
*
* Off by default, can be enabled inside the configuration file.
*
* @see AELog#log(Level, String, Object...)
* @param pos
* @param aeBaseTile
*/
public static void blockUpdate( @Nonnull final BlockPos pos, @Nonnull final AEBaseTile aeBaseTile )
2014-09-24 02:26:27 +02:00
{
if( AEConfig.instance.isFeatureEnabled( AEFeature.UpdateLogging ) )
2014-09-24 02:26:27 +02:00
{
info( BLOCK_UPDATE, aeBaseTile.getClass().getName(), pos );
2014-09-24 02:26:27 +02:00
}
}
/**
* Use to check for an enabled crafting log.
*
* Can be used to prevent the execution of unneeded logic.
*
* @return true when the crafting log is enabled.
*/
public static boolean isCraftingLogEnabled()
{
return AEConfig.instance.isFeatureEnabled( AEFeature.CraftingLog );
}
/**
* Logging for autocrafting.
*
* Off by default, can be enabled inside the configuration file.
*
* @see AELog#log(Level, String, Object...)
* @param message
* @param params
*/
public static void crafting( @Nonnull final String message, final Object... params )
2014-09-24 02:26:27 +02:00
{
if( AELog.isCraftingLogEnabled() )
2014-09-24 02:26:27 +02:00
{
log( Level.INFO, message, params );
2014-09-24 02:26:27 +02:00
}
}
/**
* Use to check for an enabled crafting debug log.
*
* Can be used to prevent the execution of unneeded logic.
*
* @return true when the crafting debug log is enabled.
*/
public static boolean isCraftingDebugLogEnabled()
{
return AEConfig.instance.isFeatureEnabled( AEFeature.CraftingLog ) && AEConfig.instance.isFeatureEnabled( AEFeature.DebugLogging );
}
/**
* Debug logging for autocrafting.
*
* Off by default, can be enabled inside the configuration file.
*
* @see AELog#log(Level, String, Object...)
* @param message
* @param params
*/
public static void craftingDebug( @Nonnull final String message, final Object... params )
{
if( AELog.isCraftingDebugLogEnabled() )
{
log( Level.DEBUG, message, params );
}
}
2014-09-24 02:26:27 +02:00
}