From 7ce59cac2c5ec2a7b61e2f9e966b9314d783f8c0 Mon Sep 17 00:00:00 2001 From: Glavo Date: Mon, 15 Sep 2025 15:21:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=20=20org.jackhuang.hmcl.util?= =?UTF-8?q?.logging=20(#4484)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jackhuang/hmcl/util/logging/Level.java | 8 ---- .../jackhuang/hmcl/util/logging/LogEvent.java | 43 ++++++++++------- .../jackhuang/hmcl/util/logging/Logger.java | 48 ++++++------------- 3 files changed, 41 insertions(+), 58 deletions(-) delete mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Level.java diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Level.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Level.java deleted file mode 100644 index e1a584726..000000000 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Level.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.jackhuang.hmcl.util.logging; - -/** - * @author Glavo - */ -public enum Level { - ERROR, WARNING, INFO, DEBUG, TRACE -} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/LogEvent.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/LogEvent.java index dcacdb756..1a6812340 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/LogEvent.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/LogEvent.java @@ -1,3 +1,20 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.jackhuang.hmcl.util.logging; import java.io.IOException; @@ -7,24 +24,16 @@ import java.util.concurrent.CountDownLatch; /** * @author Glavo */ -abstract class LogEvent { - static final class DoLog extends LogEvent { - final long time; - final String caller; - final Level level; - final String message; - final Throwable exception; - - DoLog(long time, String caller, Level level, String message, Throwable exception) { - this.time = time; - this.caller = caller; - this.level = level; - this.message = message; - this.exception = exception; - } +sealed interface LogEvent { + record DoLog(long time, + String caller, + System.Logger.Level level, + String message, + Throwable exception + ) implements LogEvent { } - static final class ExportLog extends LogEvent { + final class ExportLog implements LogEvent { final CountDownLatch latch = new CountDownLatch(1); final OutputStream output; @@ -39,6 +48,6 @@ abstract class LogEvent { } } - static final class Shutdown extends LogEvent { + final class Shutdown implements LogEvent { } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Logger.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Logger.java index a97449fa1..16ee74b3d 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Logger.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/logging/Logger.java @@ -23,6 +23,7 @@ import org.tukaani.xz.LZMA2Options; import org.tukaani.xz.XZOutputStream; import java.io.*; +import java.lang.System.Logger.Level; import java.nio.file.*; import java.time.Instant; import java.time.LocalDateTime; @@ -86,26 +87,26 @@ public final class Logger { StringBuilder builder = this.builder; builder.setLength(0); builder.append('['); - TIME_FORMATTER.formatTo(Instant.ofEpochMilli(event.time), builder); + TIME_FORMATTER.formatTo(Instant.ofEpochMilli(event.time()), builder); builder.append("] ["); - if (event.caller != null && event.caller.startsWith(PACKAGE_PREFIX)) { - builder.append("@.").append(event.caller, PACKAGE_PREFIX.length(), event.caller.length()); + if (event.caller() != null && event.caller().startsWith(PACKAGE_PREFIX)) { + builder.append("@.").append(event.caller(), PACKAGE_PREFIX.length(), event.caller().length()); } else { - builder.append(event.caller); + builder.append(event.caller()); } builder.append('/') - .append(event.level) + .append(event.level()) .append("] ") - .append(filterForbiddenToken(event.message)); + .append(filterForbiddenToken(event.message())); return builder.toString(); } private void handle(LogEvent event) { - if (event instanceof LogEvent.DoLog) { - String log = format((LogEvent.DoLog) event); - Throwable exception = ((LogEvent.DoLog) event).exception; + if (event instanceof LogEvent.DoLog doLog) { + String log = format(doLog); + Throwable exception = doLog.exception(); System.out.println(log); if (exception != null) @@ -114,8 +115,7 @@ public final class Logger { logWriter.println(log); if (exception != null) exception.printStackTrace(logWriter); - } else if (event instanceof LogEvent.ExportLog) { - LogEvent.ExportLog exportEvent = (LogEvent.ExportLog) event; + } else if (event instanceof LogEvent.ExportLog exportEvent) { logWriter.flush(); try { if (logFile != null) { @@ -368,7 +368,9 @@ public final class Logger { log(Level.TRACE, CallerFinder.getCaller(), msg, exception); } - private static final class LogFile implements Comparable { + private record LogFile(Path file, + int year, int month, int day, int hour, int minute, int second, + int n) implements Comparable { private static final Pattern FILE_NAME_PATTERN = Pattern.compile("(?\\d{4})-(?\\d{2})-(?\\d{2})T(?\\d{2})-(?\\d{2})-(?\\d{2})(\\.(?\\d+))?\\.log(\\.(gz|xz))?"); private static @Nullable LogFile ofFile(Path file) { @@ -390,26 +392,6 @@ public final class Logger { return new LogFile(file, year, month, day, hour, minute, second, n); } - private final Path file; - private final int year; - private final int month; - private final int day; - private final int hour; - private final int minute; - private final int second; - private final int n; - - private LogFile(Path file, int year, int month, int day, int hour, int minute, int second, int n) { - this.file = file; - this.year = year; - this.month = month; - this.day = day; - this.hour = hour; - this.minute = minute; - this.second = second; - this.n = n; - } - @Override public int compareTo(@NotNull Logger.LogFile that) { if (this.year != that.year) return Integer.compare(this.year, that.year); @@ -429,7 +411,7 @@ public final class Logger { @Override public boolean equals(Object obj) { - return obj instanceof LogFile && compareTo((LogFile) obj) == 0; + return obj instanceof LogFile that && compareTo(that) == 0; } } }