From 4ca8d0fd45dace2c453aee55d90bfeb0d35adc4e Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 23 Aug 2019 10:26:13 +0200 Subject: [PATCH] Added a generic log throttler option --- src/main/java/cr0s/warpdrive/Commons.java | 12 ++++++++++++ .../warpdrive/block/BlockAbstractContainer.java | 4 +--- .../block/TileEntityAbstractInterfaced.java | 6 +----- .../cr0s/warpdrive/config/WarpDriveConfig.java | 3 +++ .../java/cr0s/warpdrive/event/ChunkHandler.java | 16 +++++++++------- .../java/cr0s/warpdrive/event/EMPReceiver.java | 4 +--- .../warpdrive/render/BakedModelCapacitor.java | 7 ++----- .../render/CustomModelLoaderProjector.java | 10 ++++------ 8 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/Commons.java b/src/main/java/cr0s/warpdrive/Commons.java index ebbd5f81..93eac0d4 100644 --- a/src/main/java/cr0s/warpdrive/Commons.java +++ b/src/main/java/cr0s/warpdrive/Commons.java @@ -71,6 +71,7 @@ import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; @@ -791,6 +792,17 @@ public class Commons { return EnumFacing.UP; } + private static final ConcurrentHashMap throttle_timePreviousForKey_ms = new ConcurrentHashMap<>(16); + public static boolean throttleMe(final String keyword) { + final Long timeLastLog_ms = throttle_timePreviousForKey_ms.getOrDefault(keyword, Long.MIN_VALUE); + final long timeCurrent_ms = System.currentTimeMillis(); + if (timeCurrent_ms > timeLastLog_ms + WarpDriveConfig.LOGGING_THROTTLE_MS) { + throttle_timePreviousForKey_ms.put(keyword, timeCurrent_ms); + return true; + } + return false; + } + public static boolean isSafeThread() { final String name = Thread.currentThread().getName(); return name.equals("Server thread") || name.equals("Client thread"); diff --git a/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java b/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java index 7f48bcea..2b9a0e62 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java +++ b/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java @@ -47,7 +47,6 @@ import java.util.Random; }) public abstract class BlockAbstractContainer extends BlockContainer implements IBlockBase, defense.api.IEMPBlock { - private static boolean isInvalidEMPreported = false; private static long timeUpdated = -1L; private static int dimensionIdUpdated = Integer.MAX_VALUE; private static int xUpdated = Integer.MAX_VALUE; @@ -299,8 +298,7 @@ public abstract class BlockAbstractContainer extends BlockContainer implements I } else if (explosiveEMP.getRadius() == 50.0F) { onEMP(world, new BlockPos(x, y, z), 0.70F); } else { - if (!isInvalidEMPreported) { - isInvalidEMPreported = true; + if (Commons.throttleMe("BlockAbstractContainer Invalid EMP radius")) { WarpDrive.logger.warn(String.format("EMP received %s from %s with energy %d and unsupported radius %.1f", Commons.format(world, x, y, z), explosiveEMP, explosiveEMP.getEnergy(), explosiveEMP.getRadius())); diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractInterfaced.java b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractInterfaced.java index 8b2a6363..8759651d 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractInterfaced.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractInterfaced.java @@ -58,7 +58,6 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas protected String peripheralName = null; private String[] methodsArray = {}; private boolean isAlwaysInterfaced = true; - private long lastLUAinternalException_ms = Long.MIN_VALUE; // String returned to LUA script in case of error public static final String COMPUTER_ERROR_TAG = "!ERROR!"; @@ -440,11 +439,8 @@ public abstract class TileEntityAbstractInterfaced extends TileEntityAbstractBas } return result; } catch (final Exception exception) { - // only dump once per second - final long currentTime_ms = System.currentTimeMillis(); if ( WarpDriveConfig.LOGGING_LUA - || lastLUAinternalException_ms + 1000L < currentTime_ms ) { - lastLUAinternalException_ms = currentTime_ms; + || Commons.throttleMe("LUA exception") ) { exception.printStackTrace(); } diff --git a/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java b/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java index ea0cafe1..8ca3cd9a 100644 --- a/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java +++ b/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java @@ -217,6 +217,7 @@ public class WarpDriveConfig { public static EnumTooltipCondition TOOLTIP_ADD_REPAIR_WITH = EnumTooltipCondition.ON_SNEAK; // Logging + public static long LOGGING_THROTTLE_MS = 5000L; public static boolean LOGGING_JUMP = true; public static boolean LOGGING_JUMPBLOCKS = false; public static boolean LOGGING_ENERGY = false; @@ -861,6 +862,8 @@ public class WarpDriveConfig { // Logging + LOGGING_THROTTLE_MS = Commons.clamp(0L, 600000L, + config.get("logging", "throttle_ms", LOGGING_THROTTLE_MS, "How many milliseconds to we wait between before logging another occurrence in time sensitive section of the mod (rendering, events, etc.)").getLong(LOGGING_THROTTLE_MS)); LOGGING_JUMP = config.get("logging", "enable_jump_logs", LOGGING_JUMP, "Basic jump logs, should always be enabled").getBoolean(true); LOGGING_JUMPBLOCKS = config.get("logging", "enable_jumpblocks_logs", LOGGING_JUMPBLOCKS, "Detailed jump logs to help debug the mod, will spam your logs...").getBoolean(false); LOGGING_ENERGY = config.get("logging", "enable_energy_logs", LOGGING_ENERGY, "Detailed energy logs to help debug the mod, enable it before reporting a bug").getBoolean(false); diff --git a/src/main/java/cr0s/warpdrive/event/ChunkHandler.java b/src/main/java/cr0s/warpdrive/event/ChunkHandler.java index 042b1324..a0856400 100644 --- a/src/main/java/cr0s/warpdrive/event/ChunkHandler.java +++ b/src/main/java/cr0s/warpdrive/event/ChunkHandler.java @@ -239,7 +239,7 @@ public class ChunkHandler { if (chunkData != null) { chunkData.onBlockUpdated(blockPos.getX(), blockPos.getY(), blockPos.getZ()); } else { - if (WarpDriveConfig.LOGGING_WORLD_GENERATION) { + if (Commons.throttleMe("ChunkHandler block updating in non-loaded chunk")) { WarpDrive.logger.error(String.format("%s block updating %s, while chunk isn't loaded!", world.isRemote ? "Client" : "Server", Commons.format(world, blockPos))); @@ -256,11 +256,13 @@ public class ChunkHandler { public static ChunkData getChunkData(final World world, final int x, final int y, final int z) { final ChunkData chunkData = getChunkData(world.isRemote, world.provider.getDimension(), x, y, z); if (chunkData == null) { - WarpDrive.logger.error(String.format("Trying to get data from an non-loaded chunk in %s %s", - world.isRemote ? "Client" : "Server", - Commons.format(world, x, y, z))); - LocalProfiler.printCallStats(); - Commons.dumpAllThreads(); + if (Commons.throttleMe("ChunkHandler get data from an non-loaded chunk")) { + WarpDrive.logger.error(String.format("Trying to get data from an non-loaded chunk in %s %s", + world.isRemote ? "Client" : "Server", + Commons.format(world, x, y, z))); + LocalProfiler.printCallStats(); + Commons.dumpAllThreads(); + } assert false; } return chunkData; @@ -310,7 +312,7 @@ public class ChunkHandler { } if (Commons.isSafeThread()) { mapRegistryItems.put(index, chunkData); - } else { + } else if (Commons.throttleMe("ChunkHandler added to the registry outside main thread")) { WarpDrive.logger.error(String.format("%s world DIM%d chunk %s is being added to the registry outside main thread!", isRemote ? "Client" : "Server", dimensionId, diff --git a/src/main/java/cr0s/warpdrive/event/EMPReceiver.java b/src/main/java/cr0s/warpdrive/event/EMPReceiver.java index 48ae3442..8a0b750c 100644 --- a/src/main/java/cr0s/warpdrive/event/EMPReceiver.java +++ b/src/main/java/cr0s/warpdrive/event/EMPReceiver.java @@ -29,7 +29,6 @@ import icbm.classic.lib.emp.CapabilityEMP; public class EMPReceiver implements IEMPReceiver, ICapabilityProvider { public static final ResourceLocation resourceLocation = new ResourceLocation(WarpDrive.MODID, "EMPReceiver"); - private static boolean isInvalidEMPReported = false; @SubscribeEvent @Optional.Method(modid = "icbmclassic") @@ -67,8 +66,7 @@ public class EMPReceiver implements IEMPReceiver, ICapabilityProvider { } else if (blastEMP.getBlastRadius() == 50.0F) { tileEntityAbstractBase.onEMP(0.70F); } else { - if (!isInvalidEMPReported) { - isInvalidEMPReported = true; + if (Commons.throttleMe("EMPReceiver Invalid radius")) { WarpDrive.logger.warn(String.format("EMP received @ %s from %s with source %s and unsupported radius %.1f", Commons.format(world, x, y, z), blastEMP, blastEMP.getBlastSource(), blastEMP.getBlastRadius())); diff --git a/src/main/java/cr0s/warpdrive/render/BakedModelCapacitor.java b/src/main/java/cr0s/warpdrive/render/BakedModelCapacitor.java index a77cd57e..6a44a0a6 100644 --- a/src/main/java/cr0s/warpdrive/render/BakedModelCapacitor.java +++ b/src/main/java/cr0s/warpdrive/render/BakedModelCapacitor.java @@ -1,5 +1,6 @@ package cr0s.warpdrive.render; +import cr0s.warpdrive.Commons; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.block.energy.BlockCapacitor; import cr0s.warpdrive.data.EnumDisabledInputOutput; @@ -22,8 +23,6 @@ public class BakedModelCapacitor extends BakedModelAbstractBase { private IExtendedBlockState extendedBlockStateDefault; - private long timeLastError = -1L; - public BakedModelCapacitor() { } @@ -58,9 +57,7 @@ public class BakedModelCapacitor extends BakedModelAbstractBase { if (extendedBlockState != null) { final EnumDisabledInputOutput enumDisabledInputOutput = getEnumDisabledInputOutput(extendedBlockState, enumFacing); if (enumDisabledInputOutput == null) { - final long time = System.currentTimeMillis(); - if (time - timeLastError > 5000L) { - timeLastError = time; + if (Commons.throttleMe("BakedModelCapacitor invalid extended")) { new RuntimeException("Invalid extended property").printStackTrace(); WarpDrive.logger.error(String.format("%s Invalid extended property for %s enumFacing %s\n%s", this, extendedBlockState, enumFacing, formatDetails())); diff --git a/src/main/java/cr0s/warpdrive/render/CustomModelLoaderProjector.java b/src/main/java/cr0s/warpdrive/render/CustomModelLoaderProjector.java index 8a57c0e4..ce22f1fb 100644 --- a/src/main/java/cr0s/warpdrive/render/CustomModelLoaderProjector.java +++ b/src/main/java/cr0s/warpdrive/render/CustomModelLoaderProjector.java @@ -2,6 +2,8 @@ package cr0s.warpdrive.render; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; + +import cr0s.warpdrive.Commons; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.block.forcefield.BlockForceFieldProjector; import cr0s.warpdrive.data.EnumForceFieldShape; @@ -102,9 +104,7 @@ public enum CustomModelLoaderProjector implements ICustomModelLoader { class MyBakedModel implements IBakedModel { private final IBakedModel bakedModel; - - private long timeLastError = -1L; - + MyBakedModel(final IBakedModel bakedModel) { this.bakedModel = bakedModel; initSprites(); @@ -117,9 +117,7 @@ public enum CustomModelLoaderProjector implements ICustomModelLoader { final IExtendedBlockState exState = (IExtendedBlockState) blockState; EnumForceFieldShape enumForceFieldShape = exState != null ? exState.getValue(BlockForceFieldProjector.SHAPE) : EnumForceFieldShape.NONE; if (enumForceFieldShape == null) { - final long time = System.currentTimeMillis(); - if (time - timeLastError > 5000L) { - timeLastError = time; + if (Commons.throttleMe("CustomModelLoaderProjector Invalid shape")) { new RuntimeException("Invalid shape").printStackTrace(); WarpDrive.logger.error(String.format("Invalid shape for %s facing %s", blockState, enumFacing));