Skip to content

Commit

Permalink
Properly shut down terminal addons while launching, fixes an issue wh…
Browse files Browse the repository at this point in the history
…ere jline would be unresponsive on ketting
  • Loading branch information
JustRed23 committed Mar 4, 2024
1 parent 0a3d5ed commit f1d1b3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/kettingpowered/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.kettingpowered.launcher.log.LogLevel;
import org.kettingpowered.launcher.log.Logger;
import org.kettingpowered.launcher.log.impl.Log4jImpl;
import org.kettingpowered.launcher.log.impl.SysoutImpl;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -201,6 +202,9 @@ public static void main(String[] args) throws Throwable {

I18n.log("info.launcher.launching");

//use default logging implementation when launching
Logger.setImpl(new SysoutImpl());

Class.forName(launchClass, true, Main.class.getClassLoader())
.getDeclaredMethod("main", String[].class)
.invoke(null, (Object) launchArgs.toArray(String[]::new));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/kettingpowered/launcher/log/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public abstract class Logger {
private static Logger impl = new SysoutImpl();

public static void setImpl(Logger impl) {
if (Logger.impl != null) Logger.impl._shutdown();
Logger.impl = impl;
I18n.logDebug("logger.impl_changed", impl.getClass().getName());
}
Expand Down Expand Up @@ -47,4 +48,5 @@ protected void _marker(String marker, String message) {
//Default implementation does nothing
_log(LogLevel.INFO, message);
}
protected void _shutdown() {}
}
17 changes: 15 additions & 2 deletions src/main/java/org/kettingpowered/launcher/log/impl/Log4jImpl.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.kettingpowered.launcher.log.impl;

import net.minecrell.terminalconsole.TerminalConsoleAppender;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.core.pattern.AnsiEscape;
import org.apache.logging.log4j.core.LoggerContext;
import org.kettingpowered.launcher.log.LogLevel;
import org.kettingpowered.launcher.log.Logger;

import java.io.IOException;

public class Log4jImpl extends Logger {

private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger("Launcher");
private static final LoggerContext CONTEXT = (LoggerContext) LogManager.getContext(false);
private static final org.apache.logging.log4j.Logger LOGGER = CONTEXT.getLogger("Launcher");

protected void _log(LogLevel level, String message) {
LOGGER.log(level.toLog4jLevel(), message);
Expand All @@ -25,4 +29,13 @@ protected void _log(LogLevel level, String message, Throwable throwable) {
protected void _marker(String marker, String message) {
LOGGER.info(MarkerManager.getMarker(marker), message);
}

protected void _shutdown() {
CONTEXT.terminate();
try {
TerminalConsoleAppender.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

1 comment on commit f1d1b3a

@JustRed23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.