From 4694f748136dac425b9e2ed3ae930bcf988d51b0 Mon Sep 17 00:00:00 2001 From: mattirn Date: Mon, 13 Apr 2020 17:15:04 +0200 Subject: [PATCH] Repl demo: added trace script --- .../org/jline/builtins/ConsoleEngineImpl.java | 3 + .../jline/builtins/SystemRegistryImpl.java | 10 ++-- demo/src/main/scripts/trace.groovy | 57 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 demo/src/main/scripts/trace.groovy diff --git a/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java b/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java index c7e569789..0e81017f7 100644 --- a/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java +++ b/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java @@ -39,6 +39,7 @@ import org.jline.utils.AttributedString; import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; +import org.jline.utils.Log; /** * Manage console variables, commands and script execution. @@ -947,8 +948,10 @@ private void error(String message) { @Override public void println(Object object) { + long start = new Date().getTime(); Map options = defaultPrntOptions(); println(options, object); + Log.debug("println: ", new Date().getTime() - start, " msec"); } @Override diff --git a/builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java b/builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java index a4efddae3..2904b5c72 100644 --- a/builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java +++ b/builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java @@ -42,12 +42,9 @@ import org.jline.reader.impl.completer.StringsCompleter; import org.jline.terminal.Attributes; import org.jline.terminal.Attributes.InputFlag; +import org.jline.utils.*; import org.jline.terminal.Terminal; import org.jline.terminal.TerminalBuilder; -import org.jline.utils.AttributedString; -import org.jline.utils.AttributedStringBuilder; -import org.jline.utils.AttributedStyle; -import org.jline.utils.OSUtils; /** * Aggregate command registeries. @@ -423,6 +420,8 @@ public void open() throws IOException { terminal = TerminalBuilder.builder() .streams(in, outputStream) .attributes(attrs) + .jna(false) + .jansi(false) .type(Terminal.TYPE_DUMB).build(); this.commandSession = new CommandRegistry.CommandSession(terminal, terminal.input(), out, out); redirecting = true; @@ -1063,6 +1062,7 @@ public Object execute(String line) throws Exception { if (line.isEmpty() || line.trim().startsWith("#")) { return null; } + long start = new Date().getTime(); Object out = null; boolean statement = false; boolean postProcessed = false; @@ -1135,6 +1135,7 @@ public Object execute(String line) throws Exception { if (errorCount == 0) { names.extractNames(line); } + Log.debug("execute: ", new Date().getTime() - start, " msec"); return out; } @@ -1214,6 +1215,7 @@ public void trace(boolean stack, Exception exception) { asb.append(exception.getClass().getCanonicalName(), AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)); } asb.toAttributedString().println(terminal()); + Log.debug("Stack: ", exception); } } diff --git a/demo/src/main/scripts/trace.groovy b/demo/src/main/scripts/trace.groovy new file mode 100644 index 000000000..aec798aad --- /dev/null +++ b/demo/src/main/scripts/trace.groovy @@ -0,0 +1,57 @@ +// END_HELP +import java.util.logging.* +import org.jline.utils.* +import org.jline.builtins.Options + +class Trace { + + static def set(def args) { + String[] usage = [ + "trace - set JLine REPL console trace level", + "Usage: trace [LEVEL]", + " -? --help Displays command help" + ] + Options opt = Options.compile(usage).parse(args) + if (opt.isSet("help")) { + throw new Options.HelpException(opt.usage()) + } + def logger = LogManager.getLogManager().getLogger("") + def console = org.jline.builtins.SystemRegistry.get().consoleEngine() + def out + def CONSOLE_OPTIONS = console.getVariable('CONSOLE_OPTIONS'); + def handlers = logger.getHandlers() + if (!opt.args() || !opt.args().get(0).isInteger()) { + out = [:] + out['CONSOLE_OPTIONS.trace'] = CONSOLE_OPTIONS.trace + int i = 0 + for (def h : handlers) { + out['Log.handler' + i++] = h + } + } else { + def level = opt.args().get(0).toInteger() + if (!handlers) { + def handler = new ConsoleHandler() + System.setProperty("java.util.logging.SimpleFormatter.format",'%5$s%n') + SimpleFormatter formatter = new SimpleFormatter() + handler.setFormatter(formatter) + logger.addHandler(handler) + handlers = logger.getHandlers() + } + def tl = Level.OFF + CONSOLE_OPTIONS.trace = level + if (level == 2) { + tl = Level.FINE + } else if (level > 2) { + tl = Level.ALL + } + console.putVariable('CONSOLE_OPTIONS', CONSOLE_OPTIONS) + logger.setLevel(tl) + handlers.each { it.setLevel(tl) } + } + out + } +} + +def static main(def _args){ + Trace.set(_args) +}