From f89b5a005af35f10cbbd24a3f7e464a0d347b408 Mon Sep 17 00:00:00 2001 From: Nicholas Ignoffo Date: Sat, 17 Feb 2018 09:48:17 -0800 Subject: [PATCH] Logging for plugin gathering Moved spammy API stuff to a separate logger --- .../java/WayofTime/bloodmagic/BloodMagic.java | 7 ++--- .../WayofTime/bloodmagic/ConfigHandler.java | 4 ++- .../bloodmagic/api/impl/BloodMagicAPI.java | 2 +- .../api/impl/BloodMagicBlacklist.java | 10 +++--- .../api/impl/BloodMagicValueManager.java | 6 ++-- .../java/WayofTime/bloodmagic/util/BMLog.java | 6 ++++ .../WayofTime/bloodmagic/util/PluginUtil.java | 31 +++++++++++++++++-- 7 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/main/java/WayofTime/bloodmagic/BloodMagic.java b/src/main/java/WayofTime/bloodmagic/BloodMagic.java index 18a83ef5..0b1122f4 100644 --- a/src/main/java/WayofTime/bloodmagic/BloodMagic.java +++ b/src/main/java/WayofTime/bloodmagic/BloodMagic.java @@ -2,7 +2,6 @@ package WayofTime.bloodmagic; import WayofTime.bloodmagic.api.BloodMagicPlugin; import WayofTime.bloodmagic.api.IBloodMagicPlugin; -import WayofTime.bloodmagic.api.impl.BloodMagicAPI; import WayofTime.bloodmagic.core.registry.OrbRegistry; import WayofTime.bloodmagic.core.registry.RitualRegistry; import WayofTime.bloodmagic.util.helper.LogHelper; @@ -73,7 +72,7 @@ public class BloodMagic { public void preInit(FMLPreInitializationEvent event) { configDir = new File(event.getModConfigurationDirectory(), "bloodmagic"); - PLUGINS.addAll(PluginUtil.getPlugins(event.getAsmData())); + PLUGINS.addAll(PluginUtil.gatherPlugins(event.getAsmData())); ModTranquilityHandlers.init(); ModDungeons.init(); @@ -84,8 +83,8 @@ public class BloodMagic { @Mod.EventHandler public void init(FMLInitializationEvent event) { BloodMagicPacketHandler.init(); - for (Pair plugin : PLUGINS) - plugin.getLeft().register(BloodMagicAPI.INSTANCE); + + PluginUtil.registerPlugins(PLUGINS); ModRecipes.init(); ModRituals.initRituals(); diff --git a/src/main/java/WayofTime/bloodmagic/ConfigHandler.java b/src/main/java/WayofTime/bloodmagic/ConfigHandler.java index 889c07af..27316942 100644 --- a/src/main/java/WayofTime/bloodmagic/ConfigHandler.java +++ b/src/main/java/WayofTime/bloodmagic/ConfigHandler.java @@ -35,8 +35,10 @@ public class ConfigHandler { public static class ConfigGeneral { @Config.Comment({"Enables extra information to be printed to the log.", "Warning: May drastically increase log size."}) public boolean enableDebugLogging = false; - @Config.Comment({"Enables extra information to be printed to the log.", "Warning: May drastically increase log size."}) + @Config.Comment({"Enables extra information to be printed to the log."}) public boolean enableAPILogging = false; + @Config.Comment({"Enables extra information to be printed to the log.", "Warning: May drastically increase log size."}) + public boolean enableVerboseAPILogging = false; } public static class ConfigBlacklist { diff --git a/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicAPI.java b/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicAPI.java index a1f82fe7..05d0716b 100644 --- a/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicAPI.java +++ b/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicAPI.java @@ -55,7 +55,7 @@ public class BloodMagicAPI implements IBloodMagicAPI { } if (component != null) { - BMLog.API.info("Registered {} as a {} altar component.", state, componentType); + BMLog.API_VERBOSE.info("Registered {} as a {} altar component.", state, componentType); altarComponents.put(component, state); } else BMLog.API.warn("Invalid Altar component type: {}.", componentType); } diff --git a/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicBlacklist.java b/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicBlacklist.java index 5348eb2c..18029c71 100644 --- a/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicBlacklist.java +++ b/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicBlacklist.java @@ -30,7 +30,7 @@ public class BloodMagicBlacklist implements IBloodMagicBlacklist { @Override public void addTeleposer(@Nonnull IBlockState state) { if (!teleposer.contains(state)) { - BMLog.API.info("Blacklist: Added {} to the Teleposer blacklist.", state); + BMLog.API_VERBOSE.info("Blacklist: Added {} to the Teleposer blacklist.", state); teleposer.add(state); } } @@ -43,7 +43,7 @@ public class BloodMagicBlacklist implements IBloodMagicBlacklist { @Override public void addTeleposer(@Nonnull ResourceLocation entityId) { if (!teleposerEntities.contains(entityId)) { - BMLog.API.info("Blacklist: Added {} to the Teleposer blacklist.", entityId); + BMLog.API_VERBOSE.info("Blacklist: Added {} to the Teleposer blacklist.", entityId); teleposerEntities.add(entityId); } } @@ -51,7 +51,7 @@ public class BloodMagicBlacklist implements IBloodMagicBlacklist { @Override public void addTransposition(@Nonnull IBlockState state) { if (!transposition.contains(state)) { - BMLog.API.info("Blacklist: Added {} to the Transposition blacklist.", state); + BMLog.API_VERBOSE.info("Blacklist: Added {} to the Transposition blacklist.", state); transposition.add(state); } } @@ -64,7 +64,7 @@ public class BloodMagicBlacklist implements IBloodMagicBlacklist { @Override public void addGreenGrove(@Nonnull IBlockState state) { if (!greenGrove.contains(state)) { - BMLog.API.info("Blacklist: Added {} to the Green Grove blacklist.", state); + BMLog.API_VERBOSE.info("Blacklist: Added {} to the Green Grove blacklist.", state); greenGrove.add(state); } } @@ -77,7 +77,7 @@ public class BloodMagicBlacklist implements IBloodMagicBlacklist { @Override public void addWellOfSuffering(@Nonnull ResourceLocation entityId) { if (!sacrifice.contains(entityId)) { - BMLog.API.info("Blacklist: Added {} to the Well of Suffering blacklist.", entityId); + BMLog.API_VERBOSE.info("Blacklist: Added {} to the Well of Suffering blacklist.", entityId); sacrifice.add(entityId); } } diff --git a/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicValueManager.java b/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicValueManager.java index 99fc2e9f..5c894574 100644 --- a/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicValueManager.java +++ b/src/main/java/WayofTime/bloodmagic/api/impl/BloodMagicValueManager.java @@ -25,7 +25,7 @@ public class BloodMagicValueManager implements IBloodMagicValueManager { @Override public void setSacrificialValue(@Nonnull ResourceLocation entityId, int value) { - BMLog.API.info("Value Manager: Set sacrificial value of {} to {}.", entityId, value); + BMLog.API_VERBOSE.info("Value Manager: Set sacrificial value of {} to {}.", entityId, value); sacrificial.put(entityId, value); } @@ -40,14 +40,14 @@ public class BloodMagicValueManager implements IBloodMagicValueManager { } if (tranquility != null) { - BMLog.API.info("Value Manager: Set tranquility value of {} to {} @ {}", state, tranquilityType, value); + BMLog.API_VERBOSE.info("Value Manager: Set tranquility value of {} to {} @ {}", state, tranquilityType, value); this.tranquility.put(state, new TranquilityStack(tranquility, value)); } else BMLog.API.warn("Invalid tranquility type: {}.", tranquilityType); } public void setTranquility(Block block, TranquilityStack tranquilityStack) { for (IBlockState state : block.getBlockState().getValidStates()) { - BMLog.API.info("Value Manager: Set tranquility value of {} to {} @ {}", state, tranquilityStack.type, tranquilityStack.value); + BMLog.API_VERBOSE.info("Value Manager: Set tranquility value of {} to {} @ {}", state, tranquilityStack.type, tranquilityStack.value); tranquility.put(state, tranquilityStack); } } diff --git a/src/main/java/WayofTime/bloodmagic/util/BMLog.java b/src/main/java/WayofTime/bloodmagic/util/BMLog.java index 0a9f1cfa..c6e75232 100644 --- a/src/main/java/WayofTime/bloodmagic/util/BMLog.java +++ b/src/main/java/WayofTime/bloodmagic/util/BMLog.java @@ -26,6 +26,12 @@ public enum BMLog { return ConfigHandler.general.enableAPILogging; } }, + API_VERBOSE() { + @Override + boolean enabled() { + return ConfigHandler.general.enableVerboseAPILogging; + } + }, ; private final Logger logger; diff --git a/src/main/java/WayofTime/bloodmagic/util/PluginUtil.java b/src/main/java/WayofTime/bloodmagic/util/PluginUtil.java index 0b69af06..2faf5d38 100644 --- a/src/main/java/WayofTime/bloodmagic/util/PluginUtil.java +++ b/src/main/java/WayofTime/bloodmagic/util/PluginUtil.java @@ -2,7 +2,9 @@ package WayofTime.bloodmagic.util; import WayofTime.bloodmagic.api.BloodMagicPlugin; import WayofTime.bloodmagic.api.IBloodMagicPlugin; +import WayofTime.bloodmagic.api.impl.BloodMagicAPI; import WayofTime.bloodmagic.api.impl.BloodMagicCorePlugin; +import com.google.common.base.Stopwatch; import com.google.common.collect.Lists; import net.minecraftforge.fml.common.discovery.ASMDataTable; import org.apache.commons.lang3.tuple.Pair; @@ -15,7 +17,8 @@ public class PluginUtil { @SuppressWarnings("unchecked") @Nonnull - public static List> getPlugins(ASMDataTable dataTable) { + public static List> gatherPlugins(ASMDataTable dataTable) { + Stopwatch stopwatch = Stopwatch.createStarted(); List> discoveredAnnotations = Lists.newArrayList(); Set discoveredPlugins = dataTable.getAll(BloodMagicPlugin.class.getCanonicalName()); @@ -26,6 +29,7 @@ public class PluginUtil { IBloodMagicPlugin instance = pluginClass.newInstance(); + BMLog.API.info("Discovered plugin at {}", data.getClassName()); discoveredAnnotations.add(Pair.of(instance, pluginClass.getAnnotation(BloodMagicPlugin.class))); } catch (Exception e) { e.printStackTrace(); @@ -33,7 +37,30 @@ public class PluginUtil { } // Bring core plugin up to top - discoveredAnnotations.sort((o1, o2) -> o1.getLeft().getClass() == BloodMagicCorePlugin.class ? 1 : 0); + discoveredAnnotations.sort((o1, o2) -> { + if (o1.getLeft().getClass() == BloodMagicCorePlugin.class) + return -1; + + return o1.getClass().getCanonicalName().compareToIgnoreCase(o2.getClass().getCanonicalName()); + }); + BMLog.API.info("Discovered {} potential plugin(s) in {}", discoveredAnnotations.size(), stopwatch.stop()); return discoveredAnnotations; } + + public static void registerPlugins(List> plugins) { + Stopwatch total = Stopwatch.createStarted(); + int errors = 0; + for (Pair plugin : plugins) { + Stopwatch per = Stopwatch.createStarted(); + try { + plugin.getLeft().register(BloodMagicAPI.INSTANCE); + } catch (Exception e) { + errors++; + BMLog.DEFAULT.error("Error loading plugin at {}: {}: {}", plugin.getLeft().getClass(), e.getClass().getSimpleName(), e.getMessage()); + } + BMLog.API.info("Registered plugin at {} in {}", plugin.getLeft().getClass(), per.stop()); + } + + BMLog.API.info("Registered {} plugins with {} errors in {}", plugins.size() - errors, errors, total.stop()); + } }