Skip to content

Commit

Permalink
Repl demo: added trace script
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 13, 2020
1 parent e4c6f88 commit 4694f74
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -947,8 +948,10 @@ private void error(String message) {

@Override
public void println(Object object) {
long start = new Date().getTime();
Map<String,Object> options = defaultPrntOptions();
println(options, object);
Log.debug("println: ", new Date().getTime() - start, " msec");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
57 changes: 57 additions & 0 deletions demo/src/main/scripts/trace.groovy
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4694f74

Please sign in to comment.