mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 02:10:30 +00:00
Add a basic logging system
This adds a common ILogger interface and two built-in loggers - one which prints to stdout, the other using a Log4J logger. These are split up as not to cause dependency issues for emulators. The stdout logger is enabled by default, but the ComputerCraft mod class will swap over to using FML's mod specific logger, which provides a more consistent look with the rest of Minecraft.
This commit is contained in:
parent
7f365c5102
commit
bdc438fc62
@ -20,6 +20,8 @@ import dan200.computercraft.api.turtle.ITurtleUpgrade;
|
||||
import dan200.computercraft.core.filesystem.ComboMount;
|
||||
import dan200.computercraft.core.filesystem.FileMount;
|
||||
import dan200.computercraft.core.filesystem.JarMount;
|
||||
import dan200.computercraft.core.logger.Log4JLogger;
|
||||
import dan200.computercraft.core.logger.Logger;
|
||||
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
|
||||
import dan200.computercraft.shared.computer.blocks.BlockCommandComputer;
|
||||
import dan200.computercraft.shared.computer.blocks.BlockComputer;
|
||||
@ -231,6 +233,8 @@ public class ComputerCraft
|
||||
@Mod.EventHandler
|
||||
public void preInit( FMLPreInitializationEvent event )
|
||||
{
|
||||
Logger.setInstance( new Log4JLogger( event.getModLog() ) );
|
||||
|
||||
// Load config
|
||||
Config.config = new Configuration( event.getSuggestedConfigurationFile() );
|
||||
Config.config.load();
|
||||
|
10
src/main/java/dan200/computercraft/core/logger/ILogger.java
Normal file
10
src/main/java/dan200/computercraft/core/logger/ILogger.java
Normal file
@ -0,0 +1,10 @@
|
||||
package dan200.computercraft.core.logger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public interface ILogger
|
||||
{
|
||||
void log( Level level, String message );
|
||||
|
||||
void log( Level level, String message, Throwable t );
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package dan200.computercraft.core.logger;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Log4JLogger implements ILogger
|
||||
{
|
||||
private final org.apache.logging.log4j.Logger logger;
|
||||
|
||||
public Log4JLogger( Logger logger )
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log( Level level, String message )
|
||||
{
|
||||
logger.log( mapLevel( level ), message );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log( Level level, String message, Throwable t )
|
||||
{
|
||||
logger.log( mapLevel( level ), message, t );
|
||||
}
|
||||
|
||||
private static org.apache.logging.log4j.Level mapLevel( Level level )
|
||||
{
|
||||
if( level == Level.SEVERE )
|
||||
{
|
||||
return org.apache.logging.log4j.Level.ERROR;
|
||||
}
|
||||
else if( level == Level.WARNING )
|
||||
{
|
||||
return org.apache.logging.log4j.Level.WARN;
|
||||
}
|
||||
else if( level == Level.INFO )
|
||||
{
|
||||
return org.apache.logging.log4j.Level.INFO;
|
||||
}
|
||||
else if( level == Level.FINE )
|
||||
{
|
||||
return org.apache.logging.log4j.Level.DEBUG;
|
||||
}
|
||||
else if( level == Level.FINER )
|
||||
{
|
||||
return org.apache.logging.log4j.Level.TRACE;
|
||||
}
|
||||
else if( level == Level.ALL )
|
||||
{
|
||||
return org.apache.logging.log4j.Level.ALL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return org.apache.logging.log4j.Level.INFO;
|
||||
}
|
||||
}
|
||||
}
|
65
src/main/java/dan200/computercraft/core/logger/Logger.java
Normal file
65
src/main/java/dan200/computercraft/core/logger/Logger.java
Normal file
@ -0,0 +1,65 @@
|
||||
package dan200.computercraft.core.logger;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Logger
|
||||
{
|
||||
private static ILogger instance;
|
||||
|
||||
@Nonnull
|
||||
public static ILogger getInstance()
|
||||
{
|
||||
ILogger logger = instance;
|
||||
if( logger == null ) logger = instance = new StdoutLogger();
|
||||
return logger;
|
||||
}
|
||||
|
||||
public static void setInstance( @Nonnull ILogger logger )
|
||||
{
|
||||
if( logger == null ) throw new NullPointerException( "Logger cannot be null" );
|
||||
instance = logger;
|
||||
}
|
||||
|
||||
public static void log( Level level, String message )
|
||||
{
|
||||
getInstance().log( level, message );
|
||||
}
|
||||
|
||||
public static void log( Level level, String message, Throwable t )
|
||||
{
|
||||
getInstance().log( level, message, t );
|
||||
}
|
||||
|
||||
public static void error( String message )
|
||||
{
|
||||
getInstance().log( Level.SEVERE, message );
|
||||
}
|
||||
|
||||
public static void error( String message, Throwable t )
|
||||
{
|
||||
getInstance().log( Level.SEVERE, message, t );
|
||||
}
|
||||
|
||||
public static void warn( String message )
|
||||
{
|
||||
getInstance().log( Level.WARNING, message );
|
||||
}
|
||||
|
||||
public static void warn( String message, Throwable t )
|
||||
{
|
||||
getInstance().log( Level.WARNING, message, t );
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs {@code message} and creates a new {@link RuntimeException} using the same message.
|
||||
*
|
||||
* @param message The message to log.
|
||||
* @return The exception using the same message.
|
||||
*/
|
||||
public static RuntimeException loggedError( String message )
|
||||
{
|
||||
getInstance().log( Level.SEVERE, message );
|
||||
return new RuntimeException( message );
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package dan200.computercraft.core.logger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class StdoutLogger implements ILogger
|
||||
{
|
||||
|
||||
@Override
|
||||
public void log( Level level, String message )
|
||||
{
|
||||
System.out.printf( "[%s] %s\n", level.getName(), message );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log( Level level, String message, Throwable t )
|
||||
{
|
||||
System.out.printf( "[%s] %s\n", level.getName(), message );
|
||||
t.printStackTrace( System.out );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user