清理 org.jackhuang.hmcl.util.logging (#4484)

This commit is contained in:
Glavo
2025-09-15 15:21:14 +08:00
committed by GitHub
parent a7178802f8
commit 7ce59cac2c
3 changed files with 41 additions and 58 deletions

View File

@@ -1,8 +0,0 @@
package org.jackhuang.hmcl.util.logging;
/**
* @author Glavo
*/
public enum Level {
ERROR, WARNING, INFO, DEBUG, TRACE
}

View File

@@ -1,3 +1,20 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> 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 <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util.logging; package org.jackhuang.hmcl.util.logging;
import java.io.IOException; import java.io.IOException;
@@ -7,24 +24,16 @@ import java.util.concurrent.CountDownLatch;
/** /**
* @author Glavo * @author Glavo
*/ */
abstract class LogEvent { sealed interface LogEvent {
static final class DoLog extends LogEvent { record DoLog(long time,
final long time; String caller,
final String caller; System.Logger.Level level,
final Level level; String message,
final String message; Throwable exception
final Throwable exception; ) implements LogEvent {
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;
}
} }
static final class ExportLog extends LogEvent { final class ExportLog implements LogEvent {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final OutputStream output; final OutputStream output;
@@ -39,6 +48,6 @@ abstract class LogEvent {
} }
} }
static final class Shutdown extends LogEvent { final class Shutdown implements LogEvent {
} }
} }

View File

@@ -23,6 +23,7 @@ import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream; import org.tukaani.xz.XZOutputStream;
import java.io.*; import java.io.*;
import java.lang.System.Logger.Level;
import java.nio.file.*; import java.nio.file.*;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -86,26 +87,26 @@ public final class Logger {
StringBuilder builder = this.builder; StringBuilder builder = this.builder;
builder.setLength(0); builder.setLength(0);
builder.append('['); builder.append('[');
TIME_FORMATTER.formatTo(Instant.ofEpochMilli(event.time), builder); TIME_FORMATTER.formatTo(Instant.ofEpochMilli(event.time()), builder);
builder.append("] ["); builder.append("] [");
if (event.caller != null && event.caller.startsWith(PACKAGE_PREFIX)) { if (event.caller() != null && event.caller().startsWith(PACKAGE_PREFIX)) {
builder.append("@.").append(event.caller, PACKAGE_PREFIX.length(), event.caller.length()); builder.append("@.").append(event.caller(), PACKAGE_PREFIX.length(), event.caller().length());
} else { } else {
builder.append(event.caller); builder.append(event.caller());
} }
builder.append('/') builder.append('/')
.append(event.level) .append(event.level())
.append("] ") .append("] ")
.append(filterForbiddenToken(event.message)); .append(filterForbiddenToken(event.message()));
return builder.toString(); return builder.toString();
} }
private void handle(LogEvent event) { private void handle(LogEvent event) {
if (event instanceof LogEvent.DoLog) { if (event instanceof LogEvent.DoLog doLog) {
String log = format((LogEvent.DoLog) event); String log = format(doLog);
Throwable exception = ((LogEvent.DoLog) event).exception; Throwable exception = doLog.exception();
System.out.println(log); System.out.println(log);
if (exception != null) if (exception != null)
@@ -114,8 +115,7 @@ public final class Logger {
logWriter.println(log); logWriter.println(log);
if (exception != null) if (exception != null)
exception.printStackTrace(logWriter); exception.printStackTrace(logWriter);
} else if (event instanceof LogEvent.ExportLog) { } else if (event instanceof LogEvent.ExportLog exportEvent) {
LogEvent.ExportLog exportEvent = (LogEvent.ExportLog) event;
logWriter.flush(); logWriter.flush();
try { try {
if (logFile != null) { if (logFile != null) {
@@ -368,7 +368,9 @@ public final class Logger {
log(Level.TRACE, CallerFinder.getCaller(), msg, exception); log(Level.TRACE, CallerFinder.getCaller(), msg, exception);
} }
private static final class LogFile implements Comparable<LogFile> { private record LogFile(Path file,
int year, int month, int day, int hour, int minute, int second,
int n) implements Comparable<LogFile> {
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})T(?<hour>\\d{2})-(?<minute>\\d{2})-(?<second>\\d{2})(\\.(?<n>\\d+))?\\.log(\\.(gz|xz))?"); private static final Pattern FILE_NAME_PATTERN = Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})T(?<hour>\\d{2})-(?<minute>\\d{2})-(?<second>\\d{2})(\\.(?<n>\\d+))?\\.log(\\.(gz|xz))?");
private static @Nullable LogFile ofFile(Path file) { 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); 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 @Override
public int compareTo(@NotNull Logger.LogFile that) { public int compareTo(@NotNull Logger.LogFile that) {
if (this.year != that.year) return Integer.compare(this.year, that.year); if (this.year != that.year) return Integer.compare(this.year, that.year);
@@ -429,7 +411,7 @@ public final class Logger {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof LogFile && compareTo((LogFile) obj) == 0; return obj instanceof LogFile that && compareTo(that) == 0;
} }
} }
} }