Skip to content

Commit

Permalink
Avoid caching log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 committed Mar 13, 2022
1 parent 678deb7 commit 4765151
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
4 changes: 1 addition & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
<directory>${project.basedir}</directory>
<followSymlinks>false</followSymlinks>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>MinecraftDecompiler.jar</include>
</includes>
<includes><include>MinecraftDecompiler.jar</include></includes>
</fileset>
</filesets>
</configuration>
Expand Down
45 changes: 39 additions & 6 deletions src/main/java/cn/maxpixel/mcdecompiler/util/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ public final class Logging {
PARENT = Logger.getLogger("cn.maxpixel.mcdecompiler");
PARENT.setUseParentHandlers(false);
PARENT.setLevel(CONFIG.globalLevel);
StreamHandler handler = new StreamHandler(System.out, new LogFormatter());
StdoutHandler handler = new StdoutHandler();
handler.setLevel(CONFIG.globalLevel);
try {
handler.setEncoding(StandardCharsets.UTF_8.name());
} catch(UnsupportedEncodingException e) {
throw Utils.wrapInRuntime(e);// Shouldn't happen
}
PARENT.addHandler(handler);
}

Expand Down Expand Up @@ -125,6 +120,44 @@ public static LogConfiguration fromInputStream(InputStream is) {
}
}

private static final class StdoutHandler extends Handler {
{
setFormatter(new LogFormatter());
}

private boolean doneHeader;

@Override
public void publish(LogRecord record) {
if (!isLoggable(record)) return;
String msg;
try {
msg = getFormatter().format(record);
} catch (Exception ex) {
reportError(null, ex, ErrorManager.FORMAT_FAILURE);
return;
}

try {
if (!doneHeader) {
System.out.print(getFormatter().getHead(this));
doneHeader = true;
}
System.out.print(msg);
} catch (Exception ex) {
reportError(null, ex, ErrorManager.WRITE_FAILURE);
}
}

@Override
public void flush() {
}

@Override
public void close() throws SecurityException {
}
}

private static final class LogFormatter extends Formatter {
private static final ZoneId zone = ZoneId.systemDefault();
@Override
Expand Down

0 comments on commit 4765151

Please sign in to comment.