| 1 | package net.minecraft.server;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntMaps;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
|
| 6 | import java.util.Queue;
|
| 7 | import net.minecraft.util.ArrayListDeque;
|
| 8 |
|
| 9 | public class SuppressedExceptionCollector {
|
| 10 | private static final int LATEST_ENTRY_COUNT = 8;
|
| 11 | private final Queue<SuppressedExceptionCollector.LongEntry> latestEntries = new ArrayListDeque<>();
|
| 12 | private final Object2IntLinkedOpenHashMap<SuppressedExceptionCollector.ShortEntry> entryCounts = new Object2IntLinkedOpenHashMap<>();
|
| 13 |
|
| 14 | private static long currentTimeMs() {
|
| 15 | return System.currentTimeMillis();
|
| 16 | }
|
| 17 |
|
| 18 | public synchronized void addEntry(final String location, final Throwable throwable) {
|
| 19 | long now = currentTimeMs();
|
| 20 | String message = throwable.getMessage();
|
| 21 | this.latestEntries.add(new SuppressedExceptionCollector.LongEntry(now, location, (Class<? extends Throwable>)throwable.getClass(), message));
|
| 22 |
|
| 23 | while (this.latestEntries.size() > 8) {
|
| 24 | this.latestEntries.remove();
|
| 25 | }
|
| 26 |
|
| 27 | SuppressedExceptionCollector.ShortEntry key = new SuppressedExceptionCollector.ShortEntry(location, (Class<? extends Throwable>)throwable.getClass());
|
| 28 | int currentValue = this.entryCounts.getInt(key);
|
| 29 | this.entryCounts.putAndMoveToFirst(key, currentValue + 1);
|
| 30 | }
|
| 31 |
|
| 32 | public synchronized String dump() {
|
| 33 | long current = currentTimeMs();
|
| 34 | StringBuilder result = new StringBuilder();
|
| 35 | if (!this.latestEntries.isEmpty()) {
|
| 36 | result.append("\n\t\tLatest entries:\n");
|
| 37 |
|
| 38 | for (SuppressedExceptionCollector.LongEntry e : this.latestEntries) {
|
| 39 | result.append("\t\t\t")
|
| 40 | .append(e.location)
|
| 41 | .append(":")
|
| 42 | .append(e.cls)
|
| 43 | .append(": ")
|
| 44 | .append(e.message)
|
| 45 | .append(" (")
|
| 46 | .append(current - e.timestampMs)
|
| 47 | .append("ms ago)")
|
| 48 | .append("\n");
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | if (!this.entryCounts.isEmpty()) {
|
| 53 | if (result.isEmpty()) {
|
| 54 | result.append("\n");
|
| 55 | }
|
| 56 |
|
| 57 | result.append("\t\tEntry counts:\n");
|
| 58 |
|
| 59 | for (Entry<SuppressedExceptionCollector.ShortEntry> e : Object2IntMaps.fastIterable(this.entryCounts)) {
|
| 60 | result.append("\t\t\t").append(e.getKey().location).append(":").append(e.getKey().cls).append(" x ").append(e.getIntValue()).append("\n");
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | return result.isEmpty() ? "~~NONE~~" : result.toString();
|
| 65 | }
|
| 66 |
|
| 67 | private record LongEntry(long timestampMs, String location, Class<? extends Throwable> cls, String message) {
|
| 68 | }
|
| 69 |
|
| 70 | private record ShortEntry(String location, Class<? extends Throwable> cls) {
|
| 71 | }
|
| 72 | }
|
| 73 |
|