package net.minecraft.util.profiling.jfr; import com.mojang.logging.LogUtils; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.function.Supplier; import net.minecraft.server.Bootstrap; import net.minecraft.util.profiling.jfr.parse.JfrStatsParser; import net.minecraft.util.profiling.jfr.parse.JfrStatsResult; import org.apache.commons.lang3.StringUtils; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; public class SummaryReporter { private static final Logger LOGGER = LogUtils.getLogger(); private final Runnable onDeregistration; protected SummaryReporter(final Runnable onDeregistration) { this.onDeregistration = onDeregistration; } public void recordingStopped(@Nullable final Path result) { if (result != null) { this.onDeregistration.run(); infoWithFallback(() -> "Dumped flight recorder profiling to " + result); JfrStatsResult statsResult; try { statsResult = JfrStatsParser.parse(result); } catch (Throwable var5) { warnWithFallback(() -> "Failed to parse JFR recording", var5); return; } try { infoWithFallback(statsResult::asJson); Path jsonReport = result.resolveSibling("jfr-report-" + StringUtils.substringBefore(result.getFileName().toString(), ".jfr") + ".json"); Files.writeString(jsonReport, statsResult.asJson(), StandardOpenOption.CREATE); infoWithFallback(() -> "Dumped recording summary to " + jsonReport); } catch (Throwable var4) { warnWithFallback(() -> "Failed to output JFR report", var4); } } } private static void infoWithFallback(final Supplier message) { if (LogUtils.isLoggerActive()) { LOGGER.info((String)message.get()); } else { Bootstrap.realStdoutPrintln((String)message.get()); } } private static void warnWithFallback(final Supplier message, final Throwable t) { if (LogUtils.isLoggerActive()) { LOGGER.warn((String)message.get(), t); } else { Bootstrap.realStdoutPrintln((String)message.get()); t.printStackTrace(Bootstrap.STDOUT); } } }