1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-29 23:40:46 +00:00

Merge pull request #217 from SquidDev-CC/feature/logging

Add a basic logging system
This commit is contained in:
Daniel Ratcliffe 2017-05-16 19:38:03 +01:00 committed by GitHub
commit 2e22ca4ccf
9 changed files with 63 additions and 17 deletions

View File

@ -68,6 +68,7 @@ import net.minecraftforge.fml.common.network.FMLEventChannel;
import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import org.apache.logging.log4j.Logger;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -106,6 +107,7 @@ public class ComputerCraft
public static String http_whitelist = "*"; public static String http_whitelist = "*";
public static boolean disable_lua51_features = false; public static boolean disable_lua51_features = false;
public static String default_computer_settings = ""; public static String default_computer_settings = "";
public static boolean logPeripheralErrors = false;
public static boolean enableCommandBlock = false; public static boolean enableCommandBlock = false;
public static boolean turtlesNeedFuel = true; public static boolean turtlesNeedFuel = true;
@ -179,6 +181,7 @@ public class ComputerCraft
public static Property http_whitelist; public static Property http_whitelist;
public static Property disable_lua51_features; public static Property disable_lua51_features;
public static Property default_computer_settings; public static Property default_computer_settings;
public static Property logPeripheralErrors;
public static Property enableCommandBlock; public static Property enableCommandBlock;
public static Property turtlesNeedFuel; public static Property turtlesNeedFuel;
@ -207,6 +210,9 @@ public class ComputerCraft
// Creative // Creative
public static CreativeTabMain mainCreativeTab; public static CreativeTabMain mainCreativeTab;
// Logging
public static Logger log;
// API users // API users
private static List<IPeripheralProvider> peripheralProviders = new ArrayList<IPeripheralProvider>(); private static List<IPeripheralProvider> peripheralProviders = new ArrayList<IPeripheralProvider>();
private static List<IBundledRedstoneProvider> bundledRedstoneProviders = new ArrayList<IBundledRedstoneProvider>(); private static List<IBundledRedstoneProvider> bundledRedstoneProviders = new ArrayList<IBundledRedstoneProvider>();
@ -231,6 +237,8 @@ public class ComputerCraft
@Mod.EventHandler @Mod.EventHandler
public void preInit( FMLPreInitializationEvent event ) public void preInit( FMLPreInitializationEvent event )
{ {
log = event.getModLog();
// Load config // Load config
Config.config = new Configuration( event.getSuggestedConfigurationFile() ); Config.config = new Configuration( event.getSuggestedConfigurationFile() );
Config.config.load(); Config.config.load();
@ -247,6 +255,10 @@ public class ComputerCraft
Config.default_computer_settings = Config.config.get( Configuration.CATEGORY_GENERAL, "default_computer_settings", default_computer_settings ); Config.default_computer_settings = Config.config.get( Configuration.CATEGORY_GENERAL, "default_computer_settings", default_computer_settings );
Config.default_computer_settings.setComment( "A comma seperated list of default system settings to set on new computers. Example: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\" will disable all autocompletion" ); Config.default_computer_settings.setComment( "A comma seperated list of default system settings to set on new computers. Example: \"shell.autocomplete=false,lua.autocomplete=false,edit.autocomplete=false\" will disable all autocompletion" );
Config.logPeripheralErrors = Config.config.get( Configuration.CATEGORY_GENERAL, "logPeripheralErrors", logPeripheralErrors );
Config.logPeripheralErrors.setComment( "Log exceptions thrown by peripherals and other Lua objects.\n" +
"This makes it easier for mod authors to debug problems, but may result in log spam should people use buggy methods." );
Config.enableCommandBlock = Config.config.get( Configuration.CATEGORY_GENERAL, "enableCommandBlock", enableCommandBlock ); Config.enableCommandBlock = Config.config.get( Configuration.CATEGORY_GENERAL, "enableCommandBlock", enableCommandBlock );
Config.enableCommandBlock.setComment( "Enable Command Block peripheral support" ); Config.enableCommandBlock.setComment( "Enable Command Block peripheral support" );
@ -605,7 +617,7 @@ public class ComputerCraft
} }
catch( Exception e ) catch( Exception e )
{ {
// mod misbehaved, ignore it ComputerCraft.log.error( "Peripheral provider " + peripheralProvider + " errored.", e );
} }
} }
return null; return null;
@ -649,7 +661,7 @@ public class ComputerCraft
} }
catch( Exception e ) catch( Exception e )
{ {
// mod misbehaved, ignore it ComputerCraft.log.error( "Bundled redstone provider " + bundledRedstoneProvider + " errored.", e );
} }
} }
return combinedSignal; return combinedSignal;
@ -673,6 +685,7 @@ public class ComputerCraft
catch( Exception e ) catch( Exception e )
{ {
// mod misbehaved, ignore it // mod misbehaved, ignore it
ComputerCraft.log.error( "Media provider " + mediaProvider + " errored.", e );
} }
} }
return null; return null;

