File size: 3,364 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package net.minecraft.server;

import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
import java.util.Queue;
import net.minecraft.util.ArrayListDeque;

public class SuppressedExceptionCollector {
    private static final int LATEST_ENTRY_COUNT = 8;
    private final Queue<SuppressedExceptionCollector.LongEntry> latestEntries = new ArrayListDeque<>();
    private final Object2IntLinkedOpenHashMap<SuppressedExceptionCollector.ShortEntry> entryCounts = new Object2IntLinkedOpenHashMap<>();

    private static long currentTimeMs() {
        return System.currentTimeMillis();
    }

    public synchronized void addEntry(String p_368067_, Throwable p_370087_) {
        long i = currentTimeMs();
        String s = p_370087_.getMessage();
        this.latestEntries.add(new SuppressedExceptionCollector.LongEntry(i, p_368067_, (Class<? extends Throwable>)p_370087_.getClass(), s));

        while (this.latestEntries.size() > 8) {
            this.latestEntries.remove();
        }

        SuppressedExceptionCollector.ShortEntry suppressedexceptioncollector$shortentry = new SuppressedExceptionCollector.ShortEntry(
            p_368067_, (Class<? extends Throwable>)p_370087_.getClass()
        );
        int j = this.entryCounts.getInt(suppressedexceptioncollector$shortentry);
        this.entryCounts.putAndMoveToFirst(suppressedexceptioncollector$shortentry, j + 1);
    }

    public synchronized String dump() {
        long i = currentTimeMs();
        StringBuilder stringbuilder = new StringBuilder();
        if (!this.latestEntries.isEmpty()) {
            stringbuilder.append("\n\t\tLatest entries:\n");

            for (SuppressedExceptionCollector.LongEntry suppressedexceptioncollector$longentry : this.latestEntries) {
                stringbuilder.append("\t\t\t")
                    .append(suppressedexceptioncollector$longentry.location)
                    .append(":")
                    .append(suppressedexceptioncollector$longentry.cls)
                    .append(": ")
                    .append(suppressedexceptioncollector$longentry.message)
                    .append(" (")
                    .append(i - suppressedexceptioncollector$longentry.timestampMs)
                    .append("ms ago)")
                    .append("\n");
            }
        }

        if (!this.entryCounts.isEmpty()) {
            if (stringbuilder.isEmpty()) {
                stringbuilder.append("\n");
            }

            stringbuilder.append("\t\tEntry counts:\n");

            for (Entry<SuppressedExceptionCollector.ShortEntry> entry : Object2IntMaps.fastIterable(this.entryCounts)) {
                stringbuilder.append("\t\t\t")
                    .append(entry.getKey().location)
                    .append(":")
                    .append(entry.getKey().cls)
                    .append(" x ")
                    .append(entry.getIntValue())
                    .append("\n");
            }
        }

        return stringbuilder.isEmpty() ? "~~NONE~~" : stringbuilder.toString();
    }

    static record LongEntry(long timestampMs, String location, Class<? extends Throwable> cls, String message) {
    }

    static record ShortEntry(String location, Class<? extends Throwable> cls) {
    }
}