From 6db52d7f8f19659863ddae04a301e3c751cabc2f Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Tue, 17 Dec 2024 13:26:06 +0100 Subject: [PATCH] LDEV-5206 support writing executionLog straight to console, with optional code snippet --- .../runtime/engine/ConsoleExecutionLog.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/lucee/runtime/engine/ConsoleExecutionLog.java b/core/src/main/java/lucee/runtime/engine/ConsoleExecutionLog.java index db4b0ca542..e034027b14 100644 --- a/core/src/main/java/lucee/runtime/engine/ConsoleExecutionLog.java +++ b/core/src/main/java/lucee/runtime/engine/ConsoleExecutionLog.java @@ -23,23 +23,30 @@ import lucee.commons.io.log.Log; import lucee.commons.io.log.LogUtil; +import lucee.commons.io.res.util.ResourceSnippet; +import lucee.commons.io.res.util.ResourceSnippetsMap; import lucee.runtime.PageContext; +import lucee.runtime.PageContextImpl; +import lucee.runtime.functions.math.Int; +import lucee.runtime.op.Caster; public class ConsoleExecutionLog extends ExecutionLogSupport { private PrintWriter pw; private PageContext pc; + private ResourceSnippetsMap snippetsMap = new ResourceSnippetsMap(1024, 128); + private boolean snippet = false; @Override protected void _init(PageContext pc, Map arguments) { this.pc = pc; - + if (Caster.toBooleanValue(arguments.get("snippet"), false)) snippet = true; if (pw == null) { // stream type String type = arguments.get("stream-type"); - if (type != null && type.trim().equalsIgnoreCase("error")) pw = new PrintWriter(System.err); - else pw = new PrintWriter(System.out); - + if (type == null) return; + if (type.trim().equalsIgnoreCase("error")) pw = new PrintWriter(System.err); + else if (type.trim().equalsIgnoreCase("out")) pw = new PrintWriter(System.out); } } @@ -47,15 +54,28 @@ protected void _init(PageContext pc, Map arguments) { protected void _log(int startPos, int endPos, long startTime, long endTime) { long diff = endTime - startTime; - LogUtil.log(pc, Log.LEVEL_TRACE, Controler.class.getName(), - pc.getId() + ":" + pc.getCurrentPageSource().getDisplayPath() + ":" + positons(startPos, endPos) + " > " + timeLongToString(diff)); + String log = pc.getId() + ":" + pc.getCurrentPageSource().getDisplayPath() + ":"; + if (snippet) { + ResourceSnippet snippet = snippetsMap.getSnippet(pc.getCurrentPageSource(), startPos, endPos, ((PageContextImpl) pc).getResourceCharset().name()); + log += positions(snippet.getEndLine(), snippet.getEndLine()) + " > " + timeLongToString(diff) + " [" + snippet.getContent() + "]"; + } else { + log += positions(startPos, endPos) + " > " + timeLongToString(diff); + } + if (pw != null){ + pw.print(log + "\n"); + pw.flush(); + } else { + LogUtil.log(pc, Log.LEVEL_TRACE, Controler.class.getName(), log); + } } @Override protected void _release() { + if (pw != null) pw.close(); + snippetsMap = null; } - private static String positons(int startPos, int endPos) { + private static String positions(int startPos, int endPos) { if (startPos == endPos) return startPos + ""; return startPos + ":" + endPos; }