Skip to content

Commit

Permalink
启动发生错误时刷新log4j输出到控制台
Browse files Browse the repository at this point in the history
  • Loading branch information
Luohuayu committed Oct 3, 2020
1 parent 2a54a03 commit eb3ed0a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/main/java/catserver/server/CatGradleStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ protected void launch(String[] args) throws Throwable
extras = null;

System.gc();
Class.forName("net.minecraft.launchwrapper.Launch").getDeclaredMethod("main", String[].class).invoke(null, new Object[] { args });
try {
Class.forName("net.minecraft.launchwrapper.Launch").getDeclaredMethod("main", String[].class).invoke(null, new Object[] { args });
} catch (Throwable throwable) {
throwable.printStackTrace();
catserver.server.utils.Log4jUitls.flush(); // CatServer - flush error to console
}
}

private String[] getArgs()
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/catserver/server/utils/Log4jUitls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package catserver.server.utils;

import com.mojang.util.QueueLogAppender;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.ReadWriteLock;

public class Log4jUitls {
public static void flush() {
try {
Field filed_QUEUES = QueueLogAppender.class.getDeclaredField("QUEUES");
filed_QUEUES.setAccessible(true);

Field filed_QUEUE_LOCK = QueueLogAppender.class.getDeclaredField("QUEUE_LOCK");
filed_QUEUE_LOCK.setAccessible(true);

Map<String, BlockingQueue<String>> QUEUES = (Map<String, BlockingQueue<String>>) filed_QUEUES.get(null);
ReadWriteLock QUEUE_LOCK = (ReadWriteLock) filed_QUEUE_LOCK.get(null);

QUEUE_LOCK.readLock().lock();
final BlockingQueue<String> queue = QUEUES.get("TerminalConsole");
if (queue != null) {
String message;
while ((message = queue.poll()) != null) {
System.out.write(message.getBytes());
System.out.flush();
}
}
QUEUE_LOCK.readLock().unlock();
} catch (Exception ignored) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void injectIntoClassLoader(LaunchClassLoader classLoader)
classLoader.addClassLoaderExclusion("jline.");
classLoader.addClassLoaderExclusion("org.fusesource.");
classLoader.addClassLoaderExclusion("net.minecraftforge.server.console.log4j.TerminalConsoleAppender");
classLoader.addClassLoaderExclusion("catserver.server.utils.log4j.Log4jUitls");

FMLLaunchHandler.configureForServerLaunch(classLoader, this);
FMLLaunchHandler.appendCoreMods();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ private void run(String[] args)
}
catch (Exception e)
{
System.err.printf("A problem occurred running the Server launcher.");
System.err.println("A problem occurred running the Server launcher.");
e.printStackTrace(System.err);
catserver.server.utils.Log4jUitls.flush(); // CatServer - flush error to console
System.exit(1);
}
}
Expand Down

0 comments on commit eb3ed0a

Please sign in to comment.