diff --git a/src/main/java/dan200/computercraft/ComputerCraft.java b/src/main/java/dan200/computercraft/ComputerCraft.java index 210af293e..219fd00a2 100644 --- a/src/main/java/dan200/computercraft/ComputerCraft.java +++ b/src/main/java/dan200/computercraft/ComputerCraft.java @@ -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(); diff --git a/src/main/java/dan200/computercraft/core/logger/ILogger.java b/src/main/java/dan200/computercraft/core/logger/ILogger.java new file mode 100644 index 000000000..4c23ffc19 --- /dev/null +++ b/src/main/java/dan200/computercraft/core/logger/ILogger.java @@ -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 ); +} diff --git a/src/main/java/dan200/computercraft/core/logger/Log4JLogger.java b/src/main/java/dan200/computercraft/core/logger/Log4JLogger.java new file mode 100644 index 000000000..a15b3222d --- /dev/null +++ b/src/main/java/dan200/computercraft/core/logger/Log4JLogger.java @@ -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; + } + } +} diff --git a/src/main/java/dan200/computercraft/core/logger/Logger.java b/src/main/java/dan200/computercraft/core/logger/Logger.java new file mode 100644 index 000000000..9687e41c5 --- /dev/null +++ b/src/main/java/dan200/computercraft/core/logger/Logger.java @@ -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 ); + } +} diff --git a/src/main/java/dan200/computercraft/core/logger/StdoutLogger.java b/src/main/java/dan200/computercraft/core/logger/StdoutLogger.java new file mode 100644 index 000000000..2abea7cd1 --- /dev/null +++ b/src/main/java/dan200/computercraft/core/logger/StdoutLogger.java @@ -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 ); + } +}