View File

@ -469,7 +469,7 @@ public class Computer
} }
catch( FileSystemException e ) catch( FileSystemException e )
{ {
e.printStackTrace(); ComputerCraft.log.error( "Cannot mount rom", e );
return false; return false;
} }
} }

View File

@ -6,6 +6,8 @@
package dan200.computercraft.core.computer; package dan200.computercraft.core.computer;
import dan200.computercraft.ComputerCraft;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -106,8 +108,7 @@ public class ComputerThread
try { try {
task.execute(); task.execute();
} catch( Throwable e ) { } catch( Throwable e ) {
System.out.println( "ComputerCraft: Error running task." ); ComputerCraft.log.error( "Error running task", e );
e.printStackTrace();
} }
} }
} ); } );
@ -139,7 +140,7 @@ public class ComputerThread
// Step 3: abandon // Step 3: abandon
if( worker.isAlive() ) if( worker.isAlive() )
{ {
//System.out.println( "computercraft: Warning! Failed to abort Computer " + computercraft.getDescription() + ". Dangling lua thread could cause errors." ); // ComputerCraft.log.warn( "Failed to abort Computer " + computer.getID() + ". Dangling lua thread could cause errors." );
worker.interrupt(); worker.interrupt();
} }
} }

View File

@ -15,6 +15,7 @@ import dan200.computercraft.core.apis.ILuaAPI;
import dan200.computercraft.core.computer.Computer; import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ITask; import dan200.computercraft.core.computer.ITask;
import dan200.computercraft.core.computer.MainThread; import dan200.computercraft.core.computer.MainThread;
import org.luaj.vm2.*; import org.luaj.vm2.*;
import org.luaj.vm2.lib.OneArgFunction; import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.VarArgFunction; import org.luaj.vm2.lib.VarArgFunction;
@ -183,6 +184,7 @@ public class LuaJLuaMachine implements ILuaMachine
} }
catch( LuaError e ) catch( LuaError e )
{ {
ComputerCraft.log.warn( "Could not load bios.lua ", e );
if( m_mainRoutine != null ) if( m_mainRoutine != null )
{ {
((LuaThread)m_mainRoutine).abandon(); ((LuaThread)m_mainRoutine).abandon();
@ -327,7 +329,8 @@ public class LuaJLuaMachine implements ILuaMachine
{ {
final int method = i; final int method = i;
final ILuaObject apiObject = object; final ILuaObject apiObject = object;
table.set( methods[i], new VarArgFunction() { final String methodName = methods[i];
table.set( methodName, new VarArgFunction() {
@Override @Override
public Varargs invoke( Varargs _args ) public Varargs invoke( Varargs _args )
{ {
@ -412,6 +415,10 @@ public class LuaJLuaMachine implements ILuaMachine
} }
catch( Throwable t ) catch( Throwable t )
{ {
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error running task", t );
}
m_computer.queueEvent( "task_complete", new Object[] { m_computer.queueEvent( "task_complete", new Object[] {
taskID, false, "Java Exception Thrown: " + t.toString() taskID, false, "Java Exception Thrown: " + t.toString()
} ); } );
@ -478,6 +485,10 @@ public class LuaJLuaMachine implements ILuaMachine
} }
catch( Throwable t ) catch( Throwable t )
{ {
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error calling " + methodName + " on " + apiObject, t );
}
throw new LuaError( "Java Exception Thrown: " + t.toString(), 0 ); throw new LuaError( "Java Exception Thrown: " + t.toString(), 0 );
} }
return LuaValue.varargsOf( toValues( results, 0 ) ); return LuaValue.varargsOf( toValues( results, 0 ) );

View File

@ -7,6 +7,7 @@
package dan200.computercraft.shared.computer.apis; package dan200.computercraft.shared.computer.apis;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaTask; import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
@ -98,6 +99,10 @@ public class CommandAPI implements ILuaAPI
} }
catch( Throwable t ) catch( Throwable t )
{ {
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error running command.", t );
}
return new Object[]{ false, createOutput( "Java Exception Thrown: " + t.toString() ) }; return new Object[]{ false, createOutput( "Java Exception Thrown: " + t.toString() ) };
} }
} }
@ -208,6 +213,10 @@ public class CommandAPI implements ILuaAPI
catch( Throwable t ) catch( Throwable t )
{ {
// Ignore buggy command // Ignore buggy command
if( ComputerCraft.logPeripheralErrors )
{
ComputerCraft.log.error( "Error checking permissions of command.", t );
}
} }
} }
} }

View File

@ -24,7 +24,7 @@ public class PacketHandler
} }
catch( Exception e ) catch( Exception e )
{ {
e.printStackTrace(); ComputerCraft.log.error( "Error handling packet", e );
} }
} }
@ -39,7 +39,7 @@ public class PacketHandler
} }
catch( Exception e ) catch( Exception e )
{ {
e.printStackTrace(); ComputerCraft.log.error( "Error handling packet", e );
} }
} }
} }

View File

@ -82,7 +82,9 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
int id = upgrade.getLegacyUpgradeID(); int id = upgrade.getLegacyUpgradeID();
if( id >= 0 && id < 64 ) if( id >= 0 && id < 64 )
{ {
throw new RuntimeException( "Error registering '"+upgrade.getUnlocalisedAdjective()+" Turtle'. Legacy UpgradeID '"+id+"' is reserved by ComputerCraft" ); String message = "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. Legacy UpgradeID '" + id + "' is reserved by ComputerCraft";
ComputerCraft.log.error( message );
throw new RuntimeException( message );
} }
// Register // Register
@ -116,6 +118,7 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
} }
catch( Exception e ) catch( Exception e )
{ {
ComputerCraft.log.error("Error getting computer upgrade item", e);
} }
} }
return null; return null;
@ -224,13 +227,17 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
{ {
if( legacyID >= Short.MAX_VALUE ) if( legacyID >= Short.MAX_VALUE )
{ {
throw new RuntimeException( "Error registering '"+upgrade.getUnlocalisedAdjective()+" Turtle'. UpgradeID '"+legacyID+"' is out of range" ); String message = "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. UpgradeID '" + legacyID + "' is out of range";
ComputerCraft.log.error( message );
throw new RuntimeException( message );
} }
ITurtleUpgrade existing = m_legacyTurtleUpgrades.get( legacyID ); ITurtleUpgrade existing = m_legacyTurtleUpgrades.get( legacyID );
if( existing != null ) if( existing != null )
{ {
throw new RuntimeException( "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. UpgradeID '" + legacyID + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'" ); String message = "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. UpgradeID '" + legacyID + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'";
ComputerCraft.log.error( message );
throw new RuntimeException( message );
} }
} }
@ -238,7 +245,9 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
ITurtleUpgrade existing = m_turtleUpgrades.get( id ); ITurtleUpgrade existing = m_turtleUpgrades.get( id );
if( existing != null ) if( existing != null )
{ {
throw new RuntimeException( "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. UpgradeID '" + id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'" ); String message = "Error registering '" + upgrade.getUnlocalisedAdjective() + " Turtle'. UpgradeID '" + id + "' is already registered by '" + existing.getUnlocalisedAdjective() + " Turtle'";
ComputerCraft.log.error( message );
throw new RuntimeException( message );
} }
// Register // Register

View File

@ -1,5 +1,7 @@
package dan200.computercraft.shared.util; package dan200.computercraft.shared.util;
import dan200.computercraft.ComputerCraft;
import java.io.*; import java.io.*;
public class IDAssigner public class IDAssigner
@ -50,6 +52,7 @@ public class IDAssigner
} }
catch( NumberFormatException e ) catch( NumberFormatException e )
{ {
ComputerCraft.log.error( "Unexpected file '" + content + "' in '" + location.getAbsolutePath() + "'", e );
} }
} }
} }
@ -82,7 +85,7 @@ public class IDAssigner
} }
catch( IOException e ) catch( IOException e )
{ {
e.printStackTrace(); ComputerCraft.log.error( "Cannot open ID file '" + lastidFile + "'", e );
return 0; return 0;
} }
@ -92,7 +95,7 @@ public class IDAssigner
} }
catch( NumberFormatException e ) catch( NumberFormatException e )
{ {
e.printStackTrace(); ComputerCraft.log.error( "Cannot parse ID file '" + lastidFile + "', perhaps it is corrupt?", e );
return 0; return 0;
} }
} }
@ -107,8 +110,7 @@ public class IDAssigner
} }
catch( IOException e ) catch( IOException e )
{ {
System.out.println( "An error occured while trying to create the computer folder. Please check you have relevant permissions." ); ComputerCraft.log.error( "An error occured while trying to create the computer folder. Please check you have relevant permissions.", e );
e.printStackTrace();
} }
return id; return id;

View File

@ -44,6 +44,7 @@ gui.computercraft:config.http_enable=Enable HTTP API
gui.computercraft:config.http_whitelist=HTTP whitelist gui.computercraft:config.http_whitelist=HTTP whitelist
gui.computercraft:config.disable_lua51_features=Disable Lua 5.1 features gui.computercraft:config.disable_lua51_features=Disable Lua 5.1 features
gui.computercraft:config.default_computer_settings=Default Computer settings gui.computercraft:config.default_computer_settings=Default Computer settings
gui.computercraft:config.log_peripheral_errors=Log peripheral errors
gui.computercraft:config.enable_command_block=Enable command block peripheral gui.computercraft:config.enable_command_block=Enable command block peripheral
gui.computercraft:config.modem_range=Modem range (default) gui.computercraft:config.modem_range=Modem range (default)
gui.computercraft:config.modem_high_altitude_range=Modem range (high-altitude) gui.computercraft:config.modem_high_altitude_range=Modem range (high-altitude)