mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-20 12:20:11 +00:00
Improve server console implementation (#4821)
This commit is contained in:
1 parent
43895d773e
commit
1db28d072a
18 files changed
+1106
-554
No files matched your search
@@ -21,21 +21,21 @@ package net.minecraftforge.server.console;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import jline.console.completer.Completer;
|
||||
import net.minecraft.server.dedicated.DedicatedServer;
|
||||
import org.jline.reader.Candidate;
|
||||
import org.jline.reader.Completer;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.ParsedLine;
|
||||
|
||||
public final class ConsoleCommandCompleter implements Completer
|
||||
final class ConsoleCommandCompleter implements Completer
|
||||
{
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
private final DedicatedServer server;
|
||||
|
||||
@@ -45,10 +45,9 @@ public final class ConsoleCommandCompleter implements Completer
|
||||
}
|
||||
|
||||
@Override
|
||||
public int complete(String buffer, int cursor, List<CharSequence> candidates)
|
||||
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates)
|
||||
{
|
||||
int len = buffer.length();
|
||||
|
||||
String buffer = line.line();
|
||||
boolean prefix;
|
||||
if (buffer.isEmpty() || buffer.charAt(0) != '/')
|
||||
{
|
||||
@@ -61,44 +60,19 @@ public final class ConsoleCommandCompleter implements Completer
|
||||
}
|
||||
|
||||
final String input = buffer;
|
||||
Future<List<String>> tabComplete = this.server.callFromMainThread(new Callable<List<String>>() {
|
||||
Future<List<String>> tabComplete = this.server.callFromMainThread(() -> this.server.getTabCompletions(this.server, input, this.server.getPosition(), false));
|
||||
|
||||
@Override
|
||||
public List<String> call() throws Exception
|
||||
{
|
||||
return ConsoleCommandCompleter.this.server.getTabCompletions(ConsoleCommandCompleter.this.server, input,
|
||||
ConsoleCommandCompleter.this.server.getPosition(), false/* we're not a command block */);
|
||||
}
|
||||
});
|
||||
try
|
||||
{
|
||||
List<String> completions = tabComplete.get();
|
||||
Collections.sort(completions);
|
||||
if (prefix)
|
||||
for (String completion : tabComplete.get())
|
||||
{
|
||||
candidates.addAll(completions);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (String completion : completions)
|
||||
if (!completion.isEmpty())
|
||||
{
|
||||
candidates.add(completion.charAt(0) == '/' ? completion.substring(1) : completion);
|
||||
boolean hasPrefix = prefix || completion.charAt(0) != '/';
|
||||
Candidate candidate = new Candidate(hasPrefix ? completion : completion.substring(1));
|
||||
candidates.add(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
int pos = buffer.lastIndexOf(' ');
|
||||
if (pos == -1)
|
||||
{
|
||||
return cursor - len;
|
||||
}
|
||||
else if (prefix)
|
||||
{
|
||||
return cursor - len + pos + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return cursor - len + pos;
|
||||
}
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
@@ -108,8 +82,6 @@ public final class ConsoleCommandCompleter implements Completer
|
||||
{
|
||||
logger.error("Failed to tab complete", e);
|
||||
}
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.server.console;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.fusesource.jansi.Ansi;
|
||||
|
||||
import java.util.function.Function;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
|
||||
public final class ConsoleFormatter implements Function<String, String>
|
||||
{
|
||||
|
||||
public ConsoleFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
private static final String RESET = Ansi.ansi().reset().toString();
|
||||
|
||||
private static final ImmutableMap<Pattern, String> REPLACEMENTS = ImmutableMap.<Pattern, String> builder()
|
||||
.put(compile(TextFormatting.BLACK), Ansi.ansi().reset().fg(Ansi.Color.BLACK).boldOff().toString())
|
||||
.put(compile(TextFormatting.DARK_BLUE), Ansi.ansi().reset().fg(Ansi.Color.BLUE).boldOff().toString())
|
||||
.put(compile(TextFormatting.DARK_GREEN), Ansi.ansi().reset().fg(Ansi.Color.GREEN).boldOff().toString())
|
||||
.put(compile(TextFormatting.DARK_AQUA), Ansi.ansi().reset().fg(Ansi.Color.CYAN).boldOff().toString())
|
||||
.put(compile(TextFormatting.DARK_RED), Ansi.ansi().reset().fg(Ansi.Color.RED).boldOff().toString())
|
||||
.put(compile(TextFormatting.DARK_PURPLE), Ansi.ansi().reset().fg(Ansi.Color.MAGENTA).boldOff().toString())
|
||||
.put(compile(TextFormatting.GOLD), Ansi.ansi().reset().fg(Ansi.Color.YELLOW).boldOff().toString())
|
||||
.put(compile(TextFormatting.GRAY), Ansi.ansi().reset().fg(Ansi.Color.WHITE).boldOff().toString())
|
||||
.put(compile(TextFormatting.DARK_GRAY), Ansi.ansi().reset().fg(Ansi.Color.BLACK).bold().toString())
|
||||
.put(compile(TextFormatting.BLUE), Ansi.ansi().reset().fg(Ansi.Color.BLUE).bold().toString())
|
||||
.put(compile(TextFormatting.GREEN), Ansi.ansi().reset().fg(Ansi.Color.GREEN).bold().toString())
|
||||
.put(compile(TextFormatting.AQUA), Ansi.ansi().reset().fg(Ansi.Color.CYAN).bold().toString())
|
||||
.put(compile(TextFormatting.RED), Ansi.ansi().reset().fg(Ansi.Color.RED).bold().toString())
|
||||
.put(compile(TextFormatting.LIGHT_PURPLE), Ansi.ansi().reset().fg(Ansi.Color.MAGENTA).bold().toString())
|
||||
.put(compile(TextFormatting.YELLOW), Ansi.ansi().reset().fg(Ansi.Color.YELLOW).bold().toString())
|
||||
.put(compile(TextFormatting.WHITE), Ansi.ansi().reset().fg(Ansi.Color.WHITE).bold().toString())
|
||||
.put(compile(TextFormatting.OBFUSCATED), Ansi.ansi().a(Ansi.Attribute.BLINK_SLOW).toString())
|
||||
.put(compile(TextFormatting.BOLD), Ansi.ansi().a(Ansi.Attribute.UNDERLINE_DOUBLE).toString())
|
||||
.put(compile(TextFormatting.STRIKETHROUGH), Ansi.ansi().a(Ansi.Attribute.STRIKETHROUGH_ON).toString())
|
||||
.put(compile(TextFormatting.UNDERLINE), Ansi.ansi().a(Ansi.Attribute.UNDERLINE).toString())
|
||||
.put(compile(TextFormatting.ITALIC), Ansi.ansi().a(Ansi.Attribute.ITALIC).toString())
|
||||
.put(compile(TextFormatting.RESET), RESET)
|
||||
.build();
|
||||
|
||||
private static Pattern compile(TextFormatting formatting)
|
||||
{
|
||||
return Pattern.compile(formatting.toString(), Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(String text)
|
||||
{
|
||||
for (Map.Entry<Pattern, String> entry : REPLACEMENTS.entrySet())
|
||||
{
|
||||
text = entry.getKey().matcher(text).replaceAll(entry.getValue());
|
||||
}
|
||||
|
||||
return text + RESET;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.server.console;
|
||||
|
||||
import static jline.TerminalFactory.OFF;
|
||||
import static jline.console.ConsoleReader.RESET_LINE;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.apache.logging.log4j.core.Layout;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
||||
import org.apache.logging.log4j.core.layout.PatternLayout;
|
||||
import org.apache.logging.log4j.core.util.Booleans;
|
||||
import org.apache.logging.log4j.util.PropertiesUtil;
|
||||
import org.fusesource.jansi.AnsiConsole;
|
||||
|
||||
import java.util.function.Function;
|
||||
import com.google.common.base.Functions;
|
||||
|
||||
import jline.TerminalFactory;
|
||||
import jline.console.ConsoleReader;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* TODO 1.13 remove this class
|
||||
* @deprecated use the version in the log4j sub-package: {@link net.minecraftforge.server.console.log4j.TerminalConsoleAppender}
|
||||
* This is being moved into its own package so that the log configuration doesn't load this whole package,
|
||||
* which has references to Minecraft in it that can load lots of extra classes.
|
||||
*/
|
||||
@Deprecated
|
||||
@Plugin(name = "TerminalConsole", category = "Core", elementType = "appender", printObject = true)
|
||||
public class TerminalConsoleAppender extends AbstractAppender
|
||||
{
|
||||
|
||||
private static final boolean ENABLE_JLINE = PropertiesUtil.getProperties().getBooleanProperty("jline.enable", true);
|
||||
|
||||
private static final PrintStream out = System.out;
|
||||
|
||||
private static boolean initialized;
|
||||
private static ConsoleReader reader;
|
||||
|
||||
public static ConsoleReader getReader()
|
||||
{
|
||||
return reader;
|
||||
}
|
||||
|
||||
private static Function<String, String> formatter = Functions.identity();
|
||||
|
||||
public static void setFormatter(Function<String, String> format)
|
||||
{
|
||||
formatter = format != null ? format : Functions.identity();
|
||||
}
|
||||
|
||||
protected TerminalConsoleAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions)
|
||||
{
|
||||
super(name, filter, layout, ignoreExceptions);
|
||||
}
|
||||
|
||||
@PluginFactory
|
||||
@Nullable
|
||||
public static TerminalConsoleAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filters") Filter filter,
|
||||
@PluginElement("Layout") Layout<? extends Serializable> layout, @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) String ignore)
|
||||
{
|
||||
|
||||
if (name == null)
|
||||
{
|
||||
LOGGER.error("No name provided for TerminalConsoleAppender");
|
||||
return null;
|
||||
}
|
||||
if (layout == null)
|
||||
{
|
||||
layout = PatternLayout.newBuilder().build();
|
||||
}
|
||||
|
||||
boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
|
||||
|
||||
// This is handled by jline
|
||||
System.setProperty("log4j.skipJansi", "true");
|
||||
return new TerminalConsoleAppender(name, filter, layout, ignoreExceptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start()
|
||||
{
|
||||
// Initialize the reader if that hasn't happened yet
|
||||
if (!initialized && reader == null)
|
||||
{
|
||||
initialized = true;
|
||||
|
||||
if (ENABLE_JLINE)
|
||||
{
|
||||
final boolean hasConsole = System.console() != null;
|
||||
if (hasConsole)
|
||||
{
|
||||
try
|
||||
{
|
||||
AnsiConsole.systemInstall();
|
||||
reader = new ConsoleReader();
|
||||
reader.setExpandEvents(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warn("Failed to initialize terminal. Falling back to default.", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (reader == null)
|
||||
{
|
||||
// Eclipse doesn't support colors and characters like \r so enabling jline2 on it will
|
||||
// just cause a lot of issues with empty lines and weird characters.
|
||||
// Enable jline2 only on IntelliJ IDEA to prevent that.
|
||||
// Also see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936
|
||||
|
||||
if (hasConsole || System.getProperty("java.class.path").contains("idea_rt.jar"))
|
||||
{
|
||||
// Disable advanced jline features
|
||||
TerminalFactory.configure(OFF);
|
||||
TerminalFactory.reset();
|
||||
|
||||
try
|
||||
{
|
||||
reader = new ConsoleReader();
|
||||
reader.setExpandEvents(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warn("Failed to initialize fallback terminal. Falling back to standard output console.", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.warn("Disabling terminal, you're running in an unsupported environment.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(LogEvent event)
|
||||
{
|
||||
if (reader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Writer out = reader.getOutput();
|
||||
out.write(RESET_LINE);
|
||||
out.write(formatEvent(event));
|
||||
|
||||
reader.drawLine();
|
||||
reader.flush();
|
||||
}
|
||||
catch (IOException ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
out.print(formatEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
protected String formatEvent(LogEvent event)
|
||||
{
|
||||
return formatter.apply(getLayout().toSerializable(event).toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,63 +19,73 @@
|
||||
|
||||
package net.minecraftforge.server.console;
|
||||
|
||||
import java.io.IOException;
|
||||
import net.minecraftforge.server.terminalconsole.TerminalConsoleAppender;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import jline.console.ConsoleReader;
|
||||
import net.minecraft.server.dedicated.DedicatedServer;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraftforge.server.console.log4j.TerminalConsoleAppender;
|
||||
import org.jline.reader.EndOfFileException;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.UserInterruptException;
|
||||
import org.jline.terminal.Terminal;
|
||||
|
||||
public final class TerminalHandler
|
||||
{
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
|
||||
private TerminalHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public static boolean handleCommands(DedicatedServer server)
|
||||
{
|
||||
final ConsoleReader reader = TerminalConsoleAppender.getReader();
|
||||
if (reader != null)
|
||||
{
|
||||
TerminalConsoleAppender.setFormatter(new ConsoleFormatter());
|
||||
reader.addCompleter(new ConsoleCommandCompleter(server));
|
||||
final Terminal terminal = TerminalConsoleAppender.getTerminal();
|
||||
if (terminal == null)
|
||||
return false;
|
||||
|
||||
LineReader reader = LineReaderBuilder.builder()
|
||||
.appName("Forge")
|
||||
.terminal(terminal)
|
||||
.completer(new ConsoleCommandCompleter(server))
|
||||
.build();
|
||||
reader.setOpt(LineReader.Option.DISABLE_EVENT_EXPANSION);
|
||||
reader.unsetOpt(LineReader.Option.INSERT_TAB);
|
||||
|
||||
TerminalConsoleAppender.setReader(reader);
|
||||
|
||||
try
|
||||
{
|
||||
String line;
|
||||
while (!server.isServerStopped() && server.isServerRunning())
|
||||
{
|
||||
try
|
||||
{
|
||||
line = reader.readLine("> ");
|
||||
if (line == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
line = line.trim();
|
||||
if (!line.isEmpty())
|
||||
{
|
||||
server.addPendingCommand(line, server);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
catch (EndOfFileException ignored)
|
||||
{
|
||||
logger.error("Exception handling console input", e);
|
||||
// Continue reading after EOT
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
line = line.trim();
|
||||
if (!line.isEmpty())
|
||||
{
|
||||
server.addPendingCommand(line, server);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
catch (UserInterruptException e)
|
||||
{
|
||||
TerminalConsoleAppender.setFormatter(TextFormatting::getTextWithoutFormattingCodes);
|
||||
return false;
|
||||
server.initiateShutdown();
|
||||
}
|
||||
finally
|
||||
{
|
||||
TerminalConsoleAppender.setReader(null);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.server.console.log4j;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static jline.TerminalFactory.OFF;
|
||||
import static jline.console.ConsoleReader.RESET_LINE;
|
||||
|
||||
import com.google.common.base.Functions;
|
||||
import jline.TerminalFactory;
|
||||
import jline.console.ConsoleReader;
|
||||
import org.apache.logging.log4j.core.Filter;
|
||||
import org.apache.logging.log4j.core.Layout;
|
||||
import org.apache.logging.log4j.core.LogEvent;
|
||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginElement;
|
||||
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
||||
import org.apache.logging.log4j.core.layout.PatternLayout;
|
||||
import org.apache.logging.log4j.core.util.Booleans;
|
||||
import org.apache.logging.log4j.util.PropertiesUtil;
|
||||
import org.fusesource.jansi.AnsiConsole;
|
||||
|
||||
@Plugin(name = "TerminalConsole", category = "Core", elementType = "appender", printObject = true)
|
||||
public class TerminalConsoleAppender extends AbstractAppender
|
||||
{
|
||||
|
||||
private static final boolean ENABLE_JLINE = PropertiesUtil.getProperties().getBooleanProperty("jline.enable", true);
|
||||
|
||||
private static final PrintStream out = System.out;
|
||||
|
||||
private static boolean initialized;
|
||||
private static ConsoleReader reader;
|
||||
|
||||
public static ConsoleReader getReader()
|
||||
{
|
||||
return reader;
|
||||
}
|
||||
|
||||
private static Function<String, String> formatter = Functions.identity();
|
||||
|
||||
public static void setFormatter(Function<String, String> format)
|
||||
{
|
||||
formatter = format != null ? format : Functions.identity();
|
||||
}
|
||||
|
||||
protected TerminalConsoleAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions)
|
||||
{
|
||||
super(name, filter, layout, ignoreExceptions);
|
||||
}
|
||||
|
||||
@PluginFactory
|
||||
@Nullable
|
||||
public static TerminalConsoleAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filters") Filter filter,
|
||||
@PluginElement("Layout") Layout<? extends Serializable> layout, @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) String ignore)
|
||||
{
|
||||
|
||||
if (name == null)
|
||||
{
|
||||
LOGGER.error("No name provided for TerminalConsoleAppender");
|
||||
return null;
|
||||
}
|
||||
if (layout == null)
|
||||
{
|
||||
layout = PatternLayout.newBuilder().build();
|
||||
}
|
||||
|
||||
boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
|
||||
|
||||
// This is handled by jline
|
||||
System.setProperty("log4j.skipJansi", "true");
|
||||
return new TerminalConsoleAppender(name, filter, layout, ignoreExceptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start()
|
||||
{
|
||||
// Initialize the reader if that hasn't happened yet
|
||||
if (!initialized && reader == null)
|
||||
{
|
||||
initialized = true;
|
||||
|
||||
if (ENABLE_JLINE)
|
||||
{
|
||||
final boolean hasConsole = System.console() != null;
|
||||
if (hasConsole)
|
||||
{
|
||||
try
|
||||
{
|
||||
AnsiConsole.systemInstall();
|
||||
reader = new ConsoleReader();
|
||||
reader.setExpandEvents(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warn("Failed to initialize terminal. Falling back to default.", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (reader == null)
|
||||
{
|
||||
// Eclipse doesn't support colors and characters like \r so enabling jline2 on it will
|
||||
// just cause a lot of issues with empty lines and weird characters.
|
||||
// Enable jline2 only on IntelliJ IDEA to prevent that.
|
||||
// Also see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936
|
||||
|
||||
if (hasConsole || System.getProperty("java.class.path").contains("idea_rt.jar"))
|
||||
{
|
||||
// Disable advanced jline features
|
||||
TerminalFactory.configure(OFF);
|
||||
TerminalFactory.reset();
|
||||
|
||||
try
|
||||
{
|
||||
reader = new ConsoleReader();
|
||||
reader.setExpandEvents(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warn("Failed to initialize fallback terminal. Falling back to standard output console.", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.warn("Disabling terminal, you're running in an unsupported environment.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(LogEvent event)
|
||||
{
|
||||
if (reader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Writer out = reader.getOutput();
|
||||
out.write(RESET_LINE);
|
||||
out.write(formatEvent(event));
|
||||
|
||||
reader.drawLine();
|
||||
reader.flush();
|
||||
}
|
||||
catch (IOException ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
out.print(formatEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
protected String formatEvent(LogEvent event)
|
||||
{
|
||||
return formatter.apply(getLayout().toSerializable(event).toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